// Set the delete on exit flag, unless explicitly disabled
if (System.getProperty(KEEP_FILES) == null) {
newFile.deleteOnExit();
}
// All done
return newFile;
}
POI就是把超过window size的Row刷到临时文件里,然后再把临时文件转为正常的xlsx文件格式输出。
我们看看刷盘时写了什么,SheetDataWriter的writeRow方法
public void writeRow(int rownum, SXSSFRow row) throws IOException {
if (_numberOfFlushedRows == 0)
_lowestIndexOfFlushedRows = rownum;
_numberLastFlushedRow = Math.max(rownum, _numberLastFlushedRow);
_numberOfCellsOfLastFlushedRow = row.getLastCellNum();
_numberOfFlushedRows++;
beginRow(rownum, row);
Iterator cells = row.allCellsIterator();
int columnIndex = 0;
while (cells.hasNext()) {
writeCell(columnIndex++, cells.next());
}
endRow();
}
void beginRow(int rownum, SXSSFRow row) throws IOException {
_out.write(“<row”);
writeAttribute(“r”, Integer.toString(rownum + 1));
if (row.hasCustomHeight()) {
writeAttribute(“customHeight”, “true”);
writeAttribute(“ht”, Float.toString(row.getHeightInPoints()));
}
if (row.getZeroHeight()) {
writeAttribute(“hidden”, “true”);
}
if (row.isFormatted()) {
writeAttribute(“s”, Integer.toString(row.getRowStyleIndex()));
writeAttribute(“customFormat”, “1”);
}
if (row.getOutlineLevel() != 0) {
writeAttribute(“outlineLevel”, Integer.toString(row.getOutlineLevel()));
}
if(row.getHidden() != null) {
writeAttribute(“hidden”, row.getHidden() ? “1” : “0”);
}
if(row.getCollapsed() != null) {
writeAttribute(“collapsed”, row.getCollapsed() ? “1” : “0”);
}
_out.write(“>\n”);
this._rownum = rownum;
}
void endRow() throws IOException {
_out.write(“\n”);
}
public void writeCell(int columnIndex, Cell cell) throws IOException {
if (cell == null) {
return;
}
String ref = new CellReference(_rownum, columnIndex).formatAsString();
_out.write(“<c”);
writeAttribute(“r”, ref);
CellStyle cellStyle = cell.getCellStyle();
if (cellStyle.getIndex() != 0) {
// need to convert the short to unsigned short as the indexes can be up to 64k
// ideally we would use int for this index, but that would need changes to some more
// APIs
writeAttribute(“s”, Integer.toString(cellStyle.getIndex() & 0xffff));
}
CellType cellType = cell.getCellTypeEnum();
switch (cellType) {
case BLANK: {
_out.write(‘>’);
break;
}
case FORMULA: {
_out.write(“>”);
outputQuotedString(cell.getCellFormula());
_out.write(“”);
switch (cell.getCachedFormulaResultTypeEnum()) {
case NUMERIC:
double nval = cell.getNumericCellValue();
if (!Double.isNaN(nval)) {
_out.write(“”);
_out.write(Double.toString(nval));
_out.write(“”);
}
break;
default:
break;
}
break;
}
case STRING: {
if (_sharedStringSource != null) {
XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue());
int sRef = _sharedStringSource.addEntry(rt.getCTRst());
writeAttribute(“t”, STCellType.S.toString());
_out.write(“>”);
_out.write(String.valueOf(sRef));
_out.write(“”);
} else {
writeAttribute(“t”, “inlineStr”);
_out.write(“><t”);
if (hasLeadingTrailingSpaces(cell.getStringCellValue())) {
writeAttribute(“xml:space”, “preserve”);
}
_out.write(“>”);
outputQuotedString(cell.getStringCellValue());
_out.write(“”);
}
break;
}
case NUMERIC: {