mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add CellBorderStyle
This commit is contained in:
parent
cb72dacc02
commit
35f3383a1c
@ -29,6 +29,7 @@ import java.io.Serializable;
|
||||
import java.lang.reflect.Array;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@ -339,7 +340,20 @@ public class ObjUtil {
|
||||
* @return 映射函数的结果, 如果输入对象为 null,则返回 null
|
||||
*/
|
||||
public static <T, R> R apply(final T source, final Function<T, R> handler) {
|
||||
return defaultIfNull(source, handler, (R)null);
|
||||
return defaultIfNull(source, handler, (R) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果指定的对象不为 {@code null},则执行{@link Consumer}处理source,否则不进行操作
|
||||
*
|
||||
* @param source 要检查的对象
|
||||
* @param consumer source处理逻辑
|
||||
* @param <T> 输入对象的类型
|
||||
*/
|
||||
public static <T> void accept(final T source, final Consumer<T> consumer) {
|
||||
if (null != source) {
|
||||
consumer.accept(source);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,240 @@
|
||||
/*
|
||||
* 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:
|
||||
* https://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.poi.excel.style;
|
||||
|
||||
import org.apache.poi.ss.usermodel.BorderStyle;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 单元格边框样式和颜色封装,边框按照“上右下左”的顺序定义,与CSS一致
|
||||
*
|
||||
* @author Looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class CellBorderStyle implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 创建单元格边框样式对象,四边框样式保持一致。
|
||||
*
|
||||
* @param borderStyle 边框样式
|
||||
* @param colorIndex 颜色
|
||||
* @return 单元格边框样式对象
|
||||
*/
|
||||
public static CellBorderStyle of(final BorderStyle borderStyle, final IndexedColors colorIndex) {
|
||||
return new CellBorderStyle()
|
||||
.setTopStyle(borderStyle)
|
||||
.setTopColor(colorIndex.getIndex())
|
||||
.setRightStyle(borderStyle)
|
||||
.setRightColor(colorIndex.getIndex())
|
||||
.setBottomStyle(borderStyle)
|
||||
.setBottomColor(colorIndex.getIndex())
|
||||
.setLeftStyle(borderStyle)
|
||||
.setLeftColor(colorIndex.getIndex());
|
||||
}
|
||||
|
||||
private BorderStyle topStyle;
|
||||
private Short topColor;
|
||||
private BorderStyle rightStyle;
|
||||
private Short rightColor;
|
||||
private BorderStyle bottomStyle;
|
||||
private Short bottomColor;
|
||||
private BorderStyle leftStyle;
|
||||
private Short leftColor;
|
||||
|
||||
/**
|
||||
* 获取上边框的样式。
|
||||
*
|
||||
* @return 上边框的样式。
|
||||
*/
|
||||
public BorderStyle getTopStyle() {
|
||||
return topStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上边框的样式。
|
||||
*
|
||||
* @param topStyle 上边框的样式。
|
||||
* @return 当前的单元格边框样式对象,支持链式调用。
|
||||
*/
|
||||
public CellBorderStyle setTopStyle(final BorderStyle topStyle) {
|
||||
this.topStyle = topStyle;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上边框的颜色。
|
||||
*
|
||||
* @return 上边框的颜色。
|
||||
*/
|
||||
public Short getTopColor() {
|
||||
return topColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上边框的颜色。
|
||||
*
|
||||
* @param topColor 上边框的颜色。
|
||||
* @return 当前的单元格边框样式对象,支持链式调用。
|
||||
*/
|
||||
public CellBorderStyle setTopColor(final Short topColor) {
|
||||
this.topColor = topColor;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取右边框的样式。
|
||||
*
|
||||
* @return 右边框的样式。
|
||||
*/
|
||||
public BorderStyle getRightStyle() {
|
||||
return rightStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置右边框的样式。
|
||||
*
|
||||
* @param rightStyle 右边框的样式。
|
||||
* @return 当前的单元格边框样式对象,支持链式调用。
|
||||
*/
|
||||
public CellBorderStyle setRightStyle(final BorderStyle rightStyle) {
|
||||
this.rightStyle = rightStyle;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取右边框的颜色。
|
||||
*
|
||||
* @return 右边框的颜色。
|
||||
*/
|
||||
public Short getRightColor() {
|
||||
return rightColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置右边框的颜色。
|
||||
*
|
||||
* @param rightColor 右边框的颜色。
|
||||
* @return 当前的单元格边框样式对象,支持链式调用。
|
||||
*/
|
||||
public CellBorderStyle setRightColor(final Short rightColor) {
|
||||
this.rightColor = rightColor;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取底边框的样式。
|
||||
*
|
||||
* @return 底边框的样式。
|
||||
*/
|
||||
public BorderStyle getBottomStyle() {
|
||||
return bottomStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置底边框的样式。
|
||||
*
|
||||
* @param bottomStyle 底边框的样式。
|
||||
* @return 当前的单元格边框样式对象,支持链式调用。
|
||||
*/
|
||||
public CellBorderStyle setBottomStyle(final BorderStyle bottomStyle) {
|
||||
this.bottomStyle = bottomStyle;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取底边框的颜色。
|
||||
*
|
||||
* @return 底边框的颜色。
|
||||
*/
|
||||
public Short getBottomColor() {
|
||||
return bottomColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置底边框的颜色。
|
||||
*
|
||||
* @param bottomColor 底边框的颜色。
|
||||
* @return 当前的单元格边框样式对象,支持链式调用。
|
||||
*/
|
||||
public CellBorderStyle setBottomColor(final Short bottomColor) {
|
||||
this.bottomColor = bottomColor;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取左边框的样式。
|
||||
*
|
||||
* @return 左边框的样式。
|
||||
*/
|
||||
public BorderStyle getLeftStyle() {
|
||||
return leftStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置左边框的样式。
|
||||
*
|
||||
* @param leftStyle 左边框的样式。
|
||||
* @return 当前的单元格边框样式对象,支持链式调用。
|
||||
*/
|
||||
public CellBorderStyle setLeftStyle(final BorderStyle leftStyle) {
|
||||
this.leftStyle = leftStyle;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取左边框的颜色。
|
||||
*
|
||||
* @return 左边框的颜色。
|
||||
*/
|
||||
public Short getLeftColor() {
|
||||
return leftColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置左边框的颜色。
|
||||
*
|
||||
* @param leftColor 左边框的颜色。
|
||||
* @return 当前的单元格边框样式对象,支持链式调用。
|
||||
*/
|
||||
public CellBorderStyle setLeftColor(final Short leftColor) {
|
||||
this.leftColor = leftColor;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将边框样式和颜色设置到CellStyle中
|
||||
*
|
||||
* @param cellStyle CellStyle
|
||||
* @return CellStyle
|
||||
*/
|
||||
public CellStyle setTo(final CellStyle cellStyle){
|
||||
ObjUtil.accept(this.topStyle, cellStyle::setBorderTop);
|
||||
ObjUtil.accept(this.topColor, cellStyle::setTopBorderColor);
|
||||
|
||||
ObjUtil.accept(this.rightStyle, cellStyle::setBorderRight);
|
||||
ObjUtil.accept(this.rightColor, cellStyle::setRightBorderColor);
|
||||
|
||||
ObjUtil.accept(this.bottomStyle, cellStyle::setBorderBottom);
|
||||
ObjUtil.accept(this.bottomColor, cellStyle::setBottomBorderColor);
|
||||
|
||||
ObjUtil.accept(this.leftStyle, cellStyle::setBorderLeft);
|
||||
ObjUtil.accept(this.leftColor, cellStyle::setLeftBorderColor);
|
||||
|
||||
return cellStyle;
|
||||
}
|
||||
}
|
@ -12,17 +12,11 @@
|
||||
|
||||
package org.dromara.hutool.poi.excel.style;
|
||||
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
||||
import org.apache.poi.xssf.usermodel.XSSFColor;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.apache.poi.ss.usermodel.BorderStyle;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.DataFormat;
|
||||
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.IndexedColors;
|
||||
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
|
||||
/**
|
||||
* Excel样式工具类
|
||||
@ -32,129 +26,7 @@ import org.apache.poi.ss.usermodel.Workbook;
|
||||
*/
|
||||
public class StyleUtil {
|
||||
|
||||
/**
|
||||
* 克隆新的{@link CellStyle}
|
||||
*
|
||||
* @param cell 单元格
|
||||
* @param cellStyle 被复制的样式
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle cloneCellStyle(final Cell cell, final CellStyle cellStyle) {
|
||||
return cloneCellStyle(cell.getSheet().getWorkbook(), cellStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* 克隆新的{@link CellStyle}
|
||||
*
|
||||
* @param workbook 工作簿
|
||||
* @param cellStyle 被复制的样式
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle cloneCellStyle(final Workbook workbook, final CellStyle cellStyle) {
|
||||
final CellStyle newCellStyle = createCellStyle(workbook);
|
||||
newCellStyle.cloneStyleFrom(cellStyle);
|
||||
return newCellStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置cell文本对齐样式
|
||||
*
|
||||
* @param cellStyle {@link CellStyle}
|
||||
* @param halign 横向位置
|
||||
* @param valign 纵向位置
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle setAlign(final CellStyle cellStyle, final HorizontalAlignment halign, final VerticalAlignment valign) {
|
||||
cellStyle.setAlignment(halign);
|
||||
cellStyle.setVerticalAlignment(valign);
|
||||
return cellStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置cell的四个边框粗细和颜色
|
||||
*
|
||||
* @param cellStyle {@link CellStyle}
|
||||
* @param borderSize 边框粗细{@link BorderStyle}枚举
|
||||
* @param colorIndex 颜色的short值
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle setBorder(final CellStyle cellStyle, final BorderStyle borderSize, final IndexedColors colorIndex) {
|
||||
cellStyle.setBorderBottom(borderSize);
|
||||
cellStyle.setBottomBorderColor(colorIndex.index);
|
||||
|
||||
cellStyle.setBorderLeft(borderSize);
|
||||
cellStyle.setLeftBorderColor(colorIndex.index);
|
||||
|
||||
cellStyle.setBorderRight(borderSize);
|
||||
cellStyle.setRightBorderColor(colorIndex.index);
|
||||
|
||||
cellStyle.setBorderTop(borderSize);
|
||||
cellStyle.setTopBorderColor(colorIndex.index);
|
||||
|
||||
return cellStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给cell设置颜色
|
||||
*
|
||||
* @param cellStyle {@link CellStyle}
|
||||
* @param color 背景颜色
|
||||
* @param fillPattern 填充方式 {@link FillPatternType}枚举
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle setColor(final CellStyle cellStyle, final IndexedColors color, final FillPatternType fillPattern) {
|
||||
return setColor(cellStyle, color.index, fillPattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* 给cell设置颜色
|
||||
*
|
||||
* @param cellStyle {@link CellStyle}
|
||||
* @param color 背景颜色
|
||||
* @param fillPattern 填充方式 {@link FillPatternType}枚举
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle setColor(final CellStyle cellStyle, final short color, final FillPatternType fillPattern) {
|
||||
cellStyle.setFillForegroundColor(color);
|
||||
cellStyle.setFillPattern(fillPattern);
|
||||
return cellStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建字体
|
||||
*
|
||||
* @param workbook {@link Workbook}
|
||||
* @param color 字体颜色
|
||||
* @param fontSize 字体大小
|
||||
* @param fontName 字体名称,可以为null使用默认字体
|
||||
* @return {@link Font}
|
||||
*/
|
||||
public static Font createFont(final Workbook workbook, final short color, final short fontSize, final String fontName) {
|
||||
final Font font = workbook.createFont();
|
||||
return setFontStyle(font, color, fontSize, fontName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字体样式
|
||||
*
|
||||
* @param font 字体{@link Font}
|
||||
* @param color 字体颜色
|
||||
* @param fontSize 字体大小
|
||||
* @param fontName 字体名称,可以为null使用默认字体
|
||||
* @return {@link Font}
|
||||
*/
|
||||
public static Font setFontStyle(final Font font, final short color, final short fontSize, final String fontName) {
|
||||
if (color > 0) {
|
||||
font.setColor(color);
|
||||
}
|
||||
if (fontSize > 0) {
|
||||
font.setFontHeightInPoints(fontSize);
|
||||
}
|
||||
if (StrUtil.isNotBlank(fontName)) {
|
||||
font.setFontName(fontName);
|
||||
}
|
||||
return font;
|
||||
}
|
||||
// region ----- create or clone style
|
||||
|
||||
/**
|
||||
* 创建单元格样式
|
||||
@ -215,6 +87,155 @@ public class StyleUtil {
|
||||
return (null == style) || style.equals(workbook.getCellStyleAt(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* 克隆新的{@link CellStyle}
|
||||
*
|
||||
* @param cell 单元格
|
||||
* @param cellStyle 被复制的样式
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle cloneCellStyle(final Cell cell, final CellStyle cellStyle) {
|
||||
return cloneCellStyle(cell.getSheet().getWorkbook(), cellStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* 克隆新的{@link CellStyle}
|
||||
*
|
||||
* @param workbook 工作簿
|
||||
* @param cellStyle 被复制的样式
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle cloneCellStyle(final Workbook workbook, final CellStyle cellStyle) {
|
||||
final CellStyle newCellStyle = createCellStyle(workbook);
|
||||
newCellStyle.cloneStyleFrom(cellStyle);
|
||||
return newCellStyle;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 设置cell文本对齐样式
|
||||
*
|
||||
* @param cellStyle {@link CellStyle}
|
||||
* @param halign 横向位置
|
||||
* @param valign 纵向位置
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle setAlign(final CellStyle cellStyle, final HorizontalAlignment halign, final VerticalAlignment valign) {
|
||||
cellStyle.setAlignment(halign);
|
||||
cellStyle.setVerticalAlignment(valign);
|
||||
return cellStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置cell的四个边框粗细和颜色
|
||||
*
|
||||
* @param cellStyle {@link CellStyle}
|
||||
* @param borderSize 边框粗细{@link BorderStyle}枚举
|
||||
* @param colorIndex 预定义颜色的short值,见{@link IndexedColors}枚举
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle setBorder(final CellStyle cellStyle, final BorderStyle borderSize, final IndexedColors colorIndex) {
|
||||
return setBorder(cellStyle, CellBorderStyle.of(borderSize, colorIndex));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置cell的四个边框粗细和颜色
|
||||
*
|
||||
* @param cellStyle {@link CellStyle}
|
||||
* @param cellBorderStyle {@link CellBorderStyle}单元格边框样式和颜色
|
||||
* }
|
||||
* @return {@link CellStyle}
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static CellStyle setBorder(final CellStyle cellStyle, final CellBorderStyle cellBorderStyle) {
|
||||
return cellBorderStyle.setTo(cellStyle);
|
||||
}
|
||||
|
||||
// region ----- color
|
||||
|
||||
/**
|
||||
* 给cell设置颜色
|
||||
*
|
||||
* @param cellStyle {@link CellStyle}
|
||||
* @param color 预定义的背景颜色,见{@link IndexedColors}枚举
|
||||
* @param fillPattern 填充方式 {@link FillPatternType}枚举
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle setColor(final CellStyle cellStyle, final IndexedColors color, final FillPatternType fillPattern) {
|
||||
return setColor(cellStyle, color.index, fillPattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* 给cell设置颜色(即单元格背景色)
|
||||
*
|
||||
* @param cellStyle {@link CellStyle}
|
||||
* @param color 预定义的背景颜色,见{@link IndexedColors}枚举
|
||||
* @param fillPattern 填充方式 {@link FillPatternType}枚举
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle setColor(final CellStyle cellStyle, final short color, final FillPatternType fillPattern) {
|
||||
cellStyle.setFillForegroundColor(color);
|
||||
cellStyle.setFillPattern(ObjUtil.defaultIfNull(fillPattern, FillPatternType.SOLID_FOREGROUND));
|
||||
return cellStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给cell设置颜色(即单元格背景色)
|
||||
*
|
||||
* @param cellStyle {@link CellStyle}
|
||||
* @param color 背景颜色
|
||||
* @param fillPattern 填充方式 {@link FillPatternType}枚举
|
||||
* @return {@link CellStyle}
|
||||
*/
|
||||
public static CellStyle setColor(final XSSFCellStyle cellStyle, final XSSFColor color, final FillPatternType fillPattern) {
|
||||
cellStyle.setFillForegroundColor(color);
|
||||
cellStyle.setFillPattern(ObjUtil.defaultIfNull(fillPattern, FillPatternType.SOLID_FOREGROUND));
|
||||
return cellStyle;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region ----- font
|
||||
|
||||
/**
|
||||
* 创建字体
|
||||
*
|
||||
* @param workbook {@link Workbook}
|
||||
* @param color 字体颜色
|
||||
* @param fontSize 字体大小
|
||||
* @param fontName 字体名称,可以为null使用默认字体
|
||||
* @return {@link Font}
|
||||
*/
|
||||
public static Font createFont(final Workbook workbook, final short color, final short fontSize, final String fontName) {
|
||||
final Font font = workbook.createFont();
|
||||
return setFontStyle(font, color, fontSize, fontName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字体样式
|
||||
*
|
||||
* @param font 字体{@link Font}
|
||||
* @param color 字体颜色
|
||||
* @param fontSize 字体大小
|
||||
* @param fontName 字体名称,可以为null使用默认字体
|
||||
* @return {@link Font}
|
||||
*/
|
||||
public static Font setFontStyle(final Font font, final short color, final short fontSize, final String fontName) {
|
||||
if (color > 0) {
|
||||
font.setColor(color);
|
||||
}
|
||||
if (fontSize > 0) {
|
||||
font.setFontHeightInPoints(fontSize);
|
||||
}
|
||||
if (StrUtil.isNotBlank(fontName)) {
|
||||
font.setFontName(fontName);
|
||||
}
|
||||
return font;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 创建数据格式并获取格式
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user