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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| package com.test.controller;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
@RestController @RequestMapping("/excel") public class ExcelController {
@RequestMapping(value = "/exportExcel", method = RequestMethod.GET) @ResponseBody public void exportExcel(HttpServletRequest request, HttpServletResponse response) { long start = System.currentTimeMillis(); List<String> topDNames = new ArrayList<>(); Map<String, List<String>> map = new HashMap<>(); for (int i = 1; i <= 6;i++) { topDNames.add("rowV" + i); List<String> item = new ArrayList<>(); for (int j = 1; j <= 2; j++) { item.add("行" + i + "数" + j); } map.put("rowV" + i, item); }
List<String> leftDNames = new ArrayList<>(); for (int i = 1; i <= 6; i++) { leftDNames.add("columnV" + i); List<String> item = new ArrayList<>(); for (int j = 1; j <= 3; j++) { item.add("列" + i + "数" + j); } map.put("columnV" + i, item); }
try { OutputStream out = response.getOutputStream(); response.reset(); String fileName = "test"; String codeFileName = java.net.URLEncoder.encode(fileName, StandardCharsets.UTF_8); response.setHeader("Content-Disposition", "attachment; filename=" + codeFileName + ".csv"); response.setContentType("application/octet-stream;charset=utf-8");
Workbook workbook = new SXSSFWorkbook(10000); Sheet sheet = workbook.createSheet(); sheet.setDefaultColumnWidth(15);
int rowIndex = 0; int columnIndex = leftDNames.size();
List<List<String>> topKeys = new ArrayList<>(); List<List<String>> leftKeys = new ArrayList<>();
int allCols = 1; for (String topDName : topDNames) { int length = map.get(topDName).size(); allCols = allCols * length; } int tmpClos = allCols; for (String item : topDNames) { List<String> columnNames = map.get(item); int loopCnt = allCols / tmpClos; tmpClos = tmpClos / columnNames.size(); Row row = sheet.getRow(rowIndex); if (null == row) { row = sheet.createRow(rowIndex); }
List<String> keyTop = new ArrayList<>();
for (int i = 0; i < loopCnt; i++) { for (String name : columnNames) { Cell cell = row.createCell(columnIndex); cell.setCellValue(name); for (int tmp = 0; tmp < tmpClos; tmp++) { keyTop.add(name); } if (tmpClos > 1) { sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, columnIndex, columnIndex + tmpClos - 1)); } columnIndex = columnIndex + tmpClos; } } topKeys.add(keyTop); columnIndex = leftDNames.size(); rowIndex++; }
columnIndex = 0; rowIndex = topDNames.size();
int allRows = 1; for (String leftDName : leftDNames) { int length = map.get(leftDName).size(); allRows = allRows * length; } int tmpRows = allRows; for (String item : leftDNames) { List<String> rowNames = map.get(item); int loopCnt = allRows / tmpRows; tmpRows = tmpRows / rowNames.size(); List<String> keyleft = new ArrayList<>(); for (int i = 0; i < loopCnt; i++) { for (String name : rowNames) { Row row = sheet.getRow(rowIndex); if (null == row) { row = sheet.createRow(rowIndex); } Cell cell = row.createCell(columnIndex); cell.setCellValue(name); for (int tmp = 0; tmp < tmpRows; tmp++) { keyleft.add(name); } if (tmpRows > 1) { sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex + tmpRows - 1, columnIndex, columnIndex)); } rowIndex = rowIndex + tmpRows; } } leftKeys.add(keyleft); rowIndex = topDNames.size(); columnIndex++; }
List<String> topKeysStrings = new ArrayList<>(); for (int i=0;i<topKeys.get(0).size();i++){ StringBuffer key = new StringBuffer(); key.append(topKeys.get(0).get(i)); for (int j=1;j<topKeys.size();j++){ key.append("." + topKeys.get(j).get(i)); } topKeysStrings.add(String.valueOf(key)); }
List<String> leftKeysStrings = new ArrayList<>(); for (int i=0;i<leftKeys.get(0).size();i++){ StringBuffer key = new StringBuffer(); key.append(leftKeys.get(0).get(i)); for (int j=1;j<leftKeys.size();j++){ key.append("." + leftKeys.get(j).get(i)); } leftKeysStrings.add(String.valueOf(key)); }
rowIndex = topDNames.size(); columnIndex = leftDNames.size(); for (String left:leftKeysStrings){ for (String top:topKeysStrings){ Row row = sheet.getRow(rowIndex); if (null == sheet.getRow(rowIndex)) { row = sheet.createRow(rowIndex); } Cell cell = row.createCell(columnIndex); cell.setCellValue(top + "/" + left); columnIndex++; } columnIndex = leftDNames.size(); rowIndex++; }
workbook.write(out); out.flush(); workbook.close(); out.close(); ((SXSSFWorkbook)workbook).dispose(); System.out.println(System.currentTimeMillis() - start);
} catch (Exception e) { e.printStackTrace(); } } }
|