gpt4 book ai didi

excel - Grails:如何在客户端中发送封闭的工作簿?

转载 作者:行者123 更新时间:2023-12-02 15:54:37 25 4
gpt4 key购买 nike

目前,我有一个可以将excel文件发送到客户端的工作代码。唯一的问题是,客户端一旦收到文件,便会自动打开该文件。我在工作簿中使用org.apache。 Ans,这是我的代码。

 private void generateErrorReport(ServletResponse response, Map message, MultipartFile file, String ext){
InputStream is = file.getInputStream();
OutputStream os = response.outputStream;

int index = file.originalFilename.lastIndexOf(".");
String fileName = file.originalFilename.substring(0,index) + "_error." + ext

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "Attachment;Filename=${fileName}");
try {

PoiTransformer transformer = PoiTransformer.createTransformer(is, os);
org.apache.poi.ss.usermodel.Workbook workbook = transformer.getWorkbook()
Sheet sheet = workbook.getSheetAt(workbook.getActiveSheetIndex())
int lastColNum = sheet.getRow(0).getLastCellNum()

Cell cell;

cell = sheet.getRow(0).getCell(lastColNum);
if(cell==null){
cell = sheet.getRow(0).createCell(lastColNum);
}
cell.setCellType(1)
cell.setCellValue("Message")
cell.setCellStyle(getStyle(workbook, 2))

for(int it=1; it<sheet.getLastRowNum(); it++) {
if (message.get(new Long(it))!=null && message.get(new Long(it))!=[]) {
cell = sheet.getRow(it).getCell(lastColNum);
if(cell==null){
cell = sheet.getRow(it).createCell(lastColNum);
}
cell.setCellType(1)
cell.setCellValue(message.get(new Long(it)).join(', '))
cell.setCellStyle(getStyle(workbook, 1))
}
}

sheet.autoSizeColumn(lastColNum);
transformer.write();
} catch (IOException ex) {
println ex;
} finally {
closeStream(is);
closeStream(os);
}

}

当用户收到Excel文件时,如何阻止它自动打开?

最佳答案

根据此RFC(http://tools.ietf.org/html/rfc6266#section-4.1),浏览器应打开“保存文件”对话框,并让用户选择将文件保存在何处。

我的建议是打开开发工具(F12,Cmd Alt I)切换到网络,并查看请求excel文件时将哪些 header 发送到客户端。

您可以考虑的其他事项:

  • 将mime类型(response.setContentType)设置为application/vnd.ms-excel
  • 设置mime类型application/octet-stream。这样,您就告诉浏览器数据是二进制的,仅此而已。
  • 关于excel - Grails:如何在客户端中发送封闭的工作簿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34986319/

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