|
@@ -0,0 +1,126 @@
|
|
|
|
+package shop.alien.gateway.util;
|
|
|
|
+
|
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
+
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
+import java.net.URL;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+public class ExcelGenerator {
|
|
|
|
+
|
|
|
|
+ public static <T> void generateExcel(String filePath, List<T> dataList, Class<T> clazz) throws IOException {
|
|
|
|
+ // 创建一个新的工作簿
|
|
|
|
+ XSSFWorkbook workbook = new XSSFWorkbook();
|
|
|
|
+ // 创建一个工作表
|
|
|
|
+ Sheet sheet = workbook.createSheet("Sheet1");
|
|
|
|
+
|
|
|
|
+ // 获取类的所有字段
|
|
|
|
+ Field[] fields = clazz.getDeclaredFields();
|
|
|
|
+
|
|
|
|
+ // 创建加粗字体
|
|
|
|
+ Font headerFont = workbook.createFont();
|
|
|
|
+ headerFont.setBold(true);
|
|
|
|
+
|
|
|
|
+ // 创建表头样式并应用字体
|
|
|
|
+ CellStyle headerCellStyle = workbook.createCellStyle();
|
|
|
|
+ headerCellStyle.setFont(headerFont);
|
|
|
|
+
|
|
|
|
+ // 创建表头
|
|
|
|
+ Row headerRow = sheet.createRow(0);
|
|
|
|
+ for (int i = 0; i < fields.length; i++) {
|
|
|
|
+ Field field = fields[i];
|
|
|
|
+ ExcelHeader excelHeader = field.getAnnotation(ExcelHeader.class);
|
|
|
|
+ String headerName;
|
|
|
|
+ if (excelHeader != null) {
|
|
|
|
+ headerName = excelHeader.value();
|
|
|
|
+ } else {
|
|
|
|
+ headerName = field.getName();
|
|
|
|
+ }
|
|
|
|
+ Cell cell = headerRow.createCell(i);
|
|
|
|
+ cell.setCellValue(headerName);
|
|
|
|
+// Cell cell = headerRow.createCell(i);
|
|
|
|
+// cell.setCellValue(fields[i].getName());
|
|
|
|
+ cell.setCellStyle(headerCellStyle);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 添加数据
|
|
|
|
+ for (int i = 0; i < dataList.size(); i++) {
|
|
|
|
+ Row row = sheet.createRow(i + 1);
|
|
|
|
+
|
|
|
|
+ T obj = dataList.get(i);
|
|
|
|
+ for (int j = 0; j < fields.length; j++) {
|
|
|
|
+ Field field = fields[j];
|
|
|
|
+ field.setAccessible(true);
|
|
|
|
+ if (field.isAnnotationPresent(ExcelImage.class)) {
|
|
|
|
+ row.setHeightInPoints(80); // 数据行行高
|
|
|
|
+ try {
|
|
|
|
+ String imageUrl = (String) field.get(obj);
|
|
|
|
+ if (imageUrl != null && !imageUrl.isEmpty()) {
|
|
|
|
+ try (InputStream is = new URL(imageUrl).openStream();
|
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
|
+ int length;
|
|
|
|
+ while ((length = is.read(buffer)) != -1) {
|
|
|
|
+ baos.write(buffer, 0, length);
|
|
|
|
+ }
|
|
|
|
+ byte[] imageBytes = baos.toByteArray();
|
|
|
|
+ int pictureIndex = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_JPEG);
|
|
|
|
+ // 去掉泛型参数 <Sheet>
|
|
|
|
+ Drawing patriarch = sheet.createDrawingPatriarch();
|
|
|
|
+ ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) j, i + 1, (short) (j + 1), i + 2);
|
|
|
|
+ patriarch.createPicture(anchor, pictureIndex);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ Cell cell = row.createCell(j);
|
|
|
|
+ cell.setCellValue("图片加载失败");
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ Cell cell = row.createCell(j);
|
|
|
|
+ cell.setCellValue("");
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ Cell cell = row.createCell(j);
|
|
|
|
+ cell.setCellValue("图片加载失败");
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ // 常规字段处理(保持原有)...
|
|
|
|
+ try {
|
|
|
|
+ Object value = field.get(obj);
|
|
|
|
+ Cell cell = row.createCell(j);
|
|
|
|
+ if (value != null) {
|
|
|
|
+ cell.setCellValue(value.toString());
|
|
|
|
+ }
|
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 自动调整列宽
|
|
|
|
+ for (int i = 0; i < fields.length; i++) {
|
|
|
|
+// sheet.autoSizeColumn(i);
|
|
|
|
+ sheet.setColumnWidth(i, Math.max(sheet.getColumnWidth(i), 15 * 256));
|
|
|
|
+ // 处理图片列等特殊情况,确保列宽足够
|
|
|
|
+ if (fields[i].isAnnotationPresent(ExcelImage.class)) {
|
|
|
|
+ sheet.setColumnWidth(i, Math.max(sheet.getColumnWidth(i), 12 * 256)); // 图片列适当加宽
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 将工作簿写入文件
|
|
|
|
+ try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
|
|
|
|
+ workbook.write(fileOut);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 关闭工作簿
|
|
|
|
+ workbook.close();
|
|
|
|
+ }
|
|
|
|
+}
|