Copy rows with JOpenDocument - java

I am trying to copy a row several times using JOpenDocument v1.3 and I am failing miserably.
Here is my code snippet:
private void copyRow(Sheet sheet, int templateRow, int target){
sheet.ensureRowCount(target);
sheet.duplicateRows(templateRow, 1, 1);
sheet.insertDuplicatedRows(templateRow, target);
}
No row gets changed with the code above and no error is displayed.
I know I could copy each cell separately but I think if there is a way to do it all at once it should be more efficient.
Thanks in advance.

Related

Blank page added when table splits due to a page break using iText 5.5.7

I had some code that worked with iText 2.1.7. We decided to move to the latest version of iText. I tested the code with version 5.5.6 and the code also worked perfectly.
When version 5.5.7 came out last month we decided to upgrade to that version as it had some fixes regarding performance when using nested tables and splitting rows that seemed relevant to the project.
However in some scenarios, a blank page is added to the PDF when the table splits due to a page break. As I said, the code works fine with iText 2.1.7 and iText 5.5.6.
Here is the code used to build the table:
float[] widthsIllustration = { 0.75f, 8 };
PdfPTable tableIllustration = ReportBuilderCommon.buildTable(2, 100, widthsIllustration);
tableIllustration.setSplitLate(false);
tableIllustration.getDefaultCell().setBorder(Rectangle.BOTTOM);
tableIllustration.getDefaultCell().setPaddingBottom(5);
The table above is nested in another table created like this
PdfPTable leftColumn = ReportBuilderCommon.buildTable(1, 100);
leftColumn.setSplitLate(false);
Here is the method buildTable:
public static PdfPTable buildTable(final int numColums, final float widthPercentage) throws DocumentException {
PdfPTable table = new PdfPTable(numColums);
table.setWidthPercentage(widthPercentage);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
return table;
}

XLLoop (Excel 2010) - Integrating a java function that returns an array/list of values

I've started looking at XLLoop as I need a way of getting data from a java system into Excel (2010 version).
I've followed the java SimpleServer example on the front page of the website:
package org.boris.xlloop.util;
import org.boris.xlloop.FunctionServer;
import org.boris.xlloop.handler.*;
import org.boris.xlloop.reflect.*;
public class ServerExample
{
public static void main(String[] args) throws Exception {
// Create function server on the default port
FunctionServer fs = new FunctionServer();
// Create a reflection function handler and add the Math methods
ReflectFunctionHandler rfh = new ReflectFunctionHandler();
rfh.addMethods("Math.", Math.class);
rfh.addMethods("Math.", Maths.class);
rfh.addMethods("CSV.", CSV.class);
rfh.addMethods("Reflect.", Reflect.class);
// Create a function information handler to register our functions
FunctionInformationHandler firh = new FunctionInformationHandler();
firh.add(rfh.getFunctions());
// Set the handlers
CompositeFunctionHandler cfh = new CompositeFunctionHandler();
cfh.add(rfh);
cfh.add(firh);
fs.setFunctionHandler(new DebugFunctionHandler(cfh));
// Run the engine
System.out.println("Listening on port " + fs.getPort() + "...");
fs.run();
}
}
and have successfully been able to integrate simple static java methods into Excel that return one value. E.g.Math.random(), Math.pow(2, 4.45) which both return a single double.
However, I have been unable to integrate any functions that return an array/list of values. For example, in the above we have registered the Maths class (an XLLoop class) for use in Excel. This class contains a static method
public static double[] normalDist(int var0)
The method returns an array of length var0, however when I call this in Excel only one cell is populated with a value, I would have expected var0 cells to have been populated. This seems like a fairly fundamental feature to me, so I'm pretty sure I must be doing something daft.
If there is anyone out there who could help with this it would be greatly appreciated.
I should also mention that I found a similar SO question , linked from a R related SO questions feed. Unfortunately the original SO question has been removed from SO by the poster...
You need to enter the function calling the method that returns an array as a matrix-function in excel: select the cells that shall recieve the returned data, enter the function and hit ctrl-shift-enter.
If the range selected is too small, then only the values fitting in the range are displayed.

APACHE POI autoSizeColumn with useMergedCells too slow

I try to apply autoSizeColumn on an Excel sheet. I'm using POI 3.10.1.
I apply autoSizeColumn at the end but the problem is than the process is too slow/long anyway:
On the sheet I've approximately 1000 lines and 20 columns... After 5 hours, I kill the process ...
I don't understand what is taking so long, 1000 lines and 20 columns doesn't appear so huge: Did I miss something? (nb: on a smaller file, it's working)
My simplified code below:
Workbook vWorkbook = getWorkbook();
Sheet vSheet = vWorkbook.createSheet("sheet");
CreationHelper vHelper = vWorkbook.getCreationHelper();
Drawing drawing = vSheet.createDrawingPatriarch();
Set<CellRangeAddress> vRegions = new HashSet<CellRangeAddress>();
//Parcours des lignes du document
MatrixDocument vMatrixDocument = getMatrixDocument();
List<MatrixRow> vListMatrixRows = vMatrixDocument.getRows();
int maxColNb = 0;
//Parcours des lignes de la grille.
for (MatrixRow vMatrixRow : vListMatrixRows)
{
//(...)
//create cells
//(...)
}
initColSpan(vListMatrixRows, vRegions);
//Gestion des colSpan et des RowSpan
for (CellRangeAddress vRegion : vRegions)
{
vSheet.addMergedRegion(vRegion);
}
for (int i = 0; i < maxColNb; ++i)
{
vSheet.autoSizeColumn(i, true);//Here the problem. spent more than 5 hour for 1000 lines and 20 columns
}
I've already read threads below :
http://stackoverflow.com/questions/16943493/apache-poi-autosizecolumn-resizes-incorrectly
http://stackoverflow.com/questions/15740100/apache-poi-autosizecolumn-not-working-right
http://stackoverflow.com/questions/23366606/autosizecolumn-performance-effect-in-apache-poi
http://stackoverflow.com/questions/18984785/a-poi-related-code-block-running-dead-slow
http://stackoverflow.com/questions/28564045/apache-poi-autosizecolumn-behaving-weird
http://stackoverflow.com/questions/18456474/apache-poi-autosizecolumn-is-not-working
But none solve my issue.
Any idea ?
PS : I tried to upload an example image of the Excel file but I don't find how to upload it.
Even after using Apache POI 3.12, I was facing the same issue for auto-sizing. Also auto-size doesn't work in Unix/Linux.
What I learnt from various forums is this:
1.You can try is using the SXSSF API, usually works much faster.
2. If not, then go for setColumnWidth method(I know its literally manual work for 20 columns)
My solution was only use Autosize on the last Excel line, like this:
if (i >= abaExcel.Itens.Count)
sheet.AutoSizeColumn(j);
Because merged regions cannot overlap without producing a corrupt document, POI may be checking the list of merged regions on a sheet for potential intersections before adding a merged region. This gives O(N) behavior for adding one region instead of the expected O(1).
addMergedRegion - with checking but slow
addMergedRegionUnsafe - without checking but fast
Documentation: read more about addMergedRegionUnsafe(...)

Is there a way to set the width of a column in XWPFTableCell(POI)?

The full path of the class is org.apache.poi.xwpf.usermodel.XWPFTableCell.
The API goes here: http://poi.apache.org/apidocs/
Thanks for your advices.
No. You cannot set the width of column in XWPFTableCell.
But you can always use HSSFSheet.autoSizeColumn(int column) after finished entering data in sheet.
You can do it; to find this I had to study its internal XML schema.
This is a major and very tricky element; I spent several days solving this problem and finally I had managed to solve it using this method of setting the widths of a table cell:
private static void setTableColumnWidths(XWPFTable table) {
table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2000));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(3200));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1000));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1000));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1105));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1105));
}

Eclipse shows error for MissingCellPolicy

I am using Apache POI to read data from Excel files. I have some blank/null cells in my Excel which I want to handle using MissingCellPolicy.RETURN_NULL_AND_BLANK. However, Eclipse underlines RETURN_NULL_AND_BLANK when I hover the mouse over it and I get a pop-up saying RETURN_NULL_AND_BLANK cannot be resolved or is not a field
I have imported the Row.MissingCellPolicy class. Can anyone point out what needs to be done?
MissingCellPolicy is the static nested class of the Row interface. And RETURN_NULL_AND_BLANK is the static field of the ROW interface.
So you should access with:
Row.RETURN_NULL_AND_BLANK;
Or use static import instead:
import static org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import static org.apache.poi.ss.usermodel.Row.RETURN_NULL_AND_BLANK;
//....
MissingCellPolicy policy = RETURN_NULL_AND_BLANK;
Adding Row.RETURN_NULL_AND_BLANK; helped me instead of adding Row.MissingCellPolicy.RETURN_NULL_AND_BLANK; or MissingCellPolicy.RETURN_NULL_AND_BLANK; and Eclipse doesn't throw error.
This is because my worksheet is XSSFSheet which returns an XSSFRow just Row and XSSFRow (or even HSSFRow for that matter) directly inherits these constants from the Row class. See more info here
Row is interface
MissingCellPolicy is the enum in Row
and
1. RETURN_NULL_AND_BLANK
2. RETURN_BLANK_AS_NULL
3. CREATE_NULL_AS_BLANK
is the static field
To Access any of 1 to 3
use
Row.MissingCellPolicy.RETURN_NULL_AND_BLANK
or use other two as per requirement

Categories