使用POI从.xls升级到.xlsx [英] Upgrading from .xls to .xlsx with POI

查看:200
本文介绍了使用POI从.xls升级到.xlsx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我正在尝试使用POI JAR。当我尝试这样做作为一个独立的应用程序它可以正常工作,但是当我尝试将它集成到一个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异常
{
FileOutputStream out = new FileOutputStream(workbook.xlsx);

//创建一个新的工作簿
XSSFWorkbook wb = new XSSFWorkbook();
//创建一个新的工作表
XSSFSheet s = wb.createSheet();
//声明一个行对象引用
XSSFRow r = null;
//声明一个单元对象引用
XSSFCell c = null;

//标题行和列
r = s.createRow(0);
c = r.createCell(0);
c.setCellValue(业务日期);
//c.setCellStyle(cs);
c = r.createCell(1);
c.setCellValue(帐号);

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

有人可以帮忙吗?对不起,任何不好的英语!谢谢。

解决方案

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



尝试使 createExcelWorkbookPosition 返回文件的 XSSFWorkbook

  public static File createExcelWorkbookPosition(String BsnsDate)throws异常{
文件文件=新文件(workbook.xlsx);
FileOutputStream out = new FileOutputStream(file);
// ...
return 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);
}
//如果使用Apache IO,上述代码可以简化为IOUtils.copy(in,out);
//如果使用Guava,Files.copy(file,out);

//不要忘记关闭流并冲洗响应缓冲区


I have a web application In which i have excel file(.xls) download option. Now I Have to provide that feature in .xlsx

I am trying to Use POI Jar. When I try to do that as a stand alone Application it works fine, but when I try to integrate that into a web application, I am getting an error as

Excel Found Unreadable Content in FILENAME.xlsx. do you want to recover the content of this workbook?
If you trust the source of this workbook click yes!

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());

Here's the Java code where I create the spreadsheet:

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;
}

Can anyone please help? Sorry for any bad English! Thanks.

解决方案

I had a quite similar issue, please have a look at Forcing the browser to download a docx file in JAVA generates a corrupted document. The point was to add the Content-Length header of the response.

Try to make createExcelWorkbookPosition returning the file instead of the XSSFWorkbook:

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

Then:

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

这篇关于使用POI从.xls升级到.xlsx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆