1、使用poi生成excel
mvn:
复制代码
1org.apache.poipoi4.0.1org.apache.poipoi-ooxml4.0.1
code:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78public HSSFWorkbook createMineExcelHk(String firstRowName, List titils, List data) throws Exception { HSSFWorkbook wb = new HSSFWorkbook(); //建立新的sheet对象(excel的表单) HSSFSheet sheet = wb.createSheet("excel文件"); //在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个 HSSFRow row1 = sheet.createRow(0); row1.setHeightInPoints(30); //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个 HSSFCell cell = row1.createCell(0); //设置单元格内容 cell.setCellValue(firstRowName); HSSFCellStyle cellTitleStyle = wb.createCellStyle(); cellTitleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 cellTitleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 HSSFFont fontStyle = wb.createFont(); fontStyle.setColor(HSSFColor.BLUE.index); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); fontStyle.setFontHeightInPoints((short) 15); cellTitleStyle.setFont(fontStyle); cell.setCellStyle(cellTitleStyle); //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, titils.size() - 1)); sheet.setColumnWidth(0, 100 * 20); sheet.setColumnWidth(1, 100 * 40); sheet.setColumnWidth(2, 100 * 40); .......... //在sheet里创建第二行(标题栏) HSSFCellStyle cellTitle2Style = wb.createCellStyle(); cellTitle2Style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 cellTitle2Style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 HSSFFont font2Style = wb.createFont(); font2Style.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font2Style.setFontHeightInPoints((short) 10); cellTitle2Style.setFont(font2Style); HSSFRow row2 = sheet.createRow(1); row2.setHeightInPoints(20); for (int i = 0; i < titils.size(); i++) { HSSFCell cellTitle = row2.createCell(i); cellTitle.setCellStyle(cellTitle2Style); cellTitle.setCellValue(titils.get(i)); } HSSFCellStyle rowStyle = wb.createCellStyle(); rowStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 rowStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 for (int i = 0; i < data.size(); i++) { HSSFRow datarow = sheet.createRow(i + 2); EsSearchLog esSearchLog = data.get(i); //设置值 setRow(datarow, 0, rowStyle, i + 1 + ""); ......... //插图 String strBase64 = data.getAsString("base64"); HSSFClientAnchor anchor; // datarow.createCell(7).setCellValue(Keys[0]); // base64转BufferedImage BufferedImage buffer_Img; buffer_Img = base64ToBufferedImage(strBase64); ByteArrayOutputStream byteArrayOut = null;//字符输出对象 byteArrayOut = new ByteArrayOutputStream(); //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点) HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); ImageIO.write(buffer_Img, "png", byteArrayOut);// 写入 //图片位置 anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, i + 2, (short) 2, i + 3); // 插入图片 patriarch.createPicture((HSSFClientAnchor) anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); datarow.setHeight((short) 1400);// 设置行高 //保存至本地目录 String strFilePath = excelConfig.getPath() + fileName; FileOutputStream output = new FileOutputStream(strFilePath); wb.write(output);//写入磁盘 output.close(); System.out.println("写excel文件完成:" + System.currentTimeMillis()); excelExportInfoMapper.insert(uuid, strFilePath, sdf.format(new Date()), SecurityUtils.getCurrentUserLogin(), logId);*/ return wb; }
2、controller
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26@RequestMapping(value = "/excelExport", method = RequestMethod.POST) void excelExport(HttpServletResponse response) { System.out.println("开始写excel文件:"+System.currentTimeMillis()); List mineTitils = new ArrayList(); mineTitils.add("序号"); mineTitils.add("列名1"); String fileName = "excelExport.xls"; try { HSSFWorkbook wb= createMineExcelHk("excel导出",mineTitils,data); response.reset(); OutputStream out = response.getOutputStream(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); response.setCharacterEncoding("UTF-8"); response.setHeader("Pragma", "No-cache"); wb.write(out); out.flush(); out.close(); System.out.println("完成excel文件:"+System.currentTimeMillis()); /* String strFilePath = "D://" + fileName; FileOutputStream output = new FileOutputStream(strFilePath); wb.write(output);//写入磁盘*/ } catch (Exception e) { e.printStackTrace(); } }
3、前端代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23axios({ headers: { // 'Content-Type': 'application/vnd.ms-excel', 'authorization': 'Bearer ' + getToken() }, responseType: 'blob', method: 'post', url: '/excelExport', data: this.formData }).then(res => { this.loading = false; console.log(res.data) const objectURL = URL.createObjectURL(new Blob([res.data], { type: 'application/ms-excel' })) // chrome不受文件你大小限制导出文件 let a = document.createElement("a"); a.download = "excel导出.xls"; a.href = objectURL a.click(); }).catch(e => { this.loading = false; this.$message.error('导出失败'); })
4、nginx默认请求大小需要调整
复制代码
1client_max_body_size 50m;
最后
以上就是称心眼神最近收集整理的关于excel文件输出文件流到前端下载的全部内容,更多相关excel文件输出文件流到前端下载内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复