add methods

This commit is contained in:
Looly 2021-04-22 00:23:58 +08:00
parent 3cc02ed763
commit c3eefb614c
3 changed files with 63 additions and 13 deletions

View File

@ -1033,6 +1033,19 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
return this; return this;
} }
/**
* 设置列的默认样式
*
* @param x 列号从0开始
* @param style 样式
* @return this
* @since 5.6.4
*/
public ExcelWriter setColumnStyle(int x, CellStyle style){
this.sheet.setDefaultColumnStyle(x, style);
return this;
}
/** /**
* 创建字体 * 创建字体
* *

View File

@ -85,12 +85,22 @@ public class RowUtil {
return cellValues; return cellValues;
} }
/**
* 写一行数据无样式非标题
*
* @param row
* @param rowData 一行的数据
*/
public static void writeRow(Row row, Iterable<?> rowData) {
writeRow(row, rowData, null, false);
}
/** /**
* 写一行数据 * 写一行数据
* *
* @param row * @param row
* @param rowData 一行的数据 * @param rowData 一行的数据
* @param styleSet 单元格样式集包括日期等样式 * @param styleSet 单元格样式集包括日期等样式null表示无样式
* @param isHeader 是否为标题行 * @param isHeader 是否为标题行
*/ */
public static void writeRow(Row row, Iterable<?> rowData, StyleSet styleSet, boolean isHeader) { public static void writeRow(Row row, Iterable<?> rowData, StyleSet styleSet, boolean isHeader) {

View File

@ -148,7 +148,7 @@ public class CellUtil {
* *
* @param cell 单元格 * @param cell 单元格
* @param value * @param value
* @param styleSet 单元格样式集包括日期等样式 * @param styleSet 单元格样式集包括日期等样式null表示无样式
* @param isHeader 是否为标题单元格 * @param isHeader 是否为标题单元格
*/ */
public static void setCellValue(Cell cell, Object value, StyleSet styleSet, boolean isHeader) { public static void setCellValue(Cell cell, Object value, StyleSet styleSet, boolean isHeader) {
@ -166,25 +166,21 @@ public class CellUtil {
} }
} }
if (value instanceof Date) { if (value instanceof Date
if (null != styleSet && null != styleSet.getCellStyleForDate()) { || value instanceof TemporalAccessor
cell.setCellStyle(styleSet.getCellStyleForDate()); || value instanceof Calendar) {
} // 日期单独定义格式
} else if (value instanceof TemporalAccessor) {
if (null != styleSet && null != styleSet.getCellStyleForDate()) {
cell.setCellStyle(styleSet.getCellStyleForDate());
}
} else if (value instanceof Calendar) {
if (null != styleSet && null != styleSet.getCellStyleForDate()) { if (null != styleSet && null != styleSet.getCellStyleForDate()) {
cell.setCellStyle(styleSet.getCellStyleForDate()); cell.setCellStyle(styleSet.getCellStyleForDate());
} }
} else if (value instanceof Number) { } else if (value instanceof Number) {
// 数字单独定义格式
if ((value instanceof Double || value instanceof Float || value instanceof BigDecimal) && null != styleSet && null != styleSet.getCellStyleForNumber()) { if ((value instanceof Double || value instanceof Float || value instanceof BigDecimal) && null != styleSet && null != styleSet.getCellStyleForNumber()) {
cell.setCellStyle(styleSet.getCellStyleForNumber()); cell.setCellStyle(styleSet.getCellStyleForNumber());
} }
} }
setCellValue(cell, value, null); setCellValue(cell, value);
} }
/** /**
@ -205,6 +201,23 @@ public class CellUtil {
cell.setCellStyle(style); cell.setCellStyle(style);
} }
setCellValue(cell, value);
}
/**
* 设置单元格值<br>
* 根据传入的styleSet自动匹配样式<br>
* 当为头部样式时默认赋值头部样式但是头部中如果有数字日期等类型将按照数字日期样式设置
*
* @param cell 单元格
* @param value
* @since 5.6.4
*/
public static void setCellValue(Cell cell, Object value) {
if (null == cell) {
return;
}
if (null == value) { if (null == value) {
cell.setCellValue(StrUtil.EMPTY); cell.setCellValue(StrUtil.EMPTY);
} else if (value instanceof FormulaCellValue) { } else if (value instanceof FormulaCellValue) {
@ -318,7 +331,21 @@ public class CellUtil {
* @param lastRow 结束行0开始 * @param lastRow 结束行0开始
* @param firstColumn 起始列0开始 * @param firstColumn 起始列0开始
* @param lastColumn 结束列0开始 * @param lastColumn 结束列0开始
* @param cellStyle 单元格样式只提取边框样式 * @return 合并后的单元格号
*/
public static int mergingCells(Sheet sheet, int firstRow, int lastRow, int firstColumn, int lastColumn) {
return mergingCells(sheet, firstRow, lastRow, firstColumn, lastColumn, null);
}
/**
* 合并单元格可以根据设置的值来合并行和列
*
* @param sheet 表对象
* @param firstRow 起始行0开始
* @param lastRow 结束行0开始
* @param firstColumn 起始列0开始
* @param lastColumn 结束列0开始
* @param cellStyle 单元格样式只提取边框样式null表示无样式
* @return 合并后的单元格号 * @return 合并后的单元格号
*/ */
public static int mergingCells(Sheet sheet, int firstRow, int lastRow, int firstColumn, int lastColumn, CellStyle cellStyle) { public static int mergingCells(Sheet sheet, int firstRow, int lastRow, int firstColumn, int lastColumn, CellStyle cellStyle) {