Excel大批量数据的导入和导出,如何做优化?

本文探讨了Excel大批量数据的导入和导出优化,主要关注Apache POI库在处理XLSX和XLS文件时的不同模式,包括用户模式、事件模式和Event User Model。事件模式和Event User Model通过流式处理减少内存占用,适合处理大量数据。文中通过代码示例解释了这些模式的工作原理,并展示了内存使用情况的测试结果。
摘要由CSDN通过智能技术生成

// 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: {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值