springboot 使用poi导出excel文件,及注意项
2019-03-27 10:52
751 查看
springboot 使用poi导出excel文件,及注意项
1.maven依赖
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
2.excel 不同扩展名导出
// .xlsx 后缀 分别使用这几个类 org.apache.poi.xssf.usermodel.XSSFWorkbook org.apache.poi.xssf.usermodel.XSSFSheet org.apache.poi.xssf.usermodel.XSSFRow org.apache.poi.xssf.usermodel.XSSFCellStyle //.xls 使用这几个类 org.apache.poi.xssf.usermodel.HSSFWorkbook org.apache.poi.xssf.usermodel.HSSFSheet org.apache.poi.xssf.usermodel.HSSFRow org.apache.poi.xssf.usermodel.HSSFCellStyle
注意:下载文件后,出现无法打开可能是使用类和后缀名对应有误!
3…xlsx 后缀为例 导出文件
public void download(HttpServletResponse response, List<ExportVo> list, String excleName) throws IOException { Map<String, String> map = new HashMap<>(); //记录行数 AtomicInteger num = new AtomicInteger(); try (XSSFWorkbook workbook = new XSSFWorkbook()) { //创建一个Excel表单,参数为sheet的名字 XSSFSheet sheet = workbook.createSheet("Sheet1"); //设置数据 for (ExportVo e : list) { if (!map.containsKey(e.getTeamId())) { //清空数据,避免干扰 map.clear(); //仅仅做记录 map.put(e.getTeamId(), "add"); //创建表头 setTitle(workbook, sheet, num, e); } setData(sheet, num, e); } setResponse(excleName, response, workbook); } } /** * 封装响应 * * @param excleName * @param response * @param workbook * @throws IOException */ private void setResponse(String excleName, HttpServletResponse response, XSSFWorkbook workbook) throws IOException { //清空response response.reset(); //设置response的Header response.addHeader("Content-Disposition", "attachment;filename=" + excleName); OutputStream os = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/vnd.ms-excel;charset=gb2312"); //将excel写入到输出流中 workbook.write(os); os.flush(); os.close(); } private void setData(XSSFSheet sheet, AtomicInteger num, ExportVo e) { XSSFRow row = sheet.createRow(num.getAndAdd(1)); row.createCell(0).setCellValue(e.getName()); row.createCell(1).setCellValue(e.getSchool()); row.createCell(2).setCellValue(e.getMajor()); row.createCell(3).setCellValue(e.getNumber()); row.createCell(4).setCellValue(e.getStart()); row.createCell(5).setCellValue(e.getTel()); row.createCell(6).setCellValue(e.getMail()); } private void setTitle(XSSFWorkbook workbook, XSSFSheet sheet, AtomicInteger num, ExportVo e) { //设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度 sheet.setColumnWidth(0, 10 * 256); sheet.setColumnWidth(1, 20 * 256); sheet.setColumnWidth(2, 20 * 256); sheet.setColumnWidth(3, 15 * 256); sheet.setColumnWidth(4, 10 * 256); sheet.setColumnWidth(5, 15 * 256); sheet.setColumnWidth(6, 23 * 256); //设置为居中加粗 XSSFCellStyle style = workbook.createCellStyle(); XSSFFont font = workbook.createFont(); font.setBold(true); style.setFont(font); HSSFCellStyle //创建一行 XSSFRow row = sheet.createRow(num.getAndAdd(1)); //设置队伍属名 XSSFCell cell; cell = row.createCell(0); cell.setCellValue("队伍名称"); cell.setCellStyle(style); //设置队伍值 cell = row.createCell(1); cell.setCellValue(e.getTeam()); cell.setCellStyle(style); //创建一行 XSSFRow row1 = sheet.createRow(num.getAndAdd(1)); //设置队长属性名 cell = row1.createCell(0); cell.setCellValue("队长"); cell.setCellStyle(style); //设置队长名 cell = row1.createCell(1); cell.setCellValue(e.getName()); cell.setCellStyle(style); //创建一行 添加成员属性, XSSFRow row2 = sheet.createRow(num.getAndAdd(1)); cell = row2.createCell(0); cell.setCellValue("姓名"); cell.setCellStyle(style); cell = row2.createCell(1); cell.setCellValue("学校"); cell.setCellStyle(style); cell = row2.createCell(2); cell.setCellValue("专业"); cell.setCellStyle(style); cell = row2.createCell(3); cell.setCellValue("学号"); cell.setCellStyle(style); cell = row2.createCell(4); cell.setCellValue("入学年份"); cell.setCellStyle(style); cell = row2.createCell(5); cell.setCellValue("电话"); cell.setCellStyle(style); cell = row2.createCell(6); cell.setCellValue("邮箱地址"); cell.setCellStyle(style); }
注意点:包含中文名时编码处理,否则会乱码等问题,
导出效果图:
队伍名称 | xx队 | ||||||
---|---|---|---|---|---|---|---|
队长 | 张三 | ||||||
姓名 | 学校 | 专业 | 学号 | 入学年份 | 电话 | 邮箱地址 | |
张三 | xx | xx | xx | xx | xx | xx | |
队伍名称 | ZZ队 | ||||||
队长 | 李四 | ||||||
姓名 | 学校 | 专业 | 学号 | 入学年份 | 电话 | 邮箱地址 | |
李四 | yy | yy | yy | yy | yy | yy | |
… | |||||||
相关文章推荐
- SpringBoot+Mybatis+MySQL+Vue+ElementUI,用POI导出和导入Excel文件案例
- Spring Boot--POI导出excel文件下载
- SpringMVC/SpringBoot使用easypoi实现Excel文件导入导出功能实现
- SpringBoot中使用POI,快速实现Excel导入导出
- SpringBoot中使用POI,快速实现Excel导入导出
- SpringBoot中使用POI,快速实现Excel导入导出
- springboot中使用java操作poi案例(excel数据写入与导出)
- Springboot 之 使用POI导出Excel文件
- 使用poi导入导出excel文件方法使用及改进
- Spring使用POI实现Excel导入导出
- Springboot/SpringMVC+POI 实现Excel导出功能(点击下载方式实现)
- 使用poi将数据库的数据导出为Excel文件.
- Spring使用POI实现Excel导入导出
- SpringBoot 集成poi文件导出
- web中使用POI导入导出EXCEL文件的例子
- java 利用 poi 生成 Excel文件与spring使用文件流形式下载文件
- 【JavaWeb开发】使用java实现简单的Excel文件的导入与导出(POI)
- web中使用POI导入导出EXCEL文件的例子
- Springboot 之 POI导出Word文件
- web中使用POI导入导出EXCEL文件的例子