问题
I have been working on Apache POI XSSF model for quite some time now. I'm trying to generate a excel sheet which is formatted and have some set of styles applied. For performance reasons i have been using the Big-Grid example provided by POI.
https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java.
Along with this i also want to apply the autoSizeColumn for each of the columns. But i'm not sure where should i apply the autoSizeColumn method in the given program.
private static void generate(Writer out, Map<String, XSSFCellStyle> styles,String inputFileName, XSSFSheet sheet) throws Exception {
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
SpreadsheetWriter sw = new SpreadsheetWriter(out);
sw.beginSheet();
Integer rowId = 0;
br = new BufferedReader(new FileReader(inputFileName));
while ((line = br.readLine()) != null) {
//use comma as separator
String[] reportRecords = line.split(cvsSplitBy);
if (rowId == 0) { // Print the Report Name
sw.insertRow(rowId++);
sw.createCell(0, StringEscapeUtils.escapeXml(line.substring(1, line.length() - 1)), styles.get("header").getIndex());
sw.endRow();
}
else if(rowId == 1){
sw.insertRow(rowId++);
for (int column = 0; column < reportRecords.length; column++) {
sw.createCell(column, StringEscapeUtils.escapeXml(reportRecords[column].substring(1,reportRecords[column].length() - 1)), styles.get("header").getIndex());
}
sw.endRow();
}
else{
sw.insertRow(rowId++);
for (int column = 0; column < reportRecords.length; column++) {
sw.createCell(column, StringEscapeUtils.escapeXml(reportRecords[column].substring(1,reportRecords[column].length() - 1)));
}
sw.endRow();
}
}
br.close();
sheet.autoSizeColumn(1);
sheet.autoSizeColumn(2);
sheet.autoSizeColumn(3);
sw.endSheet();
}
Your help is much appreciated in this regard.
Thanks!
回答1:
You mention the BigGridDemo. What you appear to have failed to read is the big warning at the top of the file, and the slightly smaller one further down through the header javadocs:
Note - You probably don't want to use this approach any more! POI now includes the SXSSF which handles all of this for you, you should be using that instead! This code remains mostly for historical interest.
As the header goes on to say, you should be using SXSSF instead. SXSSF provides a largely XSSF-compatible way to write a .xlsx
file, but by creating the XML itself and only keeping a small "window" of rows and cells in memory, retains the very low memory footprint that the BigGridDemo does
So, you need to change your code to use SXSSFWorkbook, but otherwise largely be the same as your memory-hungry XSSF version, and you'll be good
Well... Except, that autosizing requires access to all rows in a column, to know which cell is biggest. SXSSF can't give you that, as it has flushed almost all the rows to disk to save memory. So, your auto-sizing will only work for the handful of rows still in the window, as those are the only ones available to check the size of. It'll probably get it mostly right, as normally most cells in a column are the same size. It might not be perfect, but sadly that's one of the trade-offs of the lower memory use
来源:https://stackoverflow.com/questions/32012481/where-to-put-autosizecolumn-in-apache-poi