gpt4 book ai didi

java - Apache POI 生成损坏的 Excel 文件

转载 作者:行者123 更新时间:2023-12-02 22:26:37 29 4
gpt4 key购买 nike

我正在使用 Apache POI 在数据库的帮助下创建 Excel 文件。我尝试使用不同的代码进行大量搜索,但我得到的只是一遍又一遍损坏的 Excel 表。截至目前,这是我正在使用的代码:

    public void  generateExcel(ResultSet rs, String excelFilename, String newDesc){

try {
Workbook wb = new XSSFWorkbook();
Cell c = null;
//Cell style for header row
CellStyle cs = wb.createCellStyle();
cs.setFillForegroundColor(IndexedColors.LIME.getIndex());
cs.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
Font f = wb.createFont();
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
f.setFontHeightInPoints((short) 12);
cs.setFont(f);

//New Sheet
Sheet sheet1 = null;
sheet1 = wb.createSheet(newDesc);


ResultSetMetaData metaData = rs.getMetaData();
int colCount = metaData.getColumnCount();

//Create Hash Map of Field Definitions
LinkedHashMap<Integer, MyTableInfo> hashMap = new LinkedHashMap<Integer, MyTableInfo>(colCount);

for (int i = 0; i < colCount; i++) {
MyTableInfo db2TableInfo = new MyTableInfo();
db2TableInfo.setFieldName(metaData.getColumnName(i + 1).trim());
db2TableInfo.setFieldText(metaData.getColumnLabel(i + 1));
db2TableInfo.setFieldSize(metaData.getPrecision(i + 1));
db2TableInfo.setFieldDecimal(metaData.getScale(i + 1));
db2TableInfo.setFieldType(metaData.getColumnType(i + 1));
db2TableInfo.setCellStyle(getCellAttributes(wb, c, db2TableInfo));
hashMap.put(i, db2TableInfo);
}

// Row and column indexes
int idx = 0;
int idy = 0;

// Generate column headings
Row row = sheet1.createRow(idx);
MyTableInfo db2TableInfo = new MyTableInfo();

Iterator<Integer> iterator = hashMap.keySet().iterator();
while (iterator.hasNext()) {
Integer key = (Integer) iterator.next();
db2TableInfo = hashMap.get(key);
c = row.createCell(idy);
c.setCellValue(db2TableInfo.getFieldText());
c.setCellStyle(cs);
if(db2TableInfo.getFieldSize() > db2TableInfo.getFieldText().trim().length()){
sheet1.setColumnWidth(idy, (db2TableInfo.getFieldSize()* 500));
}
else {
sheet1.setColumnWidth(idy, (db2TableInfo.getFieldText().trim().length() * 500));
}
idy++;
}

while (rs.next()) {

idx++;
row = sheet1.createRow(idx);
System.out.println(idx);
for (int i = 0; i < colCount; i++) {

c = row.createCell(i);
db2TableInfo = hashMap.get(i);

switch (db2TableInfo.getFieldType()) {
case 1:
c.setCellValue(rs.getString(i+1));
break;
case 2:
c.setCellValue(rs.getDouble(i+1));
break;
case 3:
c.setCellValue(rs.getDouble(i+1));
break;
default:
c.setCellValue(rs.getString(i+1));
break;
}
c.setCellStyle(db2TableInfo.getCellStyle());
}

}

rs.close();


FileOutputStream fileOut = new FileOutputStream(excelFilename);

wb.write(fileOut);
fileOut.close();

}
catch (Exception e) {
System.out.println(e);
}

}


private static CellStyle getCellAttributes (Workbook wb, Cell c, MyTableInfo db2TableInfo){

CellStyle cs= wb.createCellStyle();
DataFormat df = wb.createDataFormat();
Font f = wb.createFont();

switch (db2TableInfo.getFieldDecimal()) {
case 1:
cs.setDataFormat(df.getFormat("#,##0.0"));
break;
case 2:
cs.setDataFormat(df.getFormat("#,##0.00"));
break;
case 3:
cs.setDataFormat(df.getFormat("#,##0.000"));
break;
case 4:
cs.setDataFormat(df.getFormat("#,##0.0000"));
break;
case 5:
cs.setDataFormat(df.getFormat("#,##0.00000"));
break;
default:
break;
}

cs.setFont(f);

return cs;

}

我收到以下消息:

"Excel cannot open the file "filename" because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

我之前曾使用 JXL 生成 excel 文件,但最近它也开始给我同样的结果,因此我选择切换到 Apache POI。

提前致谢。

最佳答案

您的 HttpServletResponse 设置正确吗?您使用什么文件扩展名?

试试这个:

ServletOutputStream os = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"example.xls\"");

workbook = getWorkbook(....)

workbook.write(os);
workbook.close();
os.flush();

response.flushBuffer();

其中“response”是来自 Controller 方法的 HttpServletResponse

关于java - Apache POI 生成损坏的 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52053861/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com