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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
| import cn.hutool.core.collection.CollectionUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy; import com.cloudwise.config.CustomCellWriteHandler; import lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.usermodel.*; import org.springframework.stereotype.Component; import sun.tools.java.Constants;
import javax.servlet.http.HttpServletResponse; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URLEncoder; import java.util.*;
@Component @Slf4j public class ExcelUtils {
public void excelDownloadLink(HttpServletResponse response, String title, List<String> names, List<String> fieldEn, List list, String sheetName, String fileName) { try { List<List<String>> datas = setData(list, fieldEn); fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream()) .excelType(ExcelTypeEnum.XLSX) .head(head(CollectionUtil.isNotEmpty(names) ? names.toArray(new String[0]) : new String[0], title)) .inMemory(true) .registerWriteHandler(new TitleStyleUtils(names)) .registerWriteHandler(new CellWriteHandler() { @Override public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
}
@Override public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {
}
@Override public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {
}
@Override public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) { if (cell.getRowIndex() > 6) { Workbook workbook = writeSheetHolder.getSheet().getWorkbook(); workbook.getSheetAt(0).setColumnWidth(cell.getColumnIndex(), 2000); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.CENTER); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setWrapText(true); if (cell.getColumnIndex() == 0) { Font font = workbook.createFont(); font.setColor(IndexedColors.RED.getIndex()); font.setFontName("宋体"); cellStyle.setFont(font); }
cell.setCellStyle(cellStyle); } } }) .registerWriteHandler(new SimpleColumnWidthStyleStrategy(20)) .sheet(sheetName) .doWrite(CollectionUtil.isNotEmpty(datas) ? datas : new ArrayList()); } catch (IOException e) { e.printStackTrace(); response.reset(); response.setCharacterEncoding("utf-8"); response.setContentType("application/json"); try { response.getWriter().println("打印失败"); } catch (IOException ex) { ex.printStackTrace(); } } }
public void excelExportData(HttpServletResponse response, List<String> names, List<String> fieldEn, List list, String sheetName, String fileName) { try {
List<List<String>> datas = setData(list, fieldEn); fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream()) .excelType(ExcelTypeEnum.XLSX) .head(headData(CollectionUtil.isNotEmpty(names) ? names.toArray(new String[0]) : new String[0])) .registerWriteHandler(new CustomCellWriteHandler()) .registerWriteHandler(new SimpleRowHeightStyleStrategy((short) 25, (short) 25)) .sheet(sheetName) .doWrite(CollectionUtil.isNotEmpty(datas) ? datas : new ArrayList());
} catch (IOException e) { e.printStackTrace(); response.reset(); response.setCharacterEncoding("utf-8"); response.setContentType("application/json"); try { response.getWriter().println("打印失败"); } catch (IOException ex) { ex.printStackTrace(); } } }
public void excelExportOrdinaryData(HttpServletResponse response, String sheetName, String fileName, List list, Class clazz) { try { fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); response.setCharacterEncoding("UTF-8"); EasyExcel.write(response.getOutputStream(), clazz) .excelType(ExcelTypeEnum.XLSX) .head(clazz) .relativeHeadRowIndex(0) .registerWriteHandler(new CustomCellWriteHandler()) .registerWriteHandler(new SimpleRowHeightStyleStrategy((short) 25, (short) 25)) .sheet(sheetName) .doWrite(list); } catch (IOException e) { e.printStackTrace(); response.reset(); response.setCharacterEncoding("utf-8"); response.setContentType("application/json"); try { response.getWriter().println("打印失败"); } catch (IOException ex) { ex.printStackTrace(); } }
}
private List<List<String>> setData(List list, List<String> fieldEn) { List<List<String>> datas = new ArrayList<>(); List<Map<Object, Object>> maps = new ArrayList<>(); if (CollectionUtil.isNotEmpty(list)) { for (Object o : list) { Class<?> aClass = o.getClass(); Field[] fields = aClass.getDeclaredFields(); Map<Object, Object> map = new HashMap<>(40);
for (Field field : fields) { map.put(field.getName(), getResult(field.getName(), o)); } maps.add(map); } for (Map<Object, Object> map : maps) { List<String> data = new LinkedList<String>(); for (int i = 0; i < fieldEn.size(); i++) { Object o = map.get(fieldEn.get(i)); data.add(Objects.isNull(o) ? "示例数据,此列为空,从下一行开始填写" : o.toString()); } datas.add(data); } } return datas; }
private static Object getResult(Object fieldName, Object o) { try { Class<?> aClass = o.getClass(); Field declaredField = aClass.getDeclaredField(fieldName.toString()); declaredField.setAccessible(true); PropertyDescriptor pd = new PropertyDescriptor(declaredField.getName(), aClass); Method readMethod = pd.getReadMethod();
return readMethod.invoke(o); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IntrospectionException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return null; }
private List<List<String>> head(String[] header, String bigTitle) { List<String> head0 = null; String title = "填 \n" + "写 \n" + "说 \n" + "明 \n"; List<List<String>> list = new LinkedList<List<String>>();
String[] header0 = {"字段类型", "单行文本", "单行文本", "单行文本", "单行文本", "单行文本", "单行文本", "单行文本", "日期", "多行文本"}; String[] header1 = {"表格列编码", "userInfo_UM", "userInfo_Name", "userInfo_OrgName", "userInfo_HRjobs", "userInfo_Leader", "userInfo_Role", "userInfo_Handhabe", "userInfo_ExpirationTime", "userInfo_applyExplain"}; String[] header2 = {"字段校验规则", "无校验", "无校验", "无校验", "无校验", "无校验", "无校验", "无校验", "无校验", "无校验"}; String[] header3 = {"是否唯一值", "否", "否", "否", "否", "否", "否", "否", "否", "否"}; String[] header4 = {"属性约束", "必填", "必填", "必填", "必填", "必填", "必填", "必填", "非必填", "非必填"}; for (int i = 0; i < header.length; i++) { head0 = new LinkedList<>(); if (i < 1) { head0.add(title); } else { head0.add(bigTitle); } head0.add(header0[i]); head0.add(header1[i]); head0.add(header2[i]); head0.add(header3[i]); head0.add(header4[i]); head0.add(header[i]); list.add(head0); } return list; }
private List<List<String>> headData(String[] header) { List<String> head0 = null; List<List<String>> list = new LinkedList<List<String>>(); for (String h : header) { head0 = new LinkedList<>(); head0.add(h); list.add(head0); } return list; }
}
|