java Apache POI - 使用 XSSFWorkbok + servlet 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15387243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache POI - Working with XSSFWorkbok + servlet response
提问by Lucas_Mux
I'm having problems in my java application to enable downloading XLSX files.
我的 Java 应用程序无法下载 XLSX 文件时遇到问题。
following the example displayed in this link: Create an excel file for users to download using Apache POI, I tried two configurations to download/save a spreadsheet.
按照此链接中显示的示例:使用 Apache POI 创建供用户下载的 excel 文件,我尝试了两种配置来下载/保存电子表格。
First with a .XLS file:
首先使用 .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();
This works.
这有效。
Then i tried with a XLSX file:
然后我尝试使用 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();
When i try this, i receive the message: "Excel found unreadable content in 'testxls.xlsx'. Do you want to recover the contents of this workbook? ...."
当我尝试此操作时,我收到消息:“ Excel 在 'testxls.xlsx' 中发现无法读取的内容。您想恢复该工作簿的内容吗?....”
Despite this message, the spreadsheet opens normally, but i really want to remove this message.
尽管有这条消息,电子表格正常打开,但我真的想删除这条消息。
Any ideas?
有任何想法吗?
回答by amy
Use this JSP code and generate the excel file succesfully.I have given input to excel file through the database you can also give manual inputs.
使用这个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();
%>