从.xls的升级与POI为原来的.xlsx [英] Upgrading from .xls to .xlsx with POI

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

问题描述

我有一个Web应用程序在我所擅长文件(.xls)下载选项。现在我要提供的.xlsx该功能

我试图使用POI罐。当我尝试这样做,因为它工作正常独立的应用程序,但是当我尝试在集成到Web应用程序,我得到一个错误


  

Excel中发现在FILENAME.xlsx无法读取内容。你想恢复此工作簿的内容?结果
  如果您信任该工作簿的来源点击是的!


  XSSFWorkbook W = FileName.createExcelWorkbookPosition(
        的request.getParameter(BSNS_DT));
response.setContentType(
        应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet);
response.setHeader(内容处置,附件;文件名= filename.xlsx);
w.write(response.getOutputStream());

下面是在Java code,我在S preadsheet:

 公共静态XSSFWorkbook createExcelWorkbookPosition(字符串BsnsDate)
    抛出异常
{
    FileOutputStream中出=新的FileOutputStream(workbook.xlsx);    //创建一个新的工作簿
    XSSFWorkbook WB =新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(账号);    尝试{
        wb.write(出);
        out.close();
        的System.out.println(文件所著);
    }赶上(例外五){
        的System.out.println(错误);
        的System.out.println(E);
    }
    返回WB;
}

任何人都可以请帮助?对不起,什么不好的英语!谢谢你。


解决方案

我有一个非常类似的问题,请看看强制下载的docx文件JAVA浏览器将生成一个损坏的文档。问题的关键是增加响应的Content-Length头。

尽量让 createExcelWorkbookPosition 返回文件,而不是 XSSFWorkbook

 公共静态文件createExcelWorkbookPosition(字符串BsnsDate)抛出异常{
    档案文件=新的文件(workbook.xlsx);
    FileOutputStream中出=新的FileOutputStream(文件);
    // ...
    返回文件;
}

然后:

 文件文件= FileName.createExcelWorkbookPosition(的request.getParameter(BSNS_DT));
// ...
response.setContentLength((int)的file.length());在的InputStream =新的FileInputStream(文件);
OutputStream的OUT = response.getOutputStream();
字节[]缓冲区=新的字节[1024];
INT LEN;
而((LEN = in.read(缓冲液))!= - 1){
    out.write(缓冲液,0,LEN);
}
//如果使用Apache IO,上述code可以简化为IOUtils.copy(IN,OUT);
//如果用番石榴,Files.copy(文件,出);//不要忘记关闭流和刷新响应缓冲区

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

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

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