package com.example.server.ExportExcel.method; import cn.hutool.core.date.DateUtil; import com.example.server.ExportExcel.dto.ExprotExcelDto; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import javax.servlet.http.HttpServletResponse; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Method; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; public class ExcelExport { public static void getHSSFWorkbook(String filePath, List dtos) throws IOException { // 创建一个SXSSFWorkbook,对应一个Excel文件 SXSSFWorkbook wb = new SXSSFWorkbook(); // 创建第一个工作表并填充数据 for (ExprotExcelDto dto : dtos) { String[][] content = new String[dto.getDataList().size()][dto.getColumnDto().size()]; if (dto.getDataList() != null) { for (int i = 0; i < dto.getDataList().size(); i++) { Object object = dto.getDataList().get(i); Class c1azz = object.getClass(); for (int j = 0; j < dto.getColumnDto().size(); j++) { Method method = null; try { String fieldName = dto.getColumnDto().get(j).getColumnName(); method = c1azz.getMethod("get" + StringUtils.capitalize(fieldName)); Object value = method.invoke(object); String valueStr = ""; if (value == null) { } else if (value instanceof Date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); valueStr = sdf.format(value); } else { valueStr = value.toString(); } content[i][j] = valueStr; } catch (Exception e) { e.printStackTrace(); } } } } createSheetAndFillData(wb, dto, content); } try (FileOutputStream fileOut = new FileOutputStream(filePath)) { wb.write(fileOut); } catch (IOException e) { e.printStackTrace(); } finally { try { wb.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void createSheetAndFillData(SXSSFWorkbook wb, ExprotExcelDto data, String[][] content) { SXSSFSheet sheet = wb.createSheet(data.getSheetName()); //sheet.setDefaultRowHeightInPoints(20); CellStyle titleStyle = wb.createCellStyle(); //4.设置字体样式 Font titleFont = wb.createFont(); titleFont.setFontName("宋体"); //titleFont.setFontHeight((short) 10); //titleFont.setItalic(true); //titleFont.setStrikeout(true); //titleFont.setColor((short) 111); //titleFont.setTypeOffset((short) 5); //titleFont.setUnderline((byte) 0000); titleFont.setBold(true); //titleFont.setFontHeightInPoints((short) 12); titleStyle.setBorderBottom(BorderStyle.THICK); titleStyle.setBorderLeft(BorderStyle.THIN); titleStyle.setBorderRight(BorderStyle.THIN); titleStyle.setBorderTop(BorderStyle.THIN); titleStyle.setAlignment(HorizontalAlignment.CENTER); titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); titleStyle.setWrapText(true); titleStyle.setFont(titleFont); SXSSFCell cell = null; SXSSFRow titleRow = sheet.createRow(0); //titleRow.setHeightInPoints(40); // 循环输出标题行 for (int i = 0; i < data.getColumnDto().size(); i++) { cell = titleRow.createCell(i); cell.setCellValue(data.getColumnDto().get(i).getColumnDesc()); cell.setCellStyle(titleStyle); sheet.setColumnWidth(i, 5000); } CellStyle contentStyle = wb.createCellStyle(); //4.设置字体样式 Font contentFont = wb.createFont(); contentFont.setFontName("宋体"); contentFont.setBold(false); contentStyle.setBorderBottom(BorderStyle.THICK); contentStyle.setBorderLeft(BorderStyle.THIN); contentStyle.setBorderRight(BorderStyle.THIN); contentStyle.setBorderTop(BorderStyle.THIN); contentStyle.setAlignment(HorizontalAlignment.CENTER); contentStyle.setVerticalAlignment(VerticalAlignment.CENTER); contentStyle.setWrapText(true); contentStyle.setFont(contentFont); for (int j = 0; j < data.getColumnDto().size(); j++) { if (data.getColumnDto().get(j).getColumnWidth() != null && data.getColumnDto().get(j).getColumnWidth() != 0) sheet.setColumnWidth(j, data.getColumnDto().get(j).getColumnWidth() * 256); } for (int i = 0; i < content.length; i++) { SXSSFRow row = sheet.createRow(1 + i); for (int j = 0; j < content[i].length; j++) { cell = row.createCell(j); if (data.getColumnDto().get(j).getHorizontalAlignment() != null) { CellStyle contentStyle2 = wb.createCellStyle(); contentStyle2.cloneStyleFrom(contentStyle); contentStyle2.setAlignment(data.getColumnDto().get(j).getHorizontalAlignment()); cell.setCellStyle(contentStyle2); } else { cell.setCellStyle(contentStyle); } cell.setCellValue(content[i][j]); } } } }