mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-29 01:47:07 +08:00
add DefaultStyleSet
This commit is contained in:
parent
b8cfaecfc0
commit
9a34a155c6
@ -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.CellUtil;
|
||||
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.IOException;
|
||||
@ -184,7 +186,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
*/
|
||||
public ExcelWriter(final Sheet sheet) {
|
||||
super(sheet);
|
||||
this.styleSet = new StyleSet(workbook);
|
||||
this.styleSet = new DefaultStyleSet(workbook);
|
||||
this.currentRow = new AtomicInteger(0);
|
||||
}
|
||||
// endregion
|
||||
@ -338,27 +340,6 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
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 合并到的最后一个列号
|
||||
* @return this
|
||||
@ -656,8 +636,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 合并当前行的单元格,并写入对象到单元格<br>
|
||||
* 如果写到单元格中的内容非null,行号自动+1,否则当前行号不变<br>
|
||||
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
|
||||
* 如果写到单元格中的内容非null,行号自动+1,否则当前行号不变
|
||||
*
|
||||
* @param lastColumn 合并到的最后一个列号
|
||||
* @param content 合并单元格后的内容
|
||||
@ -669,8 +648,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 合并某行的单元格,并写入对象到单元格<br>
|
||||
* 如果写到单元格中的内容非null,行号自动+1,否则当前行号不变<br>
|
||||
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
|
||||
* 如果写到单元格中的内容非null,行号自动+1,否则当前行号不变
|
||||
*
|
||||
* @param lastColumn 合并到的最后一个列号
|
||||
* @param content 合并单元格后的内容
|
||||
@ -692,8 +670,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并某行的单元格,并写入对象到单元格<br>
|
||||
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
|
||||
* 合并某行的单元格,并写入对象到单元格
|
||||
*
|
||||
* @param cellRangeAddress 合并单元格范围,定义了起始行列和结束行列
|
||||
* @param content 合并单元格后的内容
|
||||
@ -706,7 +683,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
CellStyle style = null;
|
||||
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);
|
||||
@ -737,8 +714,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加<br>
|
||||
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加
|
||||
* 默认的,当当前行号为0时,写出标题(如果为Map或Bean),否则不写标题
|
||||
*
|
||||
* <p>
|
||||
@ -760,8 +736,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加<br>
|
||||
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加
|
||||
*
|
||||
* <p>
|
||||
* data中元素支持的类型有:
|
||||
@ -791,8 +766,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加<br>
|
||||
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加
|
||||
* data中元素支持的类型有:
|
||||
*
|
||||
* <p>
|
||||
@ -931,8 +905,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 写出一行标题数据<br>
|
||||
* 本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br>
|
||||
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
|
||||
*
|
||||
* @param rowData 一行的数据
|
||||
* @return this
|
||||
@ -955,8 +928,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 写出复杂标题的第二行标题数据<br>
|
||||
* 本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br>
|
||||
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
|
||||
*
|
||||
* <p>
|
||||
* 此方法的逻辑是:将一行数据写出到当前行,遇到已存在的单元格跳过,不存在的创建并赋值。
|
||||
@ -1085,8 +1057,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 写出一行数据<br>
|
||||
* 本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br>
|
||||
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
|
||||
*
|
||||
* @param rowData 一行的数据
|
||||
* @return this
|
||||
@ -1103,8 +1074,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 从第1列开始按列写入数据(index 从0开始)<br>
|
||||
* 本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br>
|
||||
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
|
||||
*
|
||||
* @param colMap 一列的数据
|
||||
* @param isWriteKeyAsHead 是否将Map的Key作为表头输出,如果为True第一行为表头,紧接着为values
|
||||
@ -1117,8 +1087,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 从指定列开始按列写入数据(index 从0开始)<br>
|
||||
* 本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br>
|
||||
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
|
||||
*
|
||||
* @param colMap 一列的数据
|
||||
* @param startColIndex 起始的列号,从0开始
|
||||
@ -1140,8 +1109,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 为第一列写入数据<br>
|
||||
* 本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br>
|
||||
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
|
||||
*
|
||||
* @param headerVal 表头名称,如果为null则不写入
|
||||
* @param colData 需要写入的列数据
|
||||
@ -1155,8 +1123,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 为第指定列写入数据<br>
|
||||
* 本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br>
|
||||
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
|
||||
*
|
||||
* @param headerVal 表头名称,如果为null则不写入
|
||||
* @param colIndex 列index
|
||||
|
@ -21,6 +21,7 @@ import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.ss.util.CellRangeUtil;
|
||||
import org.dromara.hutool.poi.excel.style.StyleSet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -18,10 +18,10 @@ import org.apache.poi.ss.util.CellReference;
|
||||
import org.apache.poi.ss.util.RegionUtil;
|
||||
import org.apache.poi.ss.util.SheetUtil;
|
||||
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.setters.CellSetterFactory;
|
||||
import org.dromara.hutool.poi.excel.cell.values.CompositeCellValue;
|
||||
import org.dromara.hutool.poi.excel.style.StyleSet;
|
||||
|
||||
/**
|
||||
* Excel表格中单元格工具类
|
||||
@ -114,7 +114,7 @@ public class CellUtil {
|
||||
|
||||
CellStyle cellStyle = null;
|
||||
if (null != styleSet) {
|
||||
cellStyle = styleSet.getStyleByValueType(value, isHeader);
|
||||
cellStyle = styleSet.getStyleFor(new CellReference(cell), value, isHeader);
|
||||
}
|
||||
|
||||
setCellValue(cell, value, cellStyle, cellEditor);
|
||||
|
@ -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.
|
||||
* 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:
|
||||
@ -10,19 +10,11 @@
|
||||
* 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.ss.usermodel.BorderStyle;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
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 org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.util.CellReference;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
@ -31,11 +23,12 @@ import java.util.Calendar;
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -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 工作簿
|
||||
*/
|
||||
public StyleSet(final Workbook workbook) {
|
||||
public DefaultStyleSet(final Workbook workbook) {
|
||||
this.workbook = workbook;
|
||||
this.headCellStyle = StyleUtil.createHeadCellStyle(workbook);
|
||||
this.cellStyle = StyleUtil.createDefaultCellStyle(workbook);
|
||||
@ -86,12 +79,46 @@ public class StyleSet implements Serializable {
|
||||
|
||||
// 默认链接样式
|
||||
this.cellStyleForHyperlink = StyleUtil.cloneCellStyle(workbook, this.cellStyle);
|
||||
final Font font = this.workbook.createFont();
|
||||
final Font font = workbook.createFont();
|
||||
font.setUnderline((byte) 1);
|
||||
font.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
|
||||
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
|
||||
* @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.cellStyle, borderSize, colorIndex);
|
||||
StyleUtil.setBorder(this.cellStyleForNumber, borderSize, colorIndex);
|
||||
@ -163,7 +190,7 @@ public class StyleSet implements Serializable {
|
||||
* @return this
|
||||
* @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.cellStyle, halign, valign);
|
||||
StyleUtil.setAlign(this.cellStyleForNumber, halign, valign);
|
||||
@ -180,7 +207,7 @@ public class StyleSet implements Serializable {
|
||||
* @return this
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public StyleSet setBackgroundColor(final IndexedColors backgroundColor, final boolean withHeadCell) {
|
||||
public DefaultStyleSet setBackgroundColor(final IndexedColors backgroundColor, final boolean withHeadCell) {
|
||||
if (withHeadCell) {
|
||||
StyleUtil.setColor(this.headCellStyle, backgroundColor, FillPatternType.SOLID_FOREGROUND);
|
||||
}
|
||||
@ -200,7 +227,7 @@ public class StyleSet implements Serializable {
|
||||
* @param ignoreHead 是否跳过头部样式
|
||||
* @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);
|
||||
return setFont(font, ignoreHead);
|
||||
}
|
||||
@ -213,7 +240,7 @@ public class StyleSet implements Serializable {
|
||||
* @return this
|
||||
* @since 4.1.0
|
||||
*/
|
||||
public StyleSet setFont(final Font font, final boolean ignoreHead) {
|
||||
public DefaultStyleSet setFont(final Font font, final boolean ignoreHead) {
|
||||
if (!ignoreHead) {
|
||||
this.headCellStyle.setFont(font);
|
||||
}
|
||||
@ -230,52 +257,11 @@ public class StyleSet implements Serializable {
|
||||
* @return this
|
||||
* @since 4.5.16
|
||||
*/
|
||||
public StyleSet setWrapText() {
|
||||
public DefaultStyleSet setWrapText() {
|
||||
this.cellStyle.setWrapText(true);
|
||||
this.cellStyleForNumber.setWrapText(true);
|
||||
this.cellStyleForDate.setWrapText(true);
|
||||
this.cellStyleForHyperlink.setWrapText(true);
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -15,6 +15,26 @@ package org.dromara.hutool.poi.excel.style;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.util.CellReference;
|
||||
|
||||
/**
|
||||
* 样式集合接口<br>
|
||||
* 通过自定义样式集合,可根据单元格不同、值的不同设置不同的样式
|
||||
*
|
||||
* @author Looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ import org.dromara.hutool.poi.excel.BigExcelWriter;
|
||||
import org.dromara.hutool.poi.excel.ExcelUtil;
|
||||
import org.dromara.hutool.poi.excel.ExcelWriter;
|
||||
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.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -92,7 +93,7 @@ public class BigExcelWriteTest {
|
||||
|
||||
// 通过工具类创建writer
|
||||
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);
|
||||
|
||||
// 跳过当前行,即第一行,非必须,在此演示用
|
||||
@ -138,7 +139,7 @@ public class BigExcelWriteTest {
|
||||
font.setBold(true);
|
||||
font.setColor(Font.COLOR_RED);
|
||||
font.setItalic(true);
|
||||
writer.getStyleSet().setFont(font, true);
|
||||
((DefaultStyleSet)writer.getStyleSet()).setFont(font, true);
|
||||
|
||||
// 合并单元格后的标题行,使用默认标题样式
|
||||
writer.merge(row1.size() - 1, "一班成绩单");
|
||||
|
@ -23,6 +23,7 @@ import org.dromara.hutool.core.util.CharsetUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.poi.excel.*;
|
||||
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.apache.poi.common.usermodel.HyperlinkType;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
@ -177,7 +178,7 @@ public class ExcelWriteTest {
|
||||
|
||||
// 通过工具类创建writer
|
||||
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);
|
||||
|
||||
// 跳过当前行,即第一行,非必须,在此演示用
|
||||
@ -251,7 +252,7 @@ public class ExcelWriteTest {
|
||||
font.setBold(true);
|
||||
font.setColor(Font.COLOR_RED);
|
||||
font.setItalic(true);
|
||||
writer.getStyleSet().setFont(font, true);
|
||||
((DefaultStyleSet)writer.getStyleSet()).setFont(font, true);
|
||||
|
||||
// 合并单元格后的标题行,使用默认标题样式
|
||||
writer.merge(row1.size() - 1, "一班成绩单");
|
||||
@ -617,7 +618,7 @@ public class ExcelWriteTest {
|
||||
headFont.setBold(true);
|
||||
headFont.setFontHeightInPoints((short) 50);
|
||||
headFont.setFontName("Microsoft YaHei");
|
||||
writer.getStyleSet().getHeadCellStyle().setFont(headFont);
|
||||
((DefaultStyleSet)writer.getStyleSet()).getHeadCellStyle().setFont(headFont);
|
||||
|
||||
//表2
|
||||
writer.setSheet("表2");
|
||||
@ -799,7 +800,7 @@ public class ExcelWriteTest {
|
||||
public void changeHeaderStyleTest() {
|
||||
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/headerStyle.xlsx");
|
||||
writer.writeHeadRow(ListUtil.view("姓名", "性别", "年龄"));
|
||||
final CellStyle headCellStyle = writer.getStyleSet().getHeadCellStyle();
|
||||
final CellStyle headCellStyle = ((DefaultStyleSet)writer.getStyleSet()).getHeadCellStyle();
|
||||
headCellStyle.setFillForegroundColor(IndexedColors.YELLOW1.index);
|
||||
headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
|
||||
|
@ -12,15 +12,16 @@
|
||||
|
||||
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.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.poi.excel.ExcelUtil;
|
||||
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.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.Test;
|
||||
|
||||
@ -58,17 +59,17 @@ public class Issue2221Test {
|
||||
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();
|
||||
font.setFontName("Microsoft YaHei");
|
||||
writer.getStyleSet().setFont(font, false);
|
||||
((DefaultStyleSet)writer.getStyleSet()).setFont(font, false);
|
||||
|
||||
// 设置头部的字体为白颜色
|
||||
final Font headerFont = writer.createFont();
|
||||
headerFont.setColor(IndexedColors.WHITE.getIndex());
|
||||
writer.getHeadCellStyle().setFont(headerFont);
|
||||
((DefaultStyleSet)writer.getStyleSet()).getHeadCellStyle().setFont(headerFont);
|
||||
|
||||
// 跳过多少行
|
||||
writer.passRows(1);
|
||||
|
@ -20,7 +20,7 @@ import org.dromara.hutool.core.io.file.FileUtil;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.poi.excel.ExcelUtil;
|
||||
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.Test;
|
||||
|
||||
@ -52,7 +52,7 @@ public class Issue2307Test {
|
||||
writer.write(rows, true);
|
||||
|
||||
|
||||
final StyleSet style = writer.getStyleSet();
|
||||
final DefaultStyleSet style = (DefaultStyleSet) writer.getStyleSet();
|
||||
final CellStyle cellStyleForNumber = style.getCellStyleForNumber();
|
||||
cellStyleForNumber.setAlignment(HorizontalAlignment.RIGHT);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user