1. 程式人生 > >【POI】導出excel文件,不生成中間文件,直接將內存中的數據創建對象下載到瀏覽器

【POI】導出excel文件,不生成中間文件,直接將內存中的數據創建對象下載到瀏覽器

src sheet xssf xls close creat 下載 position new

不是從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

技術分享

【POI】導出excel文件,不生成中間文件,直接將內存中的數據創建對象下載到瀏覽器