当前位置:   article > 正文

excel文件输出文件流到前端下载_xssfworkbook返回文件流给前端

xssfworkbook返回文件流给前端

1、使用poi生成excel

 mvn:

org.apache.poipoi4.0.1org.apache.poipoi-ooxml4.0.1

code:

  1. public HSSFWorkbook createMineExcelHk(String firstRowName, List titils, List data) throws Exception {
  2. HSSFWorkbook wb = new HSSFWorkbook();
  3. //建立新的sheet对象(excel的表单)
  4. HSSFSheet sheet = wb.createSheet("excel文件");
  5. //在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
  6. HSSFRow row1 = sheet.createRow(0);
  7. row1.setHeightInPoints(30);
  8. //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
  9. HSSFCell cell = row1.createCell(0);
  10. //设置单元格内容
  11. cell.setCellValue(firstRowName);
  12. HSSFCellStyle cellTitleStyle = wb.createCellStyle();
  13. cellTitleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
  14. cellTitleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
  15. HSSFFont fontStyle = wb.createFont();
  16. fontStyle.setColor(HSSFColor.BLUE.index);
  17. fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  18. fontStyle.setFontHeightInPoints((short) 15);
  19. cellTitleStyle.setFont(fontStyle);
  20. cell.setCellStyle(cellTitleStyle);
  21. //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
  22. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, titils.size() - 1));
  23. sheet.setColumnWidth(0, 100 * 20);
  24. sheet.setColumnWidth(1, 100 * 40);
  25. sheet.setColumnWidth(2, 100 * 40);
  26. ..........
  27. //在sheet里创建第二行(标题栏)
  28. HSSFCellStyle cellTitle2Style = wb.createCellStyle();
  29. cellTitle2Style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
  30. cellTitle2Style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
  31. HSSFFont font2Style = wb.createFont();
  32. font2Style.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  33. font2Style.setFontHeightInPoints((short) 10);
  34. cellTitle2Style.setFont(font2Style);
  35. HSSFRow row2 = sheet.createRow(1);
  36. row2.setHeightInPoints(20);
  37. for (int i = 0; i < titils.size(); i++) {
  38. HSSFCell cellTitle = row2.createCell(i);
  39. cellTitle.setCellStyle(cellTitle2Style);
  40. cellTitle.setCellValue(titils.get(i));
  41. }
  42. HSSFCellStyle rowStyle = wb.createCellStyle();
  43. rowStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
  44. rowStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
  45. for (int i = 0; i < data.size(); i++) {
  46. HSSFRow datarow = sheet.createRow(i + 2);
  47. EsSearchLog esSearchLog = data.get(i);
  48. //设置值
  49. setRow(datarow, 0, rowStyle, i + 1 + "");
  50.   .........
  51. //插图
  52. String strBase64 = data.getAsString("base64");
  53. HSSFClientAnchor anchor;
  54. // datarow.createCell(7).setCellValue(Keys[0]);
  55. // base64转BufferedImage
  56. BufferedImage buffer_Img;
  57. buffer_Img = base64ToBufferedImage(strBase64);
  58. ByteArrayOutputStream byteArrayOut = null;//字符输出对象
  59. byteArrayOut = new ByteArrayOutputStream();
  60. //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
  61. HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
  62. ImageIO.write(buffer_Img, "png", byteArrayOut);// 写入
  63. //图片位置
  64. anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, i + 2, (short) 2, i + 3);
  65. // 插入图片
  66. patriarch.createPicture((HSSFClientAnchor) anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
  67. datarow.setHeight((short) 1400);// 设置行高
  68. //保存至本地目录
  69. String strFilePath = excelConfig.getPath() + fileName;
  70. FileOutputStream output = new FileOutputStream(strFilePath);
  71. wb.write(output);//写入磁盘
  72. output.close();
  73. System.out.println("写excel文件完成:" + System.currentTimeMillis());
  74. excelExportInfoMapper.insert(uuid, strFilePath, sdf.format(new Date()), SecurityUtils.getCurrentUserLogin(), logId);*/
  75. return wb;
  76. }

2、controller

   

  1. @RequestMapping(value = "/excelExport", method = RequestMethod.POST)
  2.     void excelExport(HttpServletResponse response) {
  3.         System.out.println("开始写excel文件:"+System.currentTimeMillis());
  4.         List mineTitils = new ArrayList();
  5.         mineTitils.add("序号");
  6.         mineTitils.add("列名1");
  7.         String fileName = "excelExport.xls";
  8.         try {
  9.             HSSFWorkbook wb= createMineExcelHk("excel导出",mineTitils,data);
  10.             response.reset();
  11.             OutputStream out = response.getOutputStream();
  12.             response.setContentType("application/vnd.ms-excel");
  13.             response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  14.             response.setCharacterEncoding("UTF-8");
  15.             response.setHeader("Pragma", "No-cache");
  16.             wb.write(out);
  17.             out.flush();
  18.             out.close();
  19.             System.out.println("完成excel文件:"+System.currentTimeMillis());
  20.               /*  String strFilePath = "D://" + fileName;
  21.             FileOutputStream output = new FileOutputStream(strFilePath);
  22.             wb.write(output);//写入磁盘*/
  23.         } catch (Exception e) {
  24.             e.printStackTrace();
  25.         }
  26.         }

3、前端代码

  1. axios({
  2.           headers: {
  3.             // 'Content-Type': 'application/vnd.ms-excel',
  4.             'authorization': 'Bearer ' + getToken()
  5.           },
  6.           responseType: 'blob',
  7.           method: 'post',
  8.           url: '/excelExport',
  9.           data: this.formData
  10.         }).then(res => {
  11.           this.loading = false;
  12.           console.log(res.data)
  13.           const objectURL = URL.createObjectURL(new Blob([res.data], {
  14.             type: 'application/ms-excel'
  15.           })) // chrome不受文件你大小限制导出文件
  16.           let a = document.createElement("a");
  17.           a.download = "excel导出.xls";
  18.           a.href = objectURL
  19.           a.click();
  20.         }).catch(e => {
  21.           this.loading = false;
  22.           this.$message.error('导出失败');
  23.         })

4、nginx默认请求大小需要调整

 client_max_body_size 50m;
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/842541
推荐阅读
相关标签
  

闽ICP备14008679号