|
@@ -3,6 +3,7 @@ package com.qmth.boot.tools.excel;
|
|
|
import com.qmth.boot.tools.excel.convertor.ValueConvertor;
|
|
|
import com.qmth.boot.tools.excel.convertor.ValueConvertors;
|
|
|
import com.qmth.boot.tools.excel.enums.ExcelType;
|
|
|
+import com.qmth.boot.tools.excel.model.CellStyleConfig;
|
|
|
import com.qmth.boot.tools.excel.model.DataMap;
|
|
|
import com.qmth.boot.tools.excel.model.FieldParam;
|
|
|
import com.qmth.boot.tools.excel.model.ObjectParam;
|
|
@@ -19,6 +20,8 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
public abstract class ExcelWriter {
|
|
|
|
|
|
+ private CellStyleConfig styleConfig = new CellStyleConfig();
|
|
|
+
|
|
|
/**
|
|
|
* 初始化带表头的Excel生成工具
|
|
|
*
|
|
@@ -107,8 +110,13 @@ public abstract class ExcelWriter {
|
|
|
Row row = sheet.createRow(rowIndex.getAndIncrement());
|
|
|
Cell cell = row.createCell(0);
|
|
|
cell.setCellValue(title);
|
|
|
- CellStyle style = getWorkbook().createCellStyle();
|
|
|
- style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ //尝试获取预设单元格样式
|
|
|
+ CellStyle style = styleConfig.getStyle(sheet.getSheetName(), row.getRowNum(), cell.getColumnIndex());
|
|
|
+ if (style == null) {
|
|
|
+ //未预设时按照默认样式设置
|
|
|
+ style = getWorkbook().createCellStyle();
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ }
|
|
|
cell.setCellStyle(style);
|
|
|
//根据表头宽度合并单元格
|
|
|
if (columnCount > 1) {
|
|
@@ -124,6 +132,11 @@ public abstract class ExcelWriter {
|
|
|
for (String name : names) {
|
|
|
Cell cell = columnNameRow.createCell(columnIndex++);
|
|
|
cell.setCellValue(name);
|
|
|
+ //尝试获取预设单元格样式
|
|
|
+ CellStyle style = styleConfig.getStyle(sheet.getSheetName(), cell.getRowIndex(), cell.getColumnIndex());
|
|
|
+ if (style != null) {
|
|
|
+ cell.setCellStyle(style);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -133,6 +146,11 @@ public abstract class ExcelWriter {
|
|
|
for (String name : objectParam.getColumnNames()) {
|
|
|
Cell cell = dataRow.createCell(columnIndex++);
|
|
|
writeCell(cell, object, objectParam.get(name));
|
|
|
+ //尝试获取预设单元格样式
|
|
|
+ CellStyle style = styleConfig.getStyle(sheet.getSheetName(), cell.getRowIndex(), cell.getColumnIndex());
|
|
|
+ if (style != null) {
|
|
|
+ cell.setCellStyle(style);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -142,6 +160,11 @@ public abstract class ExcelWriter {
|
|
|
for (String name : columnNames) {
|
|
|
Cell cell = dataRow.createCell(columnIndex++);
|
|
|
cell.setCellValue(dataMap.getOrDefault(name, StringUtils.EMPTY));
|
|
|
+ //尝试获取预设单元格样式
|
|
|
+ CellStyle style = styleConfig.getStyle(sheet.getSheetName(), cell.getRowIndex(), cell.getColumnIndex());
|
|
|
+ if (style != null) {
|
|
|
+ cell.setCellStyle(style);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -150,6 +173,11 @@ public abstract class ExcelWriter {
|
|
|
for (int columnIndex = 0; columnIndex < columnNames.length; columnIndex++) {
|
|
|
Cell cell = dataRow.createCell(columnIndex);
|
|
|
cell.setCellValue(data.length > columnIndex ? data[columnIndex] : StringUtils.EMPTY);
|
|
|
+ //尝试获取预设单元格样式
|
|
|
+ CellStyle style = styleConfig.getStyle(sheet.getSheetName(), cell.getRowIndex(), cell.getColumnIndex());
|
|
|
+ if (style != null) {
|
|
|
+ cell.setCellStyle(style);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -174,7 +202,7 @@ public abstract class ExcelWriter {
|
|
|
protected abstract Workbook getWorkbook();
|
|
|
|
|
|
/**
|
|
|
- * 完成数据写入后,附加进行单元格样式设置
|
|
|
+ * 在数据写入前预先进行单元格样式设置
|
|
|
*
|
|
|
* @param sheetName sheet名称
|
|
|
* @param style 单元格样式,通过createCellStyle方法创建
|
|
@@ -182,11 +210,10 @@ public abstract class ExcelWriter {
|
|
|
* @param columnIndexs 列序号数组,最小序号为0
|
|
|
*/
|
|
|
public void setCellStyle(String sheetName, CellStyle style, int rowNumber, int... columnIndexs) {
|
|
|
- Sheet sheet = getWorkbook().getSheet(sheetName);
|
|
|
- for (int columnIndex : columnIndexs) {
|
|
|
- Cell cell = sheet.getRow(rowNumber).getCell(columnIndex);
|
|
|
- cell.setCellStyle(style);
|
|
|
+ if (columnIndexs == null || columnIndexs.length == 0) {
|
|
|
+ throw new IllegalArgumentException("columnIndexs should not be empty");
|
|
|
}
|
|
|
+ styleConfig.addCellStyle(sheetName, rowNumber, columnIndexs, style);
|
|
|
}
|
|
|
|
|
|
/**
|