gpt4 book ai didi

java - 将 XSSFWorkbook excel 文件写入响应流

转载 作者:行者123 更新时间:2023-11-30 02:09:23 29 4
gpt4 key购买 nike

我有这个网页单击按钮时,它会读取数据并将文件保存到位置 xyz。

我不想要这样。我想通过 chrome 浏览器下载它。

如何发送下面的回复?

给出错误的行是response.getOutputStream().write(outputStream);

谢谢

public void exportCOAExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {

//works well. save excel file to desktop
XSSFWorkbook workbook = new XSSFWorkbook ();
FileOutputStream outputStream = new FileOutputStream("C:\\Users\\user\\Desktop\\revenue.xls");
workbook.write(outputStream);
outputStream.flush();
outputStream.close();

//trying to get browser to download the file
response.setContentType("text/xls");
response.setHeader("Content-disposition", "attachment;filename=" + "a.xls");

/************problematic line************/
response.getOutputStream().write(outputStream);

response.getOutputStream().flush();

}

最佳答案

您可以将工作簿直接写入响应的输出流

XSSFWorkbook workbook = new XSSFWorkbook();
response.setContentType("text/xls");
response.setHeader("Content-disposition", "attachment;filename=" + "a.xls");

try (OutputStream outputStream = response.getOutputStream()) {
workbook.write(outputStream);
}

关于java - 将 XSSFWorkbook excel 文件写入响应流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50553744/

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