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

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

@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

原文地址:https://www.cnblogs.com/sxdcgaq8080/p/7571819.html