|  |  |  | 
|---|
|  |  |  | package com.zy.asrs.wcs.utils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.zy.asrs.framework.common.Cools; | 
|---|
|  |  |  | import io.swagger.annotations.ApiModelProperty; | 
|---|
|  |  |  | import org.apache.poi.hssf.usermodel.HSSFWorkbook; | 
|---|
|  |  |  | import org.apache.poi.ss.usermodel.Row; | 
|---|
|  |  |  | import org.apache.poi.ss.usermodel.Sheet; | 
|---|
|  |  |  | import org.apache.poi.ss.usermodel.Workbook; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletResponse; | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.lang.reflect.Field; | 
|---|
|  |  |  | import java.lang.reflect.Modifier; | 
|---|
|  |  |  | import java.net.URLEncoder; | 
|---|
|  |  |  | import java.text.SimpleDateFormat; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Created by vincent on 2/17/2024 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public class ExcelUtil { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static Workbook create() { | 
|---|
|  |  |  | // 创建 Workbook 和 Sheet | 
|---|
|  |  |  | public static void build(Workbook workbook, HttpServletResponse response) { | 
|---|
|  |  |  | response.reset(); | 
|---|
|  |  |  | Utils.cors(response); | 
|---|
|  |  |  | response.setContentType("application/octet-stream; charset=utf-8"); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("export", "UTF-8")); | 
|---|
|  |  |  | workbook.write(response.getOutputStream()); | 
|---|
|  |  |  | } catch (IOException ignore) {} | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static <T> Workbook create(List<T> list, Class<T> clz) { | 
|---|
|  |  |  | HSSFWorkbook workbook = new HSSFWorkbook(); | 
|---|
|  |  |  | Sheet sheet = workbook.createSheet("运维报表"); | 
|---|
|  |  |  | Sheet sheet = workbook.createSheet(clz.getSimpleName()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 创建表头 | 
|---|
|  |  |  | Row header = sheet.createRow(0); | 
|---|
|  |  |  | header.createCell(0).setCellValue("姓名"); | 
|---|
|  |  |  | header.createCell(1).setCellValue("年龄"); | 
|---|
|  |  |  | header.createCell(2).setCellValue("性别"); | 
|---|
|  |  |  | header.createCell(3).setCellValue("职位"); | 
|---|
|  |  |  | Field[] fields = Cools.getAllFields(clz); | 
|---|
|  |  |  | int headerIdx = 0; | 
|---|
|  |  |  | for (Field field : fields) { | 
|---|
|  |  |  | if (Modifier.isFinal(field.getModifiers()) | 
|---|
|  |  |  | || Modifier.isStatic(field.getModifiers()) | 
|---|
|  |  |  | || Modifier.isTransient(field.getModifiers())) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String memo = "未知"; | 
|---|
|  |  |  | if (field.isAnnotationPresent(ApiModelProperty.class)) { | 
|---|
|  |  |  | memo = field.getAnnotation(ApiModelProperty.class).value(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | header.createCell(headerIdx).setCellValue(memo); | 
|---|
|  |  |  | headerIdx++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 添加一些数据 | 
|---|
|  |  |  | Row row1 = sheet.createRow(1); | 
|---|
|  |  |  | row1.createCell(0).setCellValue("张三"); | 
|---|
|  |  |  | row1.createCell(1).setCellValue(28); | 
|---|
|  |  |  | row1.createCell(2).setCellValue("男"); | 
|---|
|  |  |  | row1.createCell(3).setCellValue("运维工程师"); | 
|---|
|  |  |  | int rowIndex = 1; | 
|---|
|  |  |  | for (T t : list) { | 
|---|
|  |  |  | Row row = sheet.createRow(rowIndex++); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Row row2 = sheet.createRow(2); | 
|---|
|  |  |  | row2.createCell(0).setCellValue("李四"); | 
|---|
|  |  |  | row2.createCell(1).setCellValue(32); | 
|---|
|  |  |  | row2.createCell(2).setCellValue("女"); | 
|---|
|  |  |  | row2.createCell(3).setCellValue("项目经理"); | 
|---|
|  |  |  | int cellIndex = 0; | 
|---|
|  |  |  | for (Field field : fields) { | 
|---|
|  |  |  | if (Modifier.isFinal(field.getModifiers()) | 
|---|
|  |  |  | || Modifier.isStatic(field.getModifiers()) | 
|---|
|  |  |  | || Modifier.isTransient(field.getModifiers())) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Row row3 = sheet.createRow(3); | 
|---|
|  |  |  | row3.createCell(0).setCellValue("王五"); | 
|---|
|  |  |  | row3.createCell(1).setCellValue(25); | 
|---|
|  |  |  | row3.createCell(2).setCellValue("男"); | 
|---|
|  |  |  | row3.createCell(3).setCellValue("开发工程师"); | 
|---|
|  |  |  | field.setAccessible(true);  // 此行很重要,特别是字段为private时 | 
|---|
|  |  |  | Object value = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | value = field.get(t); | 
|---|
|  |  |  | } catch (IllegalAccessException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (value != null) { | 
|---|
|  |  |  | if (value instanceof Date) { | 
|---|
|  |  |  | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 
|---|
|  |  |  | row.createCell(cellIndex).setCellValue(sdf.format((Date) value)); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | row.createCell(cellIndex).setCellValue(value.toString()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | cellIndex++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 执行单元格自动列宽 | 
|---|
|  |  |  | for (int i = 0; i <= 3; i++) { | 
|---|
|  |  |  | for (int i = 0; i <= fields.length; i++) { | 
|---|
|  |  |  | sheet.autoSizeColumn(i); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|