2013-03-13 59 views
4

我在我的Java应用程序中遇到问题以启用下载XLSX文件。Apache POI - 使用XSSFWorkbok + servlet响应

以下示例显示在此链接中:Create an excel file for users to download using Apache POI,我尝试了两种配置下载/保存电子表格。

先用.xls文件:

response.setContentType("application/ms-excel"); 
response.setHeader("Content-Disposition", "attachment; filename=testxls.xls"); 

HSSFWorkbook wb = new HSSFWorkbook(); 
HSSFSheet sheet = wb.createSheet(); 
HSSFRow row = sheet.createRow(0); 
HSSFCell cell = row.createCell(0); 
cell.setCellValue("Some text"); 

ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(); 
wb.write(outByteStream); 

byte[] outArray = outByteStream.toByteArray(); 
OutputStream outStream = response.getOutputStream(); 
outStream.write(outArray); 
outStream.flush(); 

这工作。

然后我试着用XLSX文件:

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
response.setHeader("Content-Disposition", "attachment; filename=testxls.xlsx"); 

XSSFWorkbook wb = new XSSFWorkbook(); 
XSSFSheet sheet = wb.createSheet(); 
XSSFRow row = sheet.createRow(0); 
XSSFCell cell = row.createCell(0); 
cell.setCellValue("Some text"); 

ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(); 
wb.write(outByteStream); 

byte[] outArray = outByteStream.toByteArray(); 
OutputStream outStream = response.getOutputStream(); 
outStream.write(outArray); 
outStream.flush(); 

当我尝试这个,我收到消息:“Excel中发现,在‘testxls.xlsx’不可读的内容你要恢复的内容此工作簿?...

尽管出现此消息,电子表格会正常打开,但我真的想要删除此消息。

任何想法?

+0

什么类型是'outByteStream'? – 2013-03-13 14:10:38

+1

为什么你需要一个'ByteArrayOutputStream'?你不能简单地用'wb.write(response.getOutputStream())'来代替吗? – 2013-03-13 14:17:11

+0

我尝试过使用'org.apache.commons.io.output.ByteArrayOutputStream'和 'java.io.ByteArrayOutputStream' – 2013-03-13 14:26:27

回答

-1

使用此JSP代码并成功生成excel文件。我通过数据库为excel文件提供了输入,您还可以提供手动输入。

<%HSSFWorkbook wb = new HSSFWorkbook(); 
    HSSFSheet sheet = wb.createSheet(); 
     try { 
     java.sql.Connection con; 
     Class.forName("com.mysql.jdbc.Driver"); 
     con = DriverManager.getConnection("jdbc:mysql://localhost:3306/custinfo","root","abc"); 
      Statement st= con.createStatement(); 
      out.println("hello world"); 
     ResultSet rs=st.executeQuery("select name ,state ,balance,description from customerdata where customerid='"+Id+"'"); 

     HSSFRow row = sheet.createRow((short)0); 
     row.createCell((short)0).setCellValue("NAME"); 
     row.createCell((short)1).setCellValue("STATE"); 
     row.createCell((short)2).setCellValue("BALANCE"); 
     row.createCell((short)3).setCellValue("DESCRIPTION"); 
     while(rs.next()) 
     { 
      out.println("hello world data");  
      HSSFRow row1 = sheet.createRow((short)i); 
      row1.createCell((short)0).setCellValue(rs.getString("name")); 
      row1.createCell((short)1).setCellValue(rs.getString("state")); 
     row1.createCell((short)2).setCellValue(rs.getString(3)); 
     row1.createCell((short)3).setCellValue(rs.getString(4)); 
     i=i+1; 
     sheet.autoSizeColumn((short)1); 

     } 

     } 
     catch(SQLException e) { 
     out.println("SQLException caught: " +e.getMessage()); 
     }%> 
    // create a small spreadsheet 
    <% 

    %> 
    <% 

    ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(); 
    wb.write(outByteStream); 
    byte [] outArray = outByteStream.toByteArray(); 
    response.setContentType("application/ms-excel"); 
    response.setContentLength(outArray.length); 
    response.setHeader("Expires:", "0"); // eliminates browser caching 
    response.setHeader("Content-Disposition", "attachment; filename=testxls.xls"); 
    OutputStream outStream = response.getOutputStream(); 
    outStream.write(outArray); 
    outStream.flush(); 

    %> 
+0

但是,此示例仍然是HSSF(即.xls),而不是OP需要的.xlsx的XSSF。你能突出这里有什么不同吗?你认为什么改变了它们? – Rup 2014-04-28 11:52:35

+1

基于[这个其他答案](http://stackoverflow.com/a/23340734/243245)它看起来像重要的一点是设置内容的长度。 – Rup 2014-04-28 13:21:22