gpt4 book ai didi

servlets - 将 poi 工作簿写入输出流会产生奇怪的值

转载 作者:行者123 更新时间:2023-12-04 14:32:02 24 4
gpt4 key购买 nike

我的目标是创建一个 Excel 文件供用户通过 apache poi 下载。

我的 servlet 中有此代码:

protected void doGet(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException
{

// create a workbook , worksheet
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("MySheet");
CreationHelper createHelper = wb.getCreationHelper();

// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow((short)0);
Cell cell = row.createCell(0);
cell.setCellValue(1);
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue( createHelper.createRichTextString("This is a string") );
row.createCell(3).setCellValue(true);

//write workbook to outputstream
ServletOutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();

//offer the user the option of opening or downloading the resulting Excel file
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=MyExcel.xls");

问题是我得到了这些奇怪的值:

`…MySheetŒ®üÿ » ÌÁ w dü©ñÒMbP?*+‚€%ÿÁƒ„¡"d,,à?à?



有什么建议?

最佳答案

发现出了什么问题。 HttpServletResponse 必须在其他任何事情之前先设置

//offer the user the option of opening or downloading the resulting Excel file
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=MyExcel.xls");

关于servlets - 将 poi 工作簿写入输出流会产生奇怪的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11238479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com