Java 从 Spring 返回 Excel 可下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18648823/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:50:09  来源:igfitidea点击:

Return Excel downloadable file from Spring

javaspringexcelspring-mvc

提问by praks5432

So I have a Spring controller, and I'd like to create an Excel file and return it so that it is downloaded by the browser.

所以我有一个 Spring 控制器,我想创建一个 Excel 文件并返回它,以便浏览器下载它。

I'm using JEXcelApi.

我正在使用 JEXcelApi。

This is my controller code

这是我的控制器代码

@RequestMapping(value="/excel/cols/{colString}/rows/{rowString}/", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> exportExcel(HttpServletResponse response,
    @PathVariable List<String> colString,
    @PathVariable List<String> rowString) throws JSONException, IOException, WriteException {
    WritableWorkbook workbook = Workbook.createWorkbook(new File("exported.xls"));
    WritableSheet sheet = workbook.createSheet("Exported",0);
    String[] cols = colString.get(0).split(",");
    String[] rows = rowString.get(0).split(","); 
    for(int i = 0; i < cols.length;i++){
        Label label = new Label(i,0, cols[i]);
        sheet.addCell(label);
    }
    int excelCol = 0;
    int excelRow = 1;
    for(int i = 0; i < rows.length;i++){
        Label label = new Label(excelCol,excelRow, rows[i]);
        sheet.addCell(label);
        excelCol++;
        if((i+1) % cols.length == 0){
            excelCol = 0;
            excelRow++;
        }
    }
    workbook.write();
    workbook.close();
    return null;
}

How do I do that? I suspect there's some content header I can set. I know one method is to use Spring's Abstract Excel view class, but is there a simpler method?

我怎么做?我怀疑我可以设置一些内容标题。我知道一种方法是使用 Spring 的 Abstract Excel 视图类,但是有更简单的方法吗?

采纳答案by Sotirios Delimanolis

You need to set the Content-Dispositionheader.

您需要设置Content-Disposition标题。

response.setHeader("Content-disposition","attachment; filename=" + yourFileName);

and write your bytes directly to the response OutputStream.

并将您的字节直接写入响应OutputStream

File xls = new File("exported.xls"); // or whatever your file is
FileInputStream in = new FileInputStream(xls);
OutputStream out = response.getOutputStream();

byte[] buffer= new byte[8192]; // use bigger if you want
int length = 0;

while ((length = in.read(buffer)) > 0){
     out.write(buffer, 0, length);
}
in.close();
out.close();


The above is relatively old. You can construct a ResponseEntitywith FileSystemResourcenow. A ResourceHttpMessageConverterwill then copy the bytes, as I have suggested above, for you. Spring MVC makes it simpler for you rather than having you interact with interfaces from the Servlet specification.

上面的比较老。您可以构建一个ResponseEntityFileSystemResource现在。ResourceHttpMessageConverter然后A将复制字节,正如我上面建议的那样,为您。Spring MVC 使您更简单,而不是让您与来自 Servlet 规范的接口进行交互。