Java ApachePOISAX解析-如何获取单元格的实际值
我需要使用内存有限的ApachePOI解析非常大的excel文件。在谷歌搜索之后,我知道poi提供了SAX解析器来有效地解析大文件,而不需要消耗大量内存Java ApachePOISAX解析-如何获取单元格的实际值,java,excel,apache-poi,Java,Excel,Apache Poi,我需要使用内存有限的ApachePOI解析非常大的excel文件。在谷歌搜索之后,我知道poi提供了SAX解析器来有效地解析大文件,而不需要消耗大量内存 私有类SheetToCSV实现SheetContentsHandler{ private boolean firstfrow=false; private int currentRow=-1; private int currentCol=-1; 私有void outputMissingRows(整数){ 对于(int i=0;i流媒体接口的
私有类SheetToCSV实现SheetContentsHandler{
private boolean firstfrow=false;
private int currentRow=-1;
private int currentCol=-1;
私有void outputMissingRows(整数){
对于(int i=0;i流媒体接口的当前实现没有提供这一点。因此,为了实现这一点,您需要复制底层的代码,并对其进行调整,以使单元格内容不格式化。编写您自己的SAX处理程序以传入?您可以在本文中看到我的完整示例,非常感谢@centic
private class SheetToCSV implements SheetContentsHandler {
private boolean firstCellOfRow = false;
private int currentRow = -1;
private int currentCol = -1;
private void outputMissingRows(int number) {
for (int i=0; i<number; i++) {
for (int j=0; j<minColumns; j++) {
output.append(',');
}
output.append('\n');
}
}
@Override
public void startRow(int rowNum) {
// If there were gaps, output the missing rows
outputMissingRows(rowNum-currentRow-1);
// Prepare for this row
firstCellOfRow = true;
currentRow = rowNum;
currentCol = -1;
}
@Override
public void endRow(int rowNum) {
// Ensure the minimum number of columns
for (int i=currentCol; i<minColumns; i++) {
output.append(',');
}
output.append('\n');
}
@Override
public void cell(String cellReference, String formattedValue,
XSSFComment comment) {
if (firstCellOfRow) {
firstCellOfRow = false;
} else {
output.append(',');
}
// gracefully handle missing CellRef here in a similar way as XSSFCell does
if(cellReference == null) {
cellReference = new CellAddress(currentRow, currentCol).formatAsString();
}
// Did we miss any cells?
int thisCol = (new CellReference(cellReference)).getCol();
int missedCols = thisCol - currentCol - 1;
for (int i=0; i<missedCols; i++) {
output.append(',');
}
currentCol = thisCol;
// Number or string?
try {
Double.parseDouble(formattedValue);
output.append(formattedValue);
} catch (NumberFormatException e) {
output.append('"');
output.append(formattedValue);
output.append('"');
}
}
@Override
public void headerFooter(String text, boolean isHeader, String tagName) {
// Skip, no headers or footers in CSV
}
}