且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

【POI】导出excel文件,不生成中间文件,直接将内存中的数据创建对象下载到浏览器

更新时间:2022-05-16 18:19:44

 不是从InputStream中read,然后outputStream再write

【POI】导出excel文件,不生成中间文件,直接将内存中的数据创建对象下载到浏览器【POI】导出excel文件,不生成中间文件,直接将内存中的数据创建对象下载到浏览器
@RequestMapping("download4Excel")
    public void download4Excel(HttpServletResponse response){
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("测试Sheet");
        
        sheet.setColumnWidth(1, 7000);
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(1);
        cell.setCellValue("德玛西亚");
        
        
        try {
            
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("测试生成Excel文件.xlsx", "utf-8"));
            OutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
            
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        
    }
View Code

 

【POI】导出excel文件,不生成中间文件,直接将内存中的数据创建对象下载到浏览器