add method

This commit is contained in:
Looly
2020-12-18 23:25:21 +08:00
parent 9070709dfc
commit 973c9c4227
3 changed files with 59 additions and 43 deletions

View File

@@ -1,8 +1,10 @@
package cn.hutool.poi.excel.style;
import cn.hutool.core.util.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;
@@ -10,20 +12,18 @@ import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import cn.hutool.core.util.StrUtil;
/**
* Excel样式工具类
*
*
* @author looly
* @since 4.0.0
*/
public class StyleUtil {
/**
* 克隆新的{@link CellStyle}
*
* @param cell 单元格
*
* @param cell 单元格
* @param cellStyle 被复制的样式
* @return {@link CellStyle}
*/
@@ -33,8 +33,8 @@ public class StyleUtil {
/**
* 克隆新的{@link CellStyle}
*
* @param workbook 工作簿
*
* @param workbook 工作簿
* @param cellStyle 被复制的样式
* @return {@link CellStyle}
*/
@@ -43,13 +43,13 @@ public class StyleUtil {
newCellStyle.cloneStyleFrom(cellStyle);
return newCellStyle;
}
/**
* 设置cell文本对齐样式
*
*
* @param cellStyle {@link CellStyle}
* @param halign 横向位置
* @param valign 纵向位置
* @param halign 横向位置
* @param valign 纵向位置
* @return {@link CellStyle}
*/
public static CellStyle setAlign(CellStyle cellStyle, HorizontalAlignment halign, VerticalAlignment valign) {
@@ -60,8 +60,8 @@ public class StyleUtil {
/**
* 设置cell的四个边框粗细和颜色
*
* @param cellStyle {@link CellStyle}
*
* @param cellStyle {@link CellStyle}
* @param borderSize 边框粗细{@link BorderStyle}枚举
* @param colorIndex 颜色的short值
* @return {@link CellStyle}
@@ -81,12 +81,12 @@ public class StyleUtil {
return cellStyle;
}
/**
* 给cell设置颜色
*
* @param cellStyle {@link CellStyle}
* @param color 背景颜色
*
* @param cellStyle {@link CellStyle}
* @param color 背景颜色
* @param fillPattern 填充方式 {@link FillPatternType}枚举
* @return {@link CellStyle}
*/
@@ -96,9 +96,9 @@ public class StyleUtil {
/**
* 给cell设置颜色
*
* @param cellStyle {@link CellStyle}
* @param color 背景颜色
*
* @param cellStyle {@link CellStyle}
* @param color 背景颜色
* @param fillPattern 填充方式 {@link FillPatternType}枚举
* @return {@link CellStyle}
*/
@@ -107,12 +107,12 @@ public class StyleUtil {
cellStyle.setFillPattern(fillPattern);
return cellStyle;
}
/**
* 创建字体
*
*
* @param workbook {@link Workbook}
* @param color 字体颜色
* @param color 字体颜色
* @param fontSize 字体大小
* @param fontName 字体名称可以为null使用默认字体
* @return {@link Font}
@@ -121,24 +121,24 @@ public class StyleUtil {
final Font font = workbook.createFont();
return setFontStyle(font, color, fontSize, fontName);
}
/**
* 设置字体样式
*
* @param font 字体{@link Font}
* @param color 字体颜色
*
* @param font 字体{@link Font}
* @param color 字体颜色
* @param fontSize 字体大小
* @param fontName 字体名称可以为null使用默认字体
* @return {@link Font}
*/
public static Font setFontStyle(Font font, short color, short fontSize, String fontName) {
if(color > 0) {
if (color > 0) {
font.setColor(color);
}
if(fontSize > 0) {
if (fontSize > 0) {
font.setFontHeightInPoints(fontSize);
}
if(StrUtil.isNotBlank(fontName)) {
if (StrUtil.isNotBlank(fontName)) {
font.setFontName(fontName);
}
return font;
@@ -153,7 +153,7 @@ public class StyleUtil {
* @since 5.4.0
*/
public static CellStyle createCellStyle(Workbook workbook) {
if(null == workbook){
if (null == workbook) {
return null;
}
return workbook.createCellStyle();
@@ -161,12 +161,12 @@ public class StyleUtil {
/**
* 创建默认普通单元格样式
*
*
* <pre>
* 1. 文字上下左右居中
* 2. 细边框,黑色
* </pre>
*
*
* @param workbook {@link Workbook} 工作簿
* @return {@link CellStyle}
*/
@@ -179,7 +179,7 @@ public class StyleUtil {
/**
* 创建默认头部样式
*
*
* @param workbook {@link Workbook} 工作簿
* @return {@link CellStyle}
*/
@@ -190,15 +190,28 @@ public class StyleUtil {
setColor(cellStyle, IndexedColors.GREY_25_PERCENT, FillPatternType.SOLID_FOREGROUND);
return cellStyle;
}
/**
* 给定样式是否为null无样式或默认样式默认样式为<code>workbook.getCellStyleAt(0)</code>
* 给定样式是否为null无样式或默认样式默认样式为{@code workbook.getCellStyleAt(0)}
*
* @param workbook 工作簿
* @param style 被检查的样式
* @param style 被检查的样式
* @return 是否为null无样式或默认样式
* @since 4.6.3
*/
public static boolean isNullOrDefaultStyle(Workbook workbook, CellStyle style) {
return (null == style) || style.equals(workbook.getCellStyleAt(0));
}
/**
* 创建数据格式并获取格式
*
* @param format 数据格式
* @return 数据格式
* @since 5.5.5
*/
public Short getFormat(Workbook workbook, String format) {
final DataFormat dataFormat = workbook.createDataFormat();
return dataFormat.getFormat(format);
}
}