mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-02 20:02:49 +08:00
fix bug
This commit is contained in:
parent
32684b2d5c
commit
c8071336f9
@ -3,7 +3,7 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.7.16 (2021-10-26)
|
||||
# 5.7.16 (2021-10-27)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 增加DateTime.toLocalDateTime
|
||||
@ -18,6 +18,7 @@
|
||||
* 【core 】 修复UrlBuilder.addPath歧义问题(issue#1912@Github)
|
||||
* 【core 】 修复StrBuilder中总长度计算问题(issue#I4F9L7@Gitee)
|
||||
* 【core 】 修复CharSequenceUtil.wrapIfMissing预定义长度计算问题(issue#I4FDZ2@Gitee)
|
||||
* 【poi 】 修复合并单元格为日期时,导出单元格数据为数字问题(issue#1911@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -736,7 +736,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
CellStyle style = null;
|
||||
if (null != this.styleSet) {
|
||||
style = (isSetHeaderStyle && null != this.styleSet.headCellStyle) ? this.styleSet.headCellStyle : this.styleSet.cellStyle;
|
||||
style = styleSet.getStyleByValueType(content, isSetHeaderStyle);
|
||||
}
|
||||
|
||||
return merge(firstRow, lastRow, firstColumn, lastColumn, content, style);
|
||||
|
@ -7,32 +7,48 @@ 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 java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 样式集合,此样式集合汇集了整个工作簿的样式,用于减少样式的创建和冗余
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class StyleSet implements Serializable{
|
||||
public class StyleSet implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 工作簿引用 */
|
||||
/**
|
||||
* 工作簿引用
|
||||
*/
|
||||
private final Workbook workbook;
|
||||
/** 标题样式 */
|
||||
/**
|
||||
* 标题样式
|
||||
*/
|
||||
protected CellStyle headCellStyle;
|
||||
/** 默认样式 */
|
||||
/**
|
||||
* 默认样式
|
||||
*/
|
||||
protected CellStyle cellStyle;
|
||||
/** 默认数字样式 */
|
||||
/**
|
||||
* 默认数字样式
|
||||
*/
|
||||
protected CellStyle cellStyleForNumber;
|
||||
/** 默认日期样式 */
|
||||
/**
|
||||
* 默认日期样式
|
||||
*/
|
||||
protected CellStyle cellStyleForDate;
|
||||
/** 默认链接样式 */
|
||||
/**
|
||||
* 默认链接样式
|
||||
*/
|
||||
protected CellStyle cellStyleForHyperlink;
|
||||
|
||||
/**
|
||||
@ -148,7 +164,7 @@ public class StyleSet implements Serializable{
|
||||
* 设置单元格背景样式
|
||||
*
|
||||
* @param backgroundColor 背景色
|
||||
* @param withHeadCell 是否也定义头部样式
|
||||
* @param withHeadCell 是否也定义头部样式
|
||||
* @return this
|
||||
* @since 4.0.0
|
||||
*/
|
||||
@ -166,9 +182,9 @@ public class StyleSet implements Serializable{
|
||||
/**
|
||||
* 设置全局字体
|
||||
*
|
||||
* @param color 字体颜色
|
||||
* @param fontSize 字体大小,-1表示默认大小
|
||||
* @param fontName 字体名,null表示默认字体
|
||||
* @param color 字体颜色
|
||||
* @param fontSize 字体大小,-1表示默认大小
|
||||
* @param fontName 字体名,null表示默认字体
|
||||
* @param ignoreHead 是否跳过头部样式
|
||||
* @return this
|
||||
*/
|
||||
@ -180,7 +196,7 @@ public class StyleSet implements Serializable{
|
||||
/**
|
||||
* 设置全局字体
|
||||
*
|
||||
* @param font 字体,可以通过{@link StyleUtil#createFont(Workbook, short, short, String)}创建
|
||||
* @param font 字体,可以通过{@link StyleUtil#createFont(Workbook, short, short, String)}创建
|
||||
* @param ignoreHead 是否跳过头部样式
|
||||
* @return this
|
||||
* @since 4.1.0
|
||||
@ -210,4 +226,44 @@ public class StyleSet implements Serializable{
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取值对应的公共单元格样式
|
||||
*
|
||||
* @param value 值
|
||||
* @param isHeader 是否为标题单元格
|
||||
* @return 值对应单元格样式
|
||||
* @since 5.7.16
|
||||
*/
|
||||
public CellStyle getStyleByValueType(Object value, 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,7 +15,6 @@ import org.apache.poi.ss.usermodel.ClientAnchor;
|
||||
import org.apache.poi.ss.usermodel.Comment;
|
||||
import org.apache.poi.ss.usermodel.CreationHelper;
|
||||
import org.apache.poi.ss.usermodel.Drawing;
|
||||
import org.apache.poi.ss.usermodel.Hyperlink;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
@ -23,11 +22,6 @@ import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.ss.util.RegionUtil;
|
||||
import org.apache.poi.ss.util.SheetUtil;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Excel表格中单元格工具类
|
||||
*
|
||||
@ -151,32 +145,7 @@ public class CellUtil {
|
||||
}
|
||||
|
||||
if (null != styleSet) {
|
||||
final CellStyle headCellStyle = styleSet.getHeadCellStyle();
|
||||
final CellStyle cellStyle = styleSet.getCellStyle();
|
||||
if (isHeader && null != headCellStyle) {
|
||||
cell.setCellStyle(headCellStyle);
|
||||
} else if (null != cellStyle) {
|
||||
cell.setCellStyle(cellStyle);
|
||||
}
|
||||
}
|
||||
|
||||
if (value instanceof Date
|
||||
|| value instanceof TemporalAccessor
|
||||
|| value instanceof Calendar) {
|
||||
// 日期单独定义格式
|
||||
if (null != styleSet && null != styleSet.getCellStyleForDate()) {
|
||||
cell.setCellStyle(styleSet.getCellStyleForDate());
|
||||
}
|
||||
} else if (value instanceof Number) {
|
||||
// 数字单独定义格式
|
||||
if ((value instanceof Double || value instanceof Float || value instanceof BigDecimal) && null != styleSet && null != styleSet.getCellStyleForNumber()) {
|
||||
cell.setCellStyle(styleSet.getCellStyleForNumber());
|
||||
}
|
||||
} else if(value instanceof Hyperlink){
|
||||
// 自定义超链接样式
|
||||
if (null != styleSet && null != styleSet.getCellStyleForHyperlink()) {
|
||||
cell.setCellStyle(styleSet.getCellStyleForHyperlink());
|
||||
}
|
||||
cell.setCellStyle(styleSet.getStyleByValueType(value, isHeader));
|
||||
}
|
||||
|
||||
setCellValue(cell, value);
|
||||
|
@ -727,6 +727,19 @@ public class ExcelWriteTest {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void mergeForDateTest(){
|
||||
// https://github.com/dromara/hutool/issues/1911
|
||||
|
||||
//通过工具类创建writer
|
||||
String path = "d:/test/mergeForDate.xlsx";
|
||||
FileUtil.del(path);
|
||||
ExcelWriter writer = ExcelUtil.getWriter(path);
|
||||
writer.merge(0, 3, 0, 2, DateUtil.date(), false);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void changeHeaderStyleTest(){
|
||||
|
Loading…
Reference in New Issue
Block a user