我使用Apache POI 3.7使用HSSF
创建Excel。它工作正常。但在负载测试期间,我意识到它非常慢。所以我用Google搜索,发现我可以使用SXSSF
。我将现有代码更改为XSSF
。结果太棒了。
但我坚持一个情况,autorezisecolum()
没有按预期运作。它将大内容显示为#####
,因为列宽会缩小。我发现这是一种已经出现的错误。
现在我的观点是,是否有任何解决方案,以便我可以使用SXSSF
(非常重要的性能)和良好的输出。
注意:我使用的是Windows 7,JDK 1.7.09,POI-3.10.beta-2
请帮帮我。
这是我的代码:
主要功能:
sxssfWorkbook = new SXSSFWorkbook(5);
sxssfSheet = (SXSSFSheet) sxssfWorkbook.createSheet(sheetName);
try {
// TO Write Header
//ew.writeHeaderRow(sheet, headerNames);
ew.writeHeaderRow(sxssfSheet, headerNames);
int rowNum = headerRow + 1;
for(Map.Entry<String, List<Object>> columnData : columnDataMap.entrySet()){
ew.writeNonHeaderRow(sxssfSheet, columnData.getValue(), rowNum);
rowNum++;
}
resizeXLSXColumns(sxssfSheet,rowNum-1);
sxssfWorkbook.write(outputStream);
outputStream.close();
public void writeHeaderRow(Sheet sheet, List<String> headerNames ) {
//public void writeHeaderRow(SXSSFSheet sxssfSheet, List<String> headerNames ) {
// LinkedHashMap<String,Object> mp = getFieldNames(obj);
// ArrayList<String> colNames = (ArrayList<String>) getColumnNames();
try {
XSSFCellStyle hCellStyle = getHeaderStyle();
SXSSFRow row = (SXSSFRow) sheet.createRow(headerRow);
for (int hCellInd = 0; hCellInd < headerNames.size(); hCellInd++) {
SXSSFCell cell = (SXSSFCell) row.createCell(hCellInd);
cell.setCellStyle(hCellStyle);
cell.setCellValue(headerNames.get(hCellInd));
//sheet.autoSizeColumn(hCellInd);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void writeNonHeaderRow(SXSSFSheet sxssfSheet, List<Object> rowObj, int rowIndex)
{
CreationHelper createHelper = sxssfWorkbook.getCreationHelper();
try {
SXSSFRow row = (SXSSFRow)sheet.createRow(rowIndex);
XSSFCellStyle normalStyle = getNormalStyle();
int count = 0;
for (int rCellInd = 0; rCellInd < rowObj.size(); rCellInd++) {
//Cell cell = row.createCell(rCellInd);
SXSSFCell cell = (SXSSFCell)row.createCell(rCellInd);
cell.setCellStyle(normalStyle);
Object cellData = rowObj.get(rCellInd);
if (cellData != null) {
if (cellData instanceof Double) {
cell.setCellValue((Double) cellData);
if((Double)cellData < 0){
cell.setCellStyle(getNegativeValueStyle());
}else if((Double)cellData == 0){
normalStyle.setDataFormat((short) SXSSFCell.CELL_TYPE_BLANK) ;
cell.setCellStyle(normalStyle);
}else
cell.setCellStyle(normalStyle);
} else {
//normalStyle.setDataFormat((short) HSSFCell.CELL_TYPE_BLANK);
cell.setCellType(SXSSFCell.CELL_TYPE_BLANK);
}
//sxssfSheet.autoSizeColumn();
//sxssfSheet.setColumnWidth(rCellInd, sxssfSheet.getColumnWidth(rCellInd));
//resizeXLSXColumns(sheet);
}
//autoResizeColumns(sxssfSheet);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void resizeXLSXColumns(Sheet sheet ,int rowNum){
SXSSFRow row = (SXSSFRow)sheet.getRow(rowNum);
Iterator<Cell> itr = row.cellIterator();
int max = 0;
while(itr.hasNext()){
Cell cell = itr.next();
int width = sheet.getColumnWidth(cell.getColumnIndex());
if(width > max){
max = width;
}
//sheet.setColumnWidth(cell.getColumnIndex(),max);
}
while(itr.hasNext()){
Cell cell = itr.next();
sheet.setColumnWidth(cell.getColumnIndex(),max);
}
}