Apache POI -Java Excel(补充)实现下载excel

在Java Excel系列文章(二)中心先生成电子表格,然后提供下载功能返回页面如下:

先在(二)中将输出流转化为字节数组,返回至controller层

byte[] byteArray = out.toByteArray();
            return byteArray;```

@ApiOperation(value="导出箱号模板", notes="导出箱号模板")
@RequestMapping(value="/exportExcelTemplate",method = RequestMethod.GET)
public void exportExcelTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException{
    String fileName = new String("箱号信息模板.xls".getBytes("UTF-8"),"iso-8859-1");
    byte[] templateBytes = containerNumService.exportExcelTemplate();
    response.setContentType("application/x-msdownload");
    response.setContentLength(templateBytes.length);
    response.setHeader("Content-Disposition", "attachment;filename="+fileName);//设置下载的文件名
    response.getOutputStream().write(templateBytes);
}

你可能感兴趣的:(Apache POI -Java Excel(补充)实现下载excel)