java 导出多个Sheet的Excel文件
1 package com.forestar.patrol.service; 2 3 import org.apache.poi.hssf.usermodel.*; 4 import org.apache.poi.ss.usermodel.HorizontalAlignment; 5 import org.springframework.stereotype.Service; 6 7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 import java.io.OutputStream; 10 import java.nio.charset.StandardCharsets; 11 import java.time.LocalTime; 12 import java.util.ArrayList; 13 import java.util.HashMap; 14 import java.util.List; 15 import java.util.Map; 16 17 @Service 18 public class ExcelService { 19 20 public void exportExchange(HttpServletResponse response) { 21 22 //全部数据 23 List<List<Map<String,Object>>> list = new ArrayList<>(); 24 25 //第一个sheet的数据 26 List<Map<String,Object>> maps1 = new ArrayList<>(); 27 Map<String,Object> map1 = new HashMap<>(); 28 map1.put("A", 11001); 29 map1.put("B", 22001); 30 map1.put("C", 33001); 31 maps1.add(map1); 32 Map<String,Object> map2 = new HashMap<>(); 33 map2.put("A", 11002); 34 map2.put("B", 22002); 35 map2.put("C", 33002); 36 maps1.add(map2); 37 38 //第二个sheet的数据 39 List<Map<String,Object>> maps2 = new ArrayList<>(); 40 Map<String,Object> map11 = new HashMap<>(); 41 map11.put("A", 111001); 42 map11.put("B", 222001); 43 map11.put("C", 333001); 44 maps2.add(map11); 45 Map<String,Object> map22 = new HashMap<>(); 46 map22.put("A", 111002); 47 map22.put("B", 222002); 48 map22.put("C", 333002); 49 maps2.add(map22); 50 51 list.add(maps1); 52 list.add(maps2); 53 54 //列信息 55 String[] tableHeader = {"col-1","col-2","col-3"}; 56 HSSFWorkbook workbook = new HSSFWorkbook(); //创建一个Excel 57 HSSFCellStyle style = workbook.createCellStyle(); //设置表头的类型 58 style.setAlignment(HorizontalAlignment.CENTER); 59 HSSFCellStyle style1 = workbook.createCellStyle(); //设置数据类型 60 style1.setAlignment(HorizontalAlignment.CENTER); 61 HSSFFont font = workbook.createFont(); //设置字体 62 63 for (int j = 0; j < list.size(); j++) { 64 List<Map<String, Object>> maps = list.get(j); 65 66 HSSFSheet sheet = workbook.createSheet("sheet"+(j+1)); //创建一个sheet 67 HSSFHeader header = sheet.getHeader();//设置sheet的头 68 try { 69 //根据是否取出数据,设置header信息 70 if (maps.size() == 0) { 71 header.setCenter("无数据"); 72 } else { 73 header.setCenter("导出信息"); 74 HSSFRow row = sheet.createRow(0); 75 row.setHeight((short) 400); 76 HSSFCell cell = null; 77 //表头 78 for (int k = 0; k < tableHeader.length; k++) { 79 cell = row.createCell((short) k);//创建第0行第k列 80 cell.setCellValue(tableHeader[k]);//设置第0行第k列的值 81 sheet.setColumnWidth((short) k, (short) 8000);//设置列的宽度 82 font.setColor(HSSFFont.COLOR_NORMAL); // 设置单元格字体的颜色. 83 font.setFontHeight((short) 350); //设置单元字体高度 84 style1.setFont(font);//设置字体风格 85 cell.setCellStyle(style1); 86 } 87 // 给Excel填充数据 88 for (int i = 0; i < maps.size(); i++) { 89 //获取InternationalStudent对象 90 Map<String, Object> map = maps.get(i); 91 row = sheet.createRow((short) (i + 1));//创建第i+1行 92 row.setHeight((short) 400);//设置行高 93 94 if (map.get("A") != null) { 95 cell = row.createCell((short) 0);//创建第i+1行第0列 96 cell.setCellValue(map.get("A").toString());//设置第i+1行第0列的值 97 cell.setCellStyle(style);//设置风格 98 } 99 if (map.get("B") != null) { 100 cell = row.createCell((short) 1); //创建第i+1行第1列 101 cell.setCellValue(map.get("B").toString());//设置第i+1行第1列的值 102 cell.setCellStyle(style); //设置风格 103 } 104 if (map.get("C") != null) { 105 cell = row.createCell((short) 2); //创建第i+1行第1列 106 cell.setCellValue(map.get("C").toString());//设置第i+1行第1列的值 107 cell.setCellStyle(style); //设置风格 108 } 109 110 } 111 } 112 } catch (Exception e) { 113 e.printStackTrace(); 114 } 115 } 116 outputSetting(workbook,"导出信息("+ LocalTime.now() +").xls",response); 117 } 118 119 //固定配置 120 public void outputSetting(HSSFWorkbook workbook ,String fileName ,HttpServletResponse response) { 121 OutputStream out = null;//创建一个输出流对象 122 try { 123 out = response.getOutputStream();// 得到输出流 124 response.setHeader("Content-disposition","attachment; filename="+new String(fileName.getBytes(), StandardCharsets.ISO_8859_1));//filename是下载的xls的名 125 response.setContentType("application/msexcel;charset=UTF-8");//设置类型 126 response.setHeader("Pragma","No-cache");//设置头 127 response.setHeader("Cache-Control","no-cache");//设置头 128 response.setDateHeader("Expires", 0);//设置日期头 129 workbook.write(out); 130 out.flush(); 131 workbook.write(out); 132 } catch (IOException e) { 133 e.printStackTrace(); 134 }finally{ 135 try{ 136 if(out!=null){ 137 out.close(); 138 } 139 }catch(IOException e){ 140 e.printStackTrace(); 141 } 142 } 143 } 144 145 146 }
需要引用的包:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency>
导出文件预览:
成功不是终点,失败也并非末日,重要的是前行的勇气!