使用POI从.xls升级到.xlsx

crazyStart

我有一个Web应用程序,其中有excel文件(.xls)下载选项。现在,我必须在.xlsx中提供该功能

我正在尝试使用POI罐。当我尝试将其作为独立的应用程序运行时,它可以很好地工作,但是当我尝试将其集成到Web应用程序中时,出现了以下错误:

Excel在FILENAME.xlsx中发现了不可读的内容。您要恢复此工作簿的内容吗?
如果您信任此工作簿的来源,请单击“是”!

XSSFWorkbook w = FileName.createExcelWorkbookPosition(
        request.getParameter("BSNS_DT"));
response.setContentType(
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition","attachment;filename=filename.xlsx");
w.write(response.getOutputStream());

这是我创建电子表格的Java代码:

public static XSSFWorkbook createExcelWorkbookPosition(String BsnsDate)
    throws Exception
{
    FileOutputStream out = new FileOutputStream("workbook.xlsx");

    // create a new workbook
    XSSFWorkbook wb = new XSSFWorkbook();
    // create a new sheet
    XSSFSheet s = wb.createSheet();
    // declare a row object reference
    XSSFRow r = null;
    // declare a cell object reference
    XSSFCell c = null;

    // header row and columns
    r = s.createRow(0);
    c = r.createCell(0);
    c.setCellValue("Business Date");    
    //c.setCellStyle(cs);
    c = r.createCell(1);
    c.setCellValue("Account No");

    try {
        wb.write(out);
        out.close();
        System.out.println("File writed");
    } catch (Exception e) {
        System.out.println("Error");
        System.out.println(e);
    }
    return wb;
}

谁能帮忙吗?对不起,英语不好!谢谢。

sp00m

我有一个非常相似的问题,请看看强制浏览器在JAVA中下载docx文件会生成损坏的文档重点是添加响应的Content-Length标头。

尝试createExcelWorkbookPosition返回文件而不是XSSFWorkbook

public static File createExcelWorkbookPosition(String BsnsDate) throws Exception {  
    File file = new File("workbook.xlsx");
    FileOutputStream out = new FileOutputStream(file);
    // ...  
    return file;
}

然后:

File file = FileName.createExcelWorkbookPosition(request.getParameter("BSNS_DT"));
// ...
response.setContentLength((int) file.length());    

InputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
    out.write(buffer, 0, len);
}
// if using Apache IO, the above code can be simplified to IOUtils.copy(in, out);
// if using Guava, Files.copy(file, out);

// don't forget to close your streams and flush the response buffer

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Apache POI从Java中的xls和xlsx excel文件读取和写入

是否可以使用Apache POI读取.xls和.xlsx文件?

将Apache POI HSSF / XSSF映射到XLS / XLSX

使用Apache Poi读取复杂的xlsx

无法区分隐藏的单元格和其他单元格。使用POI 3.8和xls / xlsx格式

使用Java搜索xlsx和xls文件

使用封闭XML导出.xlsx / .xls

将 apache poi 从 4.0.1 版升级到最新版本(4.1.2 版和 5.0.0 版)后,xls 文件损坏

为 XLSX Apache poi 使用 Java 临时文件

如何使用Apache POI加载大型xlsx文件?

Spring上载到服务器(org.apache.poi)时会损坏Excel(xlsx,xls)文件

使用 UTL_FILE ORACLE PACKAGE 导入 xls、xlsx 文件

使用.NET从Excel(xls / xlsx)创建HTML表

Excel:是什么使.xlsx文件比.xls文件使用更少的空间?

使用Apache POI将单元的背景色从一个XLSX文件错误地复制到另一个

使用Ubuntu 10.04 LTS升级到gcc 4.9

升级到19.10后无法使用5.3.0内核启动

如何使用Homebrew升级到特定的Python版本?

使用TensorFlow将Protobuf利兹升级到警告

如何使用conda升级到Python 3.6?

使用 ZFS 升级到主线内核 5.9(在 20.10 上)

升级到imagemagick7并强制php使用它

升级到Swift 3时使用未声明的类型

升级到Windows 10,现在WAMP无法使用

升级到Ubuntu 16.04后使用高CPU的Eclipse

使用pip升级到numba 0.16会导致错误

如何使用Pip将OpenCV升级到特定版本?

使用自制软件将Jmeter升级到3.2

使用APT从Squeeze升级到Jessie是否安全?