资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringBoot怎么使用POI进行Excel下载

这篇文章主要讲解了“SpringBoot怎么使用POI进行Excel下载”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot怎么使用POI进行Excel下载”吧!

专注于为中小企业提供网站建设、网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业当涂免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

使用poi处理Excel特别方便,此处将处理Excel的代码分享出来。

1.maven引用

org.apache.poi poi 3.17

2.service逻辑代码

/** * 获取下载模版 */ public void salaryTemplate(HttpServletResponse response)throws Exception{ HSSFWorkbook workbook = new HSSFWorkbook(); exportExcel(workbook); response.setHeader("Content-type","application/vnd.ms-excel");  // 解决导出文件名中文乱码 response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition","attachment;filename="+new String("工资模版".getBytes("UTF-8"),"ISO-8859-1")+".xls"); workbook.write(response.getOutputStream()); }  //导入为模版 private void exportExcel(HSSFWorkbook workbook) throws Exception { //创建创建sheet HSSFSheet sheet = workbook.createSheet("工资");  //创建单元格样式 CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);  //设置首行标题标题 HSSFRow headerRow = sheet.createRow(0); headerRow.createCell(0).setCellStyle(cellStyle); headerRow.createCell(0).setCellValue("工号"); headerRow.createCell(1).setCellStyle(cellStyle); headerRow.createCell(1).setCellValue("姓名"); headerRow.createCell(2).setCellStyle(cellStyle); headerRow.createCell(2).setCellValue("年龄");  //创建三行数据 HSSFRow row; for (int i = 0; i <4; i++) { row = sheet.createRow(i + 1); row.createCell(0).setCellStyle(cellStyle); row.createCell(0).setCellValue(i); row.createCell(1).setCellStyle(cellStyle); row.createCell(1).setCellValue("张三"); row.createCell(2).setCellStyle(cellStyle); row.createCell(2).setCellValue(19); } }

3.controller

@GetMapping("/salary/template") public void salaryTemplate(HttpServletResponse response)throws Exception{ salaryService.salaryTemplate(response); }

请求这个接口,下载下来就是Excel文件。写的比较简单,不过看代码基本就能看懂。

感谢各位的阅读,以上就是“SpringBoot怎么使用POI进行Excel下载”的内容了,经过本文的学习后,相信大家对SpringBoot怎么使用POI进行Excel下载这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


网站标题:SpringBoot怎么使用POI进行Excel下载
转载源于:http://cdkjz.cn/article/gpjgod.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220