add DefaultStyleSet

This commit is contained in:
Looly
2024-06-04 17:26:50 +08:00
parent b8cfaecfc0
commit 9a34a155c6
9 changed files with 114 additions and 137 deletions

View File

@@ -38,6 +38,8 @@ import org.dromara.hutool.poi.excel.cell.CellEditor;
import org.dromara.hutool.poi.excel.cell.CellRangeUtil; import org.dromara.hutool.poi.excel.cell.CellRangeUtil;
import org.dromara.hutool.poi.excel.cell.CellUtil; import org.dromara.hutool.poi.excel.cell.CellUtil;
import org.dromara.hutool.poi.excel.style.Align; import org.dromara.hutool.poi.excel.style.Align;
import org.dromara.hutool.poi.excel.style.DefaultStyleSet;
import org.dromara.hutool.poi.excel.style.StyleSet;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -184,7 +186,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
*/ */
public ExcelWriter(final Sheet sheet) { public ExcelWriter(final Sheet sheet) {
super(sheet); super(sheet);
this.styleSet = new StyleSet(workbook); this.styleSet = new DefaultStyleSet(workbook);
this.currentRow = new AtomicInteger(0); this.currentRow = new AtomicInteger(0);
} }
// endregion // endregion
@@ -338,27 +340,6 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
return this.styleSet; return this.styleSet;
} }
/**
* 获取头部样式,获取样式后可自定义样式
*
* @return 头部样式
*/
public CellStyle getHeadCellStyle() {
return this.styleSet.headCellStyle;
}
/**
* 获取单元格样式,获取样式后可自定义样式
*
* @return 单元格样式
*/
public CellStyle getCellStyle() {
if (null == this.styleSet) {
return null;
}
return this.styleSet.cellStyle;
}
/** /**
* 获得当前行 * 获得当前行
* *
@@ -644,8 +625,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
} }
/** /**
* 合并当前行的单元格<br> * 合并当前行的单元格
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
* *
* @param lastColumn 合并到的最后一个列号 * @param lastColumn 合并到的最后一个列号
* @return this * @return this
@@ -656,8 +636,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 合并当前行的单元格,并写入对象到单元格<br> * 合并当前行的单元格,并写入对象到单元格<br>
* 如果写到单元格中的内容非null行号自动+1否则当前行号不变<br> * 如果写到单元格中的内容非null行号自动+1否则当前行号不变
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
* *
* @param lastColumn 合并到的最后一个列号 * @param lastColumn 合并到的最后一个列号
* @param content 合并单元格后的内容 * @param content 合并单元格后的内容
@@ -669,8 +648,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 合并某行的单元格,并写入对象到单元格<br> * 合并某行的单元格,并写入对象到单元格<br>
* 如果写到单元格中的内容非null行号自动+1否则当前行号不变<br> * 如果写到单元格中的内容非null行号自动+1否则当前行号不变
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
* *
* @param lastColumn 合并到的最后一个列号 * @param lastColumn 合并到的最后一个列号
* @param content 合并单元格后的内容 * @param content 合并单元格后的内容
@@ -692,8 +670,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
} }
/** /**
* 合并某行的单元格,并写入对象到单元格<br> * 合并某行的单元格,并写入对象到单元格
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
* *
* @param cellRangeAddress 合并单元格范围,定义了起始行列和结束行列 * @param cellRangeAddress 合并单元格范围,定义了起始行列和结束行列
* @param content 合并单元格后的内容 * @param content 合并单元格后的内容
@@ -706,7 +683,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
CellStyle style = null; CellStyle style = null;
if (null != this.styleSet) { if (null != this.styleSet) {
style = styleSet.getStyleByValueType(content, isSetHeaderStyle); style = styleSet.getStyleFor(new CellReference(cellRangeAddress.getFirstRow(), cellRangeAddress.getFirstColumn()), content, isSetHeaderStyle);
} }
return merge(cellRangeAddress, content, style); return merge(cellRangeAddress, content, style);
@@ -737,8 +714,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 写出数据本方法只是将数据写入Workbook中的Sheet并不写出到文件<br> * 写出数据本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加<br> * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式<br>
* 默认的当当前行号为0时写出标题如果为Map或Bean否则不写标题 * 默认的当当前行号为0时写出标题如果为Map或Bean否则不写标题
* *
* <p> * <p>
@@ -760,8 +736,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 写出数据本方法只是将数据写入Workbook中的Sheet并不写出到文件<br> * 写出数据本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加<br> * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
* *
* <p> * <p>
* data中元素支持的类型有 * data中元素支持的类型有
@@ -791,8 +766,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 写出数据本方法只是将数据写入Workbook中的Sheet并不写出到文件<br> * 写出数据本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加<br> * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式<br>
* data中元素支持的类型有 * data中元素支持的类型有
* *
* <p> * <p>
@@ -931,8 +905,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 写出一行标题数据<br> * 写出一行标题数据<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br> * 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br> * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
* *
* @param rowData 一行的数据 * @param rowData 一行的数据
* @return this * @return this
@@ -955,8 +928,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 写出复杂标题的第二行标题数据<br> * 写出复杂标题的第二行标题数据<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br> * 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br> * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
* *
* <p> * <p>
* 此方法的逻辑是:将一行数据写出到当前行,遇到已存在的单元格跳过,不存在的创建并赋值。 * 此方法的逻辑是:将一行数据写出到当前行,遇到已存在的单元格跳过,不存在的创建并赋值。
@@ -1085,8 +1057,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 写出一行数据<br> * 写出一行数据<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br> * 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br> * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
* *
* @param rowData 一行的数据 * @param rowData 一行的数据
* @return this * @return this
@@ -1103,8 +1074,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 从第1列开始按列写入数据(index 从0开始)<br> * 从第1列开始按列写入数据(index 从0开始)<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br> * 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br> * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
* *
* @param colMap 一列的数据 * @param colMap 一列的数据
* @param isWriteKeyAsHead 是否将Map的Key作为表头输出如果为True第一行为表头紧接着为values * @param isWriteKeyAsHead 是否将Map的Key作为表头输出如果为True第一行为表头紧接着为values
@@ -1117,8 +1087,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 从指定列开始按列写入数据(index 从0开始)<br> * 从指定列开始按列写入数据(index 从0开始)<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br> * 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br> * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
* *
* @param colMap 一列的数据 * @param colMap 一列的数据
* @param startColIndex 起始的列号从0开始 * @param startColIndex 起始的列号从0开始
@@ -1140,8 +1109,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 为第一列写入数据<br> * 为第一列写入数据<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br> * 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br> * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
* *
* @param headerVal 表头名称,如果为null则不写入 * @param headerVal 表头名称,如果为null则不写入
* @param colData 需要写入的列数据 * @param colData 需要写入的列数据
@@ -1155,8 +1123,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 为第指定列写入数据<br> * 为第指定列写入数据<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br> * 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br> * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
* *
* @param headerVal 表头名称,如果为null则不写入 * @param headerVal 表头名称,如果为null则不写入
* @param colIndex 列index * @param colIndex 列index

View File

@@ -21,6 +21,7 @@ import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeUtil; import org.apache.poi.ss.util.CellRangeUtil;
import org.dromara.hutool.poi.excel.style.StyleSet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@@ -18,10 +18,10 @@ import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.RegionUtil; import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.ss.util.SheetUtil; import org.apache.poi.ss.util.SheetUtil;
import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.poi.excel.StyleSet;
import org.dromara.hutool.poi.excel.cell.editors.TrimEditor; import org.dromara.hutool.poi.excel.cell.editors.TrimEditor;
import org.dromara.hutool.poi.excel.cell.setters.CellSetterFactory; import org.dromara.hutool.poi.excel.cell.setters.CellSetterFactory;
import org.dromara.hutool.poi.excel.cell.values.CompositeCellValue; import org.dromara.hutool.poi.excel.cell.values.CompositeCellValue;
import org.dromara.hutool.poi.excel.style.StyleSet;
/** /**
* Excel表格中单元格工具类 * Excel表格中单元格工具类
@@ -114,7 +114,7 @@ public class CellUtil {
CellStyle cellStyle = null; CellStyle cellStyle = null;
if (null != styleSet) { if (null != styleSet) {
cellStyle = styleSet.getStyleByValueType(value, isHeader); cellStyle = styleSet.getStyleFor(new CellReference(cell), value, isHeader);
} }
setCellValue(cell, value, cellStyle, cellEditor); setCellValue(cell, value, cellStyle, cellEditor);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2023 looly(loolly@aliyun.com) * Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2. * Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at: * You may obtain a copy of Mulan PSL v2 at:
@@ -10,19 +10,11 @@
* See the Mulan PSL v2 for more details. * See the Mulan PSL v2 for more details.
*/ */
package org.dromara.hutool.poi.excel; package org.dromara.hutool.poi.excel.style;
import org.dromara.hutool.poi.excel.style.StyleUtil;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.Serializable; import java.io.Serializable;
import java.math.BigDecimal; import java.math.BigDecimal;
@@ -31,11 +23,12 @@ import java.util.Calendar;
import java.util.Date; import java.util.Date;
/** /**
* 样式集合此样式集合汇集了整个工作簿的样式用于减少样式的创建和冗余 * 默认样式集合定义了标题数字日期等默认样式
* *
* @author looly * @author Looly
* @since 6.0.0
*/ */
public class StyleSet implements Serializable { public class DefaultStyleSet implements StyleSet, Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
@@ -45,30 +38,30 @@ public class StyleSet implements Serializable {
/** /**
* 标题样式 * 标题样式
*/ */
protected final CellStyle headCellStyle; private final CellStyle headCellStyle;
/** /**
* 默认样式 * 默认样式
*/ */
protected final CellStyle cellStyle; private final CellStyle cellStyle;
/** /**
* 默认数字样式 * 默认数字样式
*/ */
protected final CellStyle cellStyleForNumber; private final CellStyle cellStyleForNumber;
/** /**
* 默认日期样式 * 默认日期样式
*/ */
protected final CellStyle cellStyleForDate; private final CellStyle cellStyleForDate;
/** /**
* 默认链接样式 * 默认链接样式
*/ */
protected final CellStyle cellStyleForHyperlink; private final CellStyle cellStyleForHyperlink;
/** /**
* 构造 * 构造
* *
* @param workbook 工作簿 * @param workbook 工作簿
*/ */
public StyleSet(final Workbook workbook) { public DefaultStyleSet(final Workbook workbook) {
this.workbook = workbook; this.workbook = workbook;
this.headCellStyle = StyleUtil.createHeadCellStyle(workbook); this.headCellStyle = StyleUtil.createHeadCellStyle(workbook);
this.cellStyle = StyleUtil.createDefaultCellStyle(workbook); this.cellStyle = StyleUtil.createDefaultCellStyle(workbook);
@@ -86,12 +79,46 @@ public class StyleSet implements Serializable {
// 默认链接样式 // 默认链接样式
this.cellStyleForHyperlink = StyleUtil.cloneCellStyle(workbook, this.cellStyle); this.cellStyleForHyperlink = StyleUtil.cloneCellStyle(workbook, this.cellStyle);
final Font font = this.workbook.createFont(); final Font font = workbook.createFont();
font.setUnderline((byte) 1); font.setUnderline((byte) 1);
font.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex()); font.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
this.cellStyleForHyperlink.setFont(font); this.cellStyleForHyperlink.setFont(font);
} }
@Override
public CellStyle getStyleFor(final CellReference reference, final Object cellValue, final boolean isHeader) {
CellStyle style = null;
if (isHeader && null != this.headCellStyle) {
style = headCellStyle;
} else if (null != cellStyle) {
style = cellStyle;
}
if (cellValue instanceof Date
|| cellValue instanceof TemporalAccessor
|| cellValue instanceof Calendar) {
// 日期单独定义格式
if (null != this.cellStyleForDate) {
style = this.cellStyleForDate;
}
} else if (cellValue instanceof Number) {
// 数字单独定义格式
if ((cellValue instanceof Double || cellValue instanceof Float || cellValue instanceof BigDecimal) &&
null != this.cellStyleForNumber) {
style = this.cellStyleForNumber;
}
} else if (cellValue instanceof Hyperlink) {
// 自定义超链接样式
if (null != this.cellStyleForHyperlink) {
style = this.cellStyleForHyperlink;
}
}
return style;
}
/** /**
* 获取头部样式获取后可以定义整体头部样式 * 获取头部样式获取后可以定义整体头部样式
* *
@@ -146,7 +173,7 @@ public class StyleSet implements Serializable {
* @return this * @return this
* @since 4.0.0 * @since 4.0.0
*/ */
public StyleSet setBorder(final BorderStyle borderSize, final IndexedColors colorIndex) { public DefaultStyleSet setBorder(final BorderStyle borderSize, final IndexedColors colorIndex) {
StyleUtil.setBorder(this.headCellStyle, borderSize, colorIndex); StyleUtil.setBorder(this.headCellStyle, borderSize, colorIndex);
StyleUtil.setBorder(this.cellStyle, borderSize, colorIndex); StyleUtil.setBorder(this.cellStyle, borderSize, colorIndex);
StyleUtil.setBorder(this.cellStyleForNumber, borderSize, colorIndex); StyleUtil.setBorder(this.cellStyleForNumber, borderSize, colorIndex);
@@ -163,7 +190,7 @@ public class StyleSet implements Serializable {
* @return this * @return this
* @since 4.0.0 * @since 4.0.0
*/ */
public StyleSet setAlign(final HorizontalAlignment halign, final VerticalAlignment valign) { public DefaultStyleSet setAlign(final HorizontalAlignment halign, final VerticalAlignment valign) {
StyleUtil.setAlign(this.headCellStyle, halign, valign); StyleUtil.setAlign(this.headCellStyle, halign, valign);
StyleUtil.setAlign(this.cellStyle, halign, valign); StyleUtil.setAlign(this.cellStyle, halign, valign);
StyleUtil.setAlign(this.cellStyleForNumber, halign, valign); StyleUtil.setAlign(this.cellStyleForNumber, halign, valign);
@@ -180,7 +207,7 @@ public class StyleSet implements Serializable {
* @return this * @return this
* @since 4.0.0 * @since 4.0.0
*/ */
public StyleSet setBackgroundColor(final IndexedColors backgroundColor, final boolean withHeadCell) { public DefaultStyleSet setBackgroundColor(final IndexedColors backgroundColor, final boolean withHeadCell) {
if (withHeadCell) { if (withHeadCell) {
StyleUtil.setColor(this.headCellStyle, backgroundColor, FillPatternType.SOLID_FOREGROUND); StyleUtil.setColor(this.headCellStyle, backgroundColor, FillPatternType.SOLID_FOREGROUND);
} }
@@ -200,7 +227,7 @@ public class StyleSet implements Serializable {
* @param ignoreHead 是否跳过头部样式 * @param ignoreHead 是否跳过头部样式
* @return this * @return this
*/ */
public StyleSet setFont(final short color, final short fontSize, final String fontName, final boolean ignoreHead) { public DefaultStyleSet setFont(final short color, final short fontSize, final String fontName, final boolean ignoreHead) {
final Font font = StyleUtil.createFont(this.workbook, color, fontSize, fontName); final Font font = StyleUtil.createFont(this.workbook, color, fontSize, fontName);
return setFont(font, ignoreHead); return setFont(font, ignoreHead);
} }
@@ -213,7 +240,7 @@ public class StyleSet implements Serializable {
* @return this * @return this
* @since 4.1.0 * @since 4.1.0
*/ */
public StyleSet setFont(final Font font, final boolean ignoreHead) { public DefaultStyleSet setFont(final Font font, final boolean ignoreHead) {
if (!ignoreHead) { if (!ignoreHead) {
this.headCellStyle.setFont(font); this.headCellStyle.setFont(font);
} }
@@ -230,52 +257,11 @@ public class StyleSet implements Serializable {
* @return this * @return this
* @since 4.5.16 * @since 4.5.16
*/ */
public StyleSet setWrapText() { public DefaultStyleSet setWrapText() {
this.cellStyle.setWrapText(true); this.cellStyle.setWrapText(true);
this.cellStyleForNumber.setWrapText(true); this.cellStyleForNumber.setWrapText(true);
this.cellStyleForDate.setWrapText(true); this.cellStyleForDate.setWrapText(true);
this.cellStyleForHyperlink.setWrapText(true); this.cellStyleForHyperlink.setWrapText(true);
return this; return this;
} }
/**
* 获取值对应的公共单元格样式
*
* @param value
* @param isHeader 是否为标题单元格
* @return 值对应单元格样式
* @since 5.7.16
*/
public CellStyle getStyleByValueType(final Object value, final boolean isHeader) {
CellStyle style = null;
if (isHeader && null != this.headCellStyle) {
style = headCellStyle;
} else if (null != cellStyle) {
style = cellStyle;
}
if (value instanceof Date
|| value instanceof TemporalAccessor
|| value instanceof Calendar) {
// 日期单独定义格式
if (null != this.cellStyleForDate) {
style = this.cellStyleForDate;
}
} else if (value instanceof Number) {
// 数字单独定义格式
if ((value instanceof Double || value instanceof Float || value instanceof BigDecimal) &&
null != this.cellStyleForNumber) {
style = this.cellStyleForNumber;
}
} else if (value instanceof Hyperlink) {
// 自定义超链接样式
if (null != this.cellStyleForHyperlink) {
style = this.cellStyleForHyperlink;
}
}
return style;
}
} }

View File

@@ -15,6 +15,26 @@ package org.dromara.hutool.poi.excel.style;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
/**
* 样式集合接口<br>
* 通过自定义样式集合,可根据单元格不同、值的不同设置不同的样式
*
* @author Looly
* @since 6.0.0
*/
public interface StyleSet { public interface StyleSet {
CellStyle getStyleFor(CellReference reference, Object cellValue);
/**
* 获取单元格样式,可以:
* <ul>
* <li>根据单元格位置获取定义不同的样式,如首行、首列、偶数行、偶数列等</li>
* <li>根据单元格值获取定义不同的样式,如数字、日期等,也可根据是否为标题行定义独立的样式</li>
* </ul>
*
* @param reference 单元格引用,包含单元格位置等信息
* @param cellValue 单元格值
* @param isHeader 是否为表头,扁头定义的特殊样式
* @return 单元格样式
*/
CellStyle getStyleFor(CellReference reference, Object cellValue, boolean isHeader);
} }

View File

@@ -26,6 +26,7 @@ import org.dromara.hutool.poi.excel.BigExcelWriter;
import org.dromara.hutool.poi.excel.ExcelUtil; import org.dromara.hutool.poi.excel.ExcelUtil;
import org.dromara.hutool.poi.excel.ExcelWriter; import org.dromara.hutool.poi.excel.ExcelWriter;
import org.dromara.hutool.poi.excel.TestBean; import org.dromara.hutool.poi.excel.TestBean;
import org.dromara.hutool.poi.excel.style.DefaultStyleSet;
import org.dromara.hutool.poi.excel.style.StyleUtil; import org.dromara.hutool.poi.excel.style.StyleUtil;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -92,7 +93,7 @@ public class BigExcelWriteTest {
// 通过工具类创建writer // 通过工具类创建writer
final BigExcelWriter writer = ExcelUtil.getBigWriter("e:/mergeTest.xlsx"); final BigExcelWriter writer = ExcelUtil.getBigWriter("e:/mergeTest.xlsx");
final CellStyle style = writer.getStyleSet().getHeadCellStyle(); final CellStyle style = ((DefaultStyleSet)writer.getStyleSet()).getHeadCellStyle();
StyleUtil.setColor(style, IndexedColors.RED, FillPatternType.SOLID_FOREGROUND); StyleUtil.setColor(style, IndexedColors.RED, FillPatternType.SOLID_FOREGROUND);
// 跳过当前行,即第一行,非必须,在此演示用 // 跳过当前行,即第一行,非必须,在此演示用
@@ -138,7 +139,7 @@ public class BigExcelWriteTest {
font.setBold(true); font.setBold(true);
font.setColor(Font.COLOR_RED); font.setColor(Font.COLOR_RED);
font.setItalic(true); font.setItalic(true);
writer.getStyleSet().setFont(font, true); ((DefaultStyleSet)writer.getStyleSet()).setFont(font, true);
// 合并单元格后的标题行,使用默认标题样式 // 合并单元格后的标题行,使用默认标题样式
writer.merge(row1.size() - 1, "一班成绩单"); writer.merge(row1.size() - 1, "一班成绩单");

View File

@@ -23,6 +23,7 @@ import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.poi.excel.*; import org.dromara.hutool.poi.excel.*;
import org.dromara.hutool.poi.excel.cell.setters.EscapeStrCellSetter; import org.dromara.hutool.poi.excel.cell.setters.EscapeStrCellSetter;
import org.dromara.hutool.poi.excel.style.DefaultStyleSet;
import org.dromara.hutool.poi.excel.style.StyleUtil; import org.dromara.hutool.poi.excel.style.StyleUtil;
import org.apache.poi.common.usermodel.HyperlinkType; import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
@@ -177,7 +178,7 @@ public class ExcelWriteTest {
// 通过工具类创建writer // 通过工具类创建writer
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/mergeTest.xlsx"); final ExcelWriter writer = ExcelUtil.getWriter("d:/test/mergeTest.xlsx");
final CellStyle style = writer.getStyleSet().getHeadCellStyle(); final CellStyle style = ((DefaultStyleSet)writer.getStyleSet()).getHeadCellStyle();
StyleUtil.setColor(style, IndexedColors.RED, FillPatternType.SOLID_FOREGROUND); StyleUtil.setColor(style, IndexedColors.RED, FillPatternType.SOLID_FOREGROUND);
// 跳过当前行,即第一行,非必须,在此演示用 // 跳过当前行,即第一行,非必须,在此演示用
@@ -251,7 +252,7 @@ public class ExcelWriteTest {
font.setBold(true); font.setBold(true);
font.setColor(Font.COLOR_RED); font.setColor(Font.COLOR_RED);
font.setItalic(true); font.setItalic(true);
writer.getStyleSet().setFont(font, true); ((DefaultStyleSet)writer.getStyleSet()).setFont(font, true);
// 合并单元格后的标题行,使用默认标题样式 // 合并单元格后的标题行,使用默认标题样式
writer.merge(row1.size() - 1, "一班成绩单"); writer.merge(row1.size() - 1, "一班成绩单");
@@ -617,7 +618,7 @@ public class ExcelWriteTest {
headFont.setBold(true); headFont.setBold(true);
headFont.setFontHeightInPoints((short) 50); headFont.setFontHeightInPoints((short) 50);
headFont.setFontName("Microsoft YaHei"); headFont.setFontName("Microsoft YaHei");
writer.getStyleSet().getHeadCellStyle().setFont(headFont); ((DefaultStyleSet)writer.getStyleSet()).getHeadCellStyle().setFont(headFont);
//表2 //表2
writer.setSheet("表2"); writer.setSheet("表2");
@@ -799,7 +800,7 @@ public class ExcelWriteTest {
public void changeHeaderStyleTest() { public void changeHeaderStyleTest() {
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/headerStyle.xlsx"); final ExcelWriter writer = ExcelUtil.getWriter("d:/test/headerStyle.xlsx");
writer.writeHeadRow(ListUtil.view("姓名", "性别", "年龄")); writer.writeHeadRow(ListUtil.view("姓名", "性别", "年龄"));
final CellStyle headCellStyle = writer.getStyleSet().getHeadCellStyle(); final CellStyle headCellStyle = ((DefaultStyleSet)writer.getStyleSet()).getHeadCellStyle();
headCellStyle.setFillForegroundColor(IndexedColors.YELLOW1.index); headCellStyle.setFillForegroundColor(IndexedColors.YELLOW1.index);
headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

View File

@@ -12,15 +12,16 @@
package org.dromara.hutool.poi.excel.writer; package org.dromara.hutool.poi.excel.writer;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.poi.excel.ExcelUtil; import org.dromara.hutool.poi.excel.ExcelUtil;
import org.dromara.hutool.poi.excel.ExcelWriter; import org.dromara.hutool.poi.excel.ExcelWriter;
import org.dromara.hutool.poi.excel.style.DefaultStyleSet;
import org.dromara.hutool.poi.excel.style.StyleUtil; import org.dromara.hutool.poi.excel.style.StyleUtil;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -58,17 +59,17 @@ public class Issue2221Test {
final ExcelWriter writer = ExcelUtil.getBigWriter("d:/test/duplicateAlias2.xlsx"); final ExcelWriter writer = ExcelUtil.getBigWriter("d:/test/duplicateAlias2.xlsx");
// 设置头部的背景颜色 // 设置头部的背景颜色
StyleUtil.setColor(writer.getHeadCellStyle(), IndexedColors.GREY_50_PERCENT, FillPatternType.SOLID_FOREGROUND); StyleUtil.setColor(((DefaultStyleSet)writer.getStyleSet()).getHeadCellStyle(), IndexedColors.GREY_50_PERCENT, FillPatternType.SOLID_FOREGROUND);
//设置全局字体 //设置全局字体
final Font font = writer.createFont(); final Font font = writer.createFont();
font.setFontName("Microsoft YaHei"); font.setFontName("Microsoft YaHei");
writer.getStyleSet().setFont(font, false); ((DefaultStyleSet)writer.getStyleSet()).setFont(font, false);
// 设置头部的字体为白颜色 // 设置头部的字体为白颜色
final Font headerFont = writer.createFont(); final Font headerFont = writer.createFont();
headerFont.setColor(IndexedColors.WHITE.getIndex()); headerFont.setColor(IndexedColors.WHITE.getIndex());
writer.getHeadCellStyle().setFont(headerFont); ((DefaultStyleSet)writer.getStyleSet()).getHeadCellStyle().setFont(headerFont);
// 跳过多少行 // 跳过多少行
writer.passRows(1); writer.passRows(1);

View File

@@ -20,7 +20,7 @@ import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Console; import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.poi.excel.ExcelUtil; import org.dromara.hutool.poi.excel.ExcelUtil;
import org.dromara.hutool.poi.excel.ExcelWriter; import org.dromara.hutool.poi.excel.ExcelWriter;
import org.dromara.hutool.poi.excel.StyleSet; import org.dromara.hutool.poi.excel.style.DefaultStyleSet;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -52,7 +52,7 @@ public class Issue2307Test {
writer.write(rows, true); writer.write(rows, true);
final StyleSet style = writer.getStyleSet(); final DefaultStyleSet style = (DefaultStyleSet) writer.getStyleSet();
final CellStyle cellStyleForNumber = style.getCellStyleForNumber(); final CellStyle cellStyleForNumber = style.getCellStyleForNumber();
cellStyleForNumber.setAlignment(HorizontalAlignment.RIGHT); cellStyleForNumber.setAlignment(HorizontalAlignment.RIGHT);