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>
導出文件預覽: