mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-03 12:18:01 +08:00
add setRowStyle
This commit is contained in:
parent
97d48dde0b
commit
3237df0855
@ -18,6 +18,7 @@
|
||||
* 【core 】 完善注释(pr#193@Gitee)
|
||||
* 【core 】 优化Combination.countAll(pr#1159@Github)
|
||||
* 【core 】 优化针对list的split方法(pr#194@Gitee)
|
||||
* 【poi 】 ExcelWriter增加setRowStyle方法
|
||||
|
||||
### Bug修复
|
||||
* 【core 】 解决农历判断节日未判断大小月导致的问题(issue#I1XHSF@Gitee)
|
||||
|
@ -55,22 +55,37 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
*/
|
||||
public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/** 目标文件 */
|
||||
/**
|
||||
* 目标文件
|
||||
*/
|
||||
protected File destFile;
|
||||
/** 当前行 */
|
||||
/**
|
||||
* 当前行
|
||||
*/
|
||||
private AtomicInteger currentRow = new AtomicInteger(0);
|
||||
/** 标题行别名 */
|
||||
/**
|
||||
* 标题行别名
|
||||
*/
|
||||
private Map<String, String> headerAlias;
|
||||
/** 是否只保留别名对应的字段 */
|
||||
/**
|
||||
* 是否只保留别名对应的字段
|
||||
*/
|
||||
private boolean onlyAlias;
|
||||
/** 标题顺序比较器 */
|
||||
/**
|
||||
* 标题顺序比较器
|
||||
*/
|
||||
private Comparator<String> aliasComparator;
|
||||
/** 样式集,定义不同类型数据样式 */
|
||||
/**
|
||||
* 样式集,定义不同类型数据样式
|
||||
*/
|
||||
private StyleSet styleSet;
|
||||
/** 标题项对应列号缓存,每次写标题更新此缓存 */
|
||||
/**
|
||||
* 标题项对应列号缓存,每次写标题更新此缓存
|
||||
*/
|
||||
private Map<String, Integer> headLocationCache;
|
||||
|
||||
// -------------------------------------------------------------------------- Constructor start
|
||||
|
||||
/**
|
||||
* 构造,默认生成xls格式的Excel文件<br>
|
||||
* 此构造不传入写出的Excel文件路径,只能调用{@link #flush(OutputStream)}方法写出到流<br>
|
||||
@ -108,7 +123,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 此构造不传入写出的Excel文件路径,只能调用{@link #flush(OutputStream)}方法写出到流<br>
|
||||
* 若写出到文件,需要调用{@link #flush(File)} 写出到文件
|
||||
*
|
||||
* @param isXlsx 是否为xlsx格式
|
||||
* @param isXlsx 是否为xlsx格式
|
||||
* @param sheetName sheet名,第一个sheet名并写出到此sheet,例如sheet1
|
||||
* @since 4.1.8
|
||||
*/
|
||||
@ -120,7 +135,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 构造
|
||||
*
|
||||
* @param destFilePath 目标文件路径,可以不存在
|
||||
* @param sheetName sheet名,第一个sheet名并写出到此sheet,例如sheet1
|
||||
* @param sheetName sheet名,第一个sheet名并写出到此sheet,例如sheet1
|
||||
*/
|
||||
public ExcelWriter(String destFilePath, String sheetName) {
|
||||
this(FileUtil.file(destFilePath), sheetName);
|
||||
@ -138,7 +153,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param destFile 目标文件,可以不存在
|
||||
* @param destFile 目标文件,可以不存在
|
||||
* @param sheetName sheet名,做为第一个sheet名并写出到此sheet,例如sheet1
|
||||
*/
|
||||
public ExcelWriter(File destFile, String sheetName) {
|
||||
@ -151,7 +166,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 此构造不传入写出的Excel文件路径,只能调用{@link #flush(OutputStream)}方法写出到流<br>
|
||||
* 若写出到文件,还需调用{@link #setDestFile(File)}方法自定义写出的文件,然后调用{@link #flush()}方法写出到文件
|
||||
*
|
||||
* @param workbook {@link Workbook}
|
||||
* @param workbook {@link Workbook}
|
||||
* @param sheetName sheet名,做为第一个sheet名并写出到此sheet,例如sheet1
|
||||
*/
|
||||
public ExcelWriter(Workbook workbook, String sheetName) {
|
||||
@ -218,7 +233,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 重命名sheet
|
||||
*
|
||||
* @param sheet sheet需要,0表示第一个sheet
|
||||
* @param sheet sheet需要,0表示第一个sheet
|
||||
* @param sheetName 新的sheet名
|
||||
* @return this
|
||||
* @since 4.1.8
|
||||
@ -261,7 +276,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 设置某列为自动宽度<br>
|
||||
* 此方法必须在指定列数据完全写出后调用才有效。
|
||||
*
|
||||
* @param columnIndex 第几列,从0计数
|
||||
* @param columnIndex 第几列,从0计数
|
||||
* @param useMergedCells 是否适用于合并单元格
|
||||
* @return this
|
||||
* @since 3.3.0
|
||||
@ -326,7 +341,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* @return 单元格样式
|
||||
*/
|
||||
public CellStyle getCellStyle() {
|
||||
if(null == this.styleSet){
|
||||
if (null == this.styleSet) {
|
||||
return null;
|
||||
}
|
||||
return this.styleSet.cellStyle;
|
||||
@ -349,15 +364,15 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* </pre>
|
||||
*
|
||||
* @param fileName 文件名,如果文件名没有扩展名,会自动按照生成Excel类型补齐扩展名,如果提供空,使用随机UUID
|
||||
* @param charset 编码,null则使用默认UTF-8编码
|
||||
* @param charset 编码,null则使用默认UTF-8编码
|
||||
* @return Content-Disposition值
|
||||
*/
|
||||
public String getDisposition(String fileName, Charset charset) {
|
||||
if(null == charset) {
|
||||
if (null == charset) {
|
||||
charset = CharsetUtil.CHARSET_UTF_8;
|
||||
}
|
||||
|
||||
if(StrUtil.isBlank(fileName)) {
|
||||
if (StrUtil.isBlank(fileName)) {
|
||||
// 未提供文件名使用随机UUID作为文件名
|
||||
fileName = IdUtil.fastSimpleUUID();
|
||||
}
|
||||
@ -461,7 +476,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 增加标题别名
|
||||
*
|
||||
* @param name 原标题
|
||||
* @param name 原标题
|
||||
* @param alias 别名
|
||||
* @return this
|
||||
* @since 4.1.5
|
||||
@ -485,7 +500,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* @return this
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public ExcelWriter setFreezePane(int rowSplit){
|
||||
public ExcelWriter setFreezePane(int rowSplit) {
|
||||
return setFreezePane(0, rowSplit);
|
||||
}
|
||||
|
||||
@ -497,7 +512,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* @return this
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public ExcelWriter setFreezePane(int colSplit, int rowSplit){
|
||||
public ExcelWriter setFreezePane(int colSplit, int rowSplit) {
|
||||
getSheet().createFreezePane(colSplit, rowSplit);
|
||||
return this;
|
||||
}
|
||||
@ -506,7 +521,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 设置列宽(单位为一个字符的宽度,例如传入width为10,表示10个字符的宽度)
|
||||
*
|
||||
* @param columnIndex 列号(从0开始计数,-1表示所有列的默认宽度)
|
||||
* @param width 宽度(单位1~256个字符宽度)
|
||||
* @param width 宽度(单位1~256个字符宽度)
|
||||
* @return this
|
||||
* @since 4.0.8
|
||||
*/
|
||||
@ -553,8 +568,8 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 设置Excel页眉或页脚
|
||||
*
|
||||
* @param text 页脚的文本
|
||||
* @param align 对齐方式枚举 {@link Align}
|
||||
* @param text 页脚的文本
|
||||
* @param align 对齐方式枚举 {@link Align}
|
||||
* @param isFooter 是否为页脚,false表示页眉,true表示页脚
|
||||
* @return this
|
||||
* @since 4.1.0
|
||||
@ -562,17 +577,17 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
public ExcelWriter setHeaderOrFooter(String text, Align align, boolean isFooter) {
|
||||
final HeaderFooter headerFooter = isFooter ? this.sheet.getFooter() : this.sheet.getHeader();
|
||||
switch (align) {
|
||||
case LEFT:
|
||||
headerFooter.setLeft(text);
|
||||
break;
|
||||
case RIGHT:
|
||||
headerFooter.setRight(text);
|
||||
break;
|
||||
case CENTER:
|
||||
headerFooter.setCenter(text);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
case LEFT:
|
||||
headerFooter.setLeft(text);
|
||||
break;
|
||||
case RIGHT:
|
||||
headerFooter.setRight(text);
|
||||
break;
|
||||
case CENTER:
|
||||
headerFooter.setCenter(text);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@ -580,8 +595,8 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 增加下拉列表
|
||||
*
|
||||
* @param x x坐标,列号,从0开始
|
||||
* @param y y坐标,行号,从0开始
|
||||
* @param x x坐标,列号,从0开始
|
||||
* @param y y坐标,行号,从0开始
|
||||
* @param selectList 下拉列表
|
||||
* @return this
|
||||
* @since 4.6.2
|
||||
@ -593,7 +608,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 增加下拉列表
|
||||
*
|
||||
* @param regions {@link CellRangeAddressList} 指定下拉列表所占的单元格范围
|
||||
* @param regions {@link CellRangeAddressList} 指定下拉列表所占的单元格范围
|
||||
* @param selectList 下拉列表内容
|
||||
* @return this
|
||||
* @since 4.6.2
|
||||
@ -606,10 +621,10 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
final DataValidation dataValidation = validationHelper.createValidation(constraint, regions);
|
||||
|
||||
//处理Excel兼容性问题
|
||||
if(dataValidation instanceof XSSFDataValidation) {
|
||||
if (dataValidation instanceof XSSFDataValidation) {
|
||||
dataValidation.setSuppressDropDownArrow(true);
|
||||
dataValidation.setShowErrorBox(true);
|
||||
}else {
|
||||
} else {
|
||||
dataValidation.setSuppressDropDownArrow(false);
|
||||
}
|
||||
|
||||
@ -645,7 +660,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
|
||||
*
|
||||
* @param lastColumn 合并到的最后一个列号
|
||||
* @param content 合并单元格后的内容
|
||||
* @param content 合并单元格后的内容
|
||||
* @return this
|
||||
*/
|
||||
public ExcelWriter merge(int lastColumn, Object content) {
|
||||
@ -657,8 +672,8 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 如果写到单元格中的内容非null,行号自动+1,否则当前行号不变<br>
|
||||
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
|
||||
*
|
||||
* @param lastColumn 合并到的最后一个列号
|
||||
* @param content 合并单元格后的内容
|
||||
* @param lastColumn 合并到的最后一个列号
|
||||
* @param content 合并单元格后的内容
|
||||
* @param isSetHeaderStyle 是否为合并后的单元格设置默认标题样式
|
||||
* @return this
|
||||
* @since 4.0.10
|
||||
@ -681,11 +696,11 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 如果写到单元格中的内容非null,行号自动+1,否则当前行号不变<br>
|
||||
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
|
||||
*
|
||||
* @param firstRow 起始行,0开始
|
||||
* @param lastRow 结束行,0开始
|
||||
* @param firstColumn 起始列,0开始
|
||||
* @param lastColumn 结束列,0开始
|
||||
* @param content 合并单元格后的内容
|
||||
* @param firstRow 起始行,0开始
|
||||
* @param lastRow 结束行,0开始
|
||||
* @param firstColumn 起始列,0开始
|
||||
* @param lastColumn 结束列,0开始
|
||||
* @param content 合并单元格后的内容
|
||||
* @param isSetHeaderStyle 是否为合并后的单元格设置默认标题样式
|
||||
* @return this
|
||||
* @since 4.0.10
|
||||
@ -694,7 +709,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
Assert.isFalse(this.isClosed, "ExcelWriter has been closed!");
|
||||
|
||||
CellStyle style = null;
|
||||
if(null != this.styleSet){
|
||||
if (null != this.styleSet) {
|
||||
style = (isSetHeaderStyle && null != this.styleSet.headCellStyle) ? this.styleSet.headCellStyle : this.styleSet.cellStyle;
|
||||
}
|
||||
CellUtil.mergingCells(this.sheet, firstRow, lastRow, firstColumn, lastColumn, style);
|
||||
@ -745,7 +760,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 4. 其它类型,按照基本类型输出(例如字符串)
|
||||
* </pre>
|
||||
*
|
||||
* @param data 数据
|
||||
* @param data 数据
|
||||
* @param isWriteKeyAsHead 是否强制写出标题行(Map或Bean)
|
||||
* @return this
|
||||
*/
|
||||
@ -772,12 +787,12 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 2. Bean,即元素为一个Bean,第一个Bean的字段名列表会作为首行,剩下的行为Bean的字段值列表,data表示多行 <br>
|
||||
* </p>
|
||||
*
|
||||
* @param data 数据
|
||||
* @param data 数据
|
||||
* @param comparator 比较器,用于字段名的排序
|
||||
* @return this
|
||||
* @since 3.2.3
|
||||
*/
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public ExcelWriter write(Iterable<?> data, Comparator<String> comparator) {
|
||||
Assert.isFalse(this.isClosed, "ExcelWriter has been closed!");
|
||||
boolean isFirstRow = true;
|
||||
@ -830,7 +845,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 3、如果为Bean,转为Map写出,isWriteKeyAsHead为true写出两行,Map的keys做为一行,values做为第二行,否则只写出一行values
|
||||
* </pre>
|
||||
*
|
||||
* @param rowBean 写出的Bean
|
||||
* @param rowBean 写出的Bean
|
||||
* @param isWriteKeyAsHead 为true写出两行,Map的keys做为一行,values做为第二行,否则只写出一行values
|
||||
* @return this
|
||||
* @see #writeRow(Iterable)
|
||||
@ -867,7 +882,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 将一个Map写入到Excel,isWriteKeyAsHead为true写出两行,Map的keys做为一行,values做为第二行,否则只写出一行values<br>
|
||||
* 如果rowMap为空(包括null),则写出空行
|
||||
*
|
||||
* @param rowMap 写出的Map,为空(包括null),则写出空行
|
||||
* @param rowMap 写出的Map,为空(包括null),则写出空行
|
||||
* @param isWriteKeyAsHead 为true写出两行,Map的keys做为一行,values做为第二行,否则只写出一行values
|
||||
* @return this
|
||||
*/
|
||||
@ -885,16 +900,16 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
}
|
||||
|
||||
// 如果已经写出标题行,根据标题行找对应的值写入
|
||||
if(MapUtil.isNotEmpty(this.headLocationCache)){
|
||||
if (MapUtil.isNotEmpty(this.headLocationCache)) {
|
||||
final Row row = RowUtil.getOrCreateRow(this.sheet, this.currentRow.getAndIncrement());
|
||||
Integer location;
|
||||
for (Entry<?, ?> entry : aliasMap.entrySet()) {
|
||||
location = this.headLocationCache.get(StrUtil.toString(entry.getKey()));
|
||||
if(null != location){
|
||||
if (null != location) {
|
||||
CellUtil.setCellValue(CellUtil.getOrCreateCell(row, location), entry.getValue(), this.styleSet, false);
|
||||
}
|
||||
}
|
||||
} else{
|
||||
} else {
|
||||
writeRow(aliasMap.values());
|
||||
}
|
||||
return this;
|
||||
@ -919,7 +934,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 给指定单元格赋值,使用默认单元格样式
|
||||
*
|
||||
* @param locationRef 单元格地址标识符,例如A11,B5
|
||||
* @param value 值
|
||||
* @param value 值
|
||||
* @return this
|
||||
* @since 5.1.4
|
||||
*/
|
||||
@ -931,8 +946,8 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 给指定单元格赋值,使用默认单元格样式
|
||||
*
|
||||
* @param x X坐标,从0计数,即列号
|
||||
* @param y Y坐标,从0计数,即行号
|
||||
* @param x X坐标,从0计数,即列号
|
||||
* @param y Y坐标,从0计数,即行号
|
||||
* @param value 值
|
||||
* @return this
|
||||
* @since 4.0.2
|
||||
@ -965,7 +980,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* <p>
|
||||
* 需要注意的是,共享样式会共享同一个{@link CellStyle},一个单元格样式改变,全部改变。
|
||||
*
|
||||
* @param style 单元格样式
|
||||
* @param style 单元格样式
|
||||
* @param locationRef 单元格地址标识符,例如A11,B5
|
||||
* @return this
|
||||
* @since 5.1.4
|
||||
@ -984,8 +999,8 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 需要注意的是,共享样式会共享同一个{@link CellStyle},一个单元格样式改变,全部改变。
|
||||
*
|
||||
* @param style 单元格样式
|
||||
* @param x X坐标,从0计数,即列号
|
||||
* @param y Y坐标,从0计数,即行号
|
||||
* @param x X坐标,从0计数,即列号
|
||||
* @param y Y坐标,从0计数,即行号
|
||||
* @return this
|
||||
* @since 4.6.3
|
||||
*/
|
||||
@ -995,6 +1010,20 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置行样式
|
||||
*
|
||||
* @param y Y坐标,从0计数,即行号
|
||||
* @param style 样式
|
||||
* @return this
|
||||
* @see Row#setRowStyle(CellStyle)
|
||||
* @since 5.4.5
|
||||
*/
|
||||
public ExcelWriter setRowStyle(int y, CellStyle style) {
|
||||
getOrCreateRow(y).setRowStyle(style);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建字体
|
||||
*
|
||||
@ -1045,7 +1074,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 将Excel Workbook刷出到输出流
|
||||
*
|
||||
* @param out 输出流
|
||||
* @param out 输出流
|
||||
* @param isCloseOut 是否关闭输出流
|
||||
* @return this
|
||||
* @throws IORuntimeException IO异常
|
||||
@ -1090,6 +1119,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------- Private method start
|
||||
|
||||
/**
|
||||
* 为指定的key列表添加标题别名,如果没有定义key的别名,在onlyAlias为false时使用原key
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user