|
@@ -0,0 +1,128 @@
|
|
|
|
+package cn.com.qmth.examcloud.tool.utils;
|
|
|
|
+
|
|
|
|
+import cn.afterturn.easypoi.excel.ExcelExportUtil;
|
|
|
|
+import cn.afterturn.easypoi.excel.entity.ExportParams;
|
|
|
|
+import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
|
|
|
|
+import cn.afterturn.easypoi.excel.export.styler.AbstractExcelExportStyler;
|
|
|
|
+import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Excel工具类
|
|
|
|
+ */
|
|
|
|
+public class ExcelUtils {
|
|
|
|
+
|
|
|
|
+ private static Logger log = LoggerFactory.getLogger(ExcelUtils.class);
|
|
|
|
+
|
|
|
|
+ public static <T> void exportExcel(Class<T> clazz, List<T> records, File excelFile) {
|
|
|
|
+ exportExcel(clazz, records, excelFile, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static <T> void exportExcel(Class<T> clazz, List<T> records, File excelFile, String title) {
|
|
|
|
+ if (excelFile == null) {
|
|
|
|
+ log.warn("[ExportExcel] excelFile is null");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (records == null) {
|
|
|
|
+ log.warn("[ExportExcel] records is null");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ FileUtils.makeDirs(excelFile.getParent());
|
|
|
|
+ log.info(String.format("[ExportExcel] records size is %s, %s", records.size(), excelFile.getAbsolutePath()));
|
|
|
|
+
|
|
|
|
+ ExportParams params = new ExportParams();
|
|
|
|
+ params.setStyle(ExcelStyle.class);
|
|
|
|
+ params.setType(ExcelType.XSSF);
|
|
|
|
+
|
|
|
|
+ if (StringUtils.isNotBlank(title)) {
|
|
|
|
+ params.setTitle(title);
|
|
|
|
+ params.setSheetName(title);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(excelFile);
|
|
|
|
+ Workbook workbook = ExcelExportUtil.exportExcel(params, clazz, records);) {
|
|
|
|
+
|
|
|
|
+ workbook.write(fos);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Excel导出默认样式
|
|
|
|
+ */
|
|
|
|
+ public static class ExcelStyle extends AbstractExcelExportStyler implements IExcelExportStyler {
|
|
|
|
+
|
|
|
|
+ private final short fontHeightInPoints = 16;
|
|
|
|
+
|
|
|
|
+ public ExcelStyle(Workbook workbook) {
|
|
|
|
+ super.createStyles(workbook);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public CellStyle getHeaderStyle(short headerColor) {
|
|
|
|
+ CellStyle headerStyle = this.workbook.createCellStyle();
|
|
|
|
+ Font font = this.workbook.createFont();
|
|
|
|
+ font.setBold(true);
|
|
|
|
+ font.setFontHeightInPoints(fontHeightInPoints);
|
|
|
|
+ headerStyle.setFont(font);
|
|
|
|
+ // headerStyle.setFillForegroundColor(headerColor);
|
|
|
|
+ // headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
+ headerStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
|
+ headerStyle.setBorderRight(BorderStyle.THIN);
|
|
|
|
+ headerStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
|
+ headerStyle.setBorderTop(BorderStyle.THIN);
|
|
|
|
+ headerStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
+ headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
+ return headerStyle;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
|
|
|
|
+ CellStyle cellStyle = workbook.createCellStyle();
|
|
|
|
+ cellStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
|
+ cellStyle.setBorderRight(BorderStyle.THIN);
|
|
|
|
+ cellStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
|
+ cellStyle.setBorderTop(BorderStyle.THIN);
|
|
|
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
+ cellStyle.setDataFormat(STRING_FORMAT);
|
|
|
|
+ if (isWarp) {
|
|
|
|
+ cellStyle.setWrapText(true);
|
|
|
|
+ }
|
|
|
|
+ return cellStyle;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public CellStyle getTitleStyle(short color) {
|
|
|
|
+ CellStyle titleStyle = this.workbook.createCellStyle();
|
|
|
|
+ Font font = this.workbook.createFont();
|
|
|
|
+ font.setBold(true);
|
|
|
|
+ font.setColor(IndexedColors.BLACK.getIndex());
|
|
|
|
+ titleStyle.setFont(font);
|
|
|
|
+ // titleStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
|
|
|
|
+ titleStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
|
+ titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
+ titleStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
|
+ titleStyle.setBorderRight(BorderStyle.THIN);
|
|
|
|
+ titleStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
|
+ titleStyle.setBorderTop(BorderStyle.THIN);
|
|
|
|
+ titleStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
+ titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
+ titleStyle.setWrapText(true);
|
|
|
|
+ return titleStyle;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
|
|
|
|
+ return isWarp ? this.stringNoneWrapStyle : this.stringNoneStyle;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|