mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-02 20:02:49 +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)
|
||||
|
@ -44,38 +44,53 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
/**
|
||||
* Excel 写入器<br>
|
||||
* 此工具用于通过POI将数据写出到Excel,此对象可完成以下两个功能
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* 1. 编辑已存在的Excel,可写出原Excel文件,也可写出到其它地方(到文件或到流)
|
||||
* 2. 新建一个空的Excel工作簿,完成数据填充后写出(到文件或到流)
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
* @since 3.2.0
|
||||
*/
|
||||
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>
|
||||
* 若写出到文件,还需调用{@link #setDestFile(File)}方法自定义写出的文件,然后调用{@link #flush()}方法写出到文件
|
||||
*
|
||||
*
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public ExcelWriter() {
|
||||
@ -86,7 +101,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 构造<br>
|
||||
* 此构造不传入写出的Excel文件路径,只能调用{@link #flush(OutputStream)}方法写出到流<br>
|
||||
* 若写出到文件,需要调用{@link #flush(File)} 写出到文件
|
||||
*
|
||||
*
|
||||
* @param isXlsx 是否为xlsx格式
|
||||
* @since 3.2.1
|
||||
*/
|
||||
@ -96,7 +111,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 构造,默认写出到第一个sheet,第一个sheet名为sheet1
|
||||
*
|
||||
*
|
||||
* @param destFilePath 目标文件路径,可以不存在
|
||||
*/
|
||||
public ExcelWriter(String destFilePath) {
|
||||
@ -107,8 +122,8 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 构造<br>
|
||||
* 此构造不传入写出的Excel文件路径,只能调用{@link #flush(OutputStream)}方法写出到流<br>
|
||||
* 若写出到文件,需要调用{@link #flush(File)} 写出到文件
|
||||
*
|
||||
* @param isXlsx 是否为xlsx格式
|
||||
*
|
||||
* @param isXlsx 是否为xlsx格式
|
||||
* @param sheetName sheet名,第一个sheet名并写出到此sheet,例如sheet1
|
||||
* @since 4.1.8
|
||||
*/
|
||||
@ -118,9 +133,9 @@ 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);
|
||||
@ -128,7 +143,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 构造,默认写出到第一个sheet,第一个sheet名为sheet1
|
||||
*
|
||||
*
|
||||
* @param destFile 目标文件,可以不存在
|
||||
*/
|
||||
public ExcelWriter(File destFile) {
|
||||
@ -137,8 +152,8 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param destFile 目标文件,可以不存在
|
||||
*
|
||||
* @param destFile 目标文件,可以不存在
|
||||
* @param sheetName sheet名,做为第一个sheet名并写出到此sheet,例如sheet1
|
||||
*/
|
||||
public ExcelWriter(File destFile, String sheetName) {
|
||||
@ -150,8 +165,8 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 构造<br>
|
||||
* 此构造不传入写出的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) {
|
||||
@ -162,7 +177,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 构造<br>
|
||||
* 此构造不传入写出的Excel文件路径,只能调用{@link #flush(OutputStream)}方法写出到流<br>
|
||||
* 若写出到文件,还需调用{@link #setDestFile(File)}方法自定义写出的文件,然后调用{@link #flush()}方法写出到文件
|
||||
*
|
||||
*
|
||||
* @param sheet {@link Sheet}
|
||||
* @since 4.0.6
|
||||
*/
|
||||
@ -189,13 +204,13 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 重置Writer,包括:
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* 1. 当前行游标归零
|
||||
* 2. 清空别名比较器
|
||||
* 3. 清除标题缓存
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public ExcelWriter reset() {
|
||||
@ -206,7 +221,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 重命名当前sheet
|
||||
*
|
||||
*
|
||||
* @param sheetName 新的sheet名
|
||||
* @return this
|
||||
* @since 4.1.8
|
||||
@ -217,8 +232,8 @@ 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
|
||||
@ -232,7 +247,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 设置所有列为自动宽度,不考虑合并单元格<br>
|
||||
* 此方法必须在指定列数据完全写出后调用才有效。<br>
|
||||
* 列数计算是通过第一行计算的
|
||||
*
|
||||
*
|
||||
* @return this
|
||||
* @since 4.0.12
|
||||
*/
|
||||
@ -247,7 +262,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 设置某列为自动宽度,不考虑合并单元格<br>
|
||||
* 此方法必须在指定列数据完全写出后调用才有效。
|
||||
*
|
||||
*
|
||||
* @param columnIndex 第几列,从0计数
|
||||
* @return this
|
||||
* @since 4.0.12
|
||||
@ -260,8 +275,8 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 设置某列为自动宽度<br>
|
||||
* 此方法必须在指定列数据完全写出后调用才有效。
|
||||
*
|
||||
* @param columnIndex 第几列,从0计数
|
||||
*
|
||||
* @param columnIndex 第几列,从0计数
|
||||
* @param useMergedCells 是否适用于合并单元格
|
||||
* @return this
|
||||
* @since 3.3.0
|
||||
@ -270,10 +285,10 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
this.sheet.autoSizeColumn(columnIndex, useMergedCells);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 禁用默认样式
|
||||
*
|
||||
*
|
||||
* @return this
|
||||
* @see #setStyleSet(StyleSet)
|
||||
* @since 4.6.3
|
||||
@ -284,7 +299,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 设置样式集,如果不使用样式,传入{@code null}
|
||||
*
|
||||
*
|
||||
* @param styleSet 样式集,{@code null}表示无样式
|
||||
* @return this
|
||||
* @since 4.1.11
|
||||
@ -296,14 +311,14 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 获取样式集,样式集可以自定义包括:<br>
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* 1. 头部样式
|
||||
* 2. 一般单元格样式
|
||||
* 3. 默认数字样式
|
||||
* 4. 默认日期样式
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @return 样式集
|
||||
* @since 4.0.0
|
||||
*/
|
||||
@ -313,7 +328,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 获取头部样式,获取样式后可自定义样式
|
||||
*
|
||||
*
|
||||
* @return 头部样式
|
||||
*/
|
||||
public CellStyle getHeadCellStyle() {
|
||||
@ -322,11 +337,11 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 获取单元格样式,获取样式后可自定义样式
|
||||
*
|
||||
*
|
||||
* @return 单元格样式
|
||||
*/
|
||||
public CellStyle getCellStyle() {
|
||||
if(null == this.styleSet){
|
||||
if (null == this.styleSet) {
|
||||
return null;
|
||||
}
|
||||
return this.styleSet.cellStyle;
|
||||
@ -334,41 +349,41 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 获得当前行
|
||||
*
|
||||
*
|
||||
* @return 当前行
|
||||
*/
|
||||
public int getCurrentRow() {
|
||||
return this.currentRow.get();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取Content-Disposition头对应的值,可以通过调用以下方法快速设置下载Excel的头信息:
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* response.setHeader("Content-Disposition", excelWriter.getDisposition("test.xlsx", CharsetUtil.CHARSET_UTF_8));
|
||||
* </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();
|
||||
}
|
||||
|
||||
|
||||
fileName = StrUtil.addSuffixIfNot(URLUtil.encodeAll(fileName, charset), isXlsx() ? ".xlsx" : ".xls");
|
||||
return StrUtil.format("attachment; filename=\"{}\"; filename*={}''{}", fileName, charset.name(), fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前所在行
|
||||
*
|
||||
*
|
||||
* @param rowIndex 行号
|
||||
* @return this
|
||||
*/
|
||||
@ -379,7 +394,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 跳过当前行
|
||||
*
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public ExcelWriter passCurrentRow() {
|
||||
@ -389,7 +404,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 跳过指定行数
|
||||
*
|
||||
*
|
||||
* @param rows 跳过的行数
|
||||
* @return this
|
||||
*/
|
||||
@ -400,7 +415,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 重置当前行为0
|
||||
*
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public ExcelWriter resetRow() {
|
||||
@ -410,7 +425,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 设置写出的目标文件
|
||||
*
|
||||
*
|
||||
* @param destFile 目标文件
|
||||
* @return this
|
||||
*/
|
||||
@ -421,7 +436,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 设置标题别名,key为Map中的key,value为别名
|
||||
*
|
||||
*
|
||||
* @param headerAlias 标题别名
|
||||
* @return this
|
||||
* @since 3.2.1
|
||||
@ -435,7 +450,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 清空标题别名,key为Map中的key,value为别名
|
||||
*
|
||||
*
|
||||
* @return this
|
||||
* @since 4.5.4
|
||||
*/
|
||||
@ -448,7 +463,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 设置是否只保留别名中的字段值,如果为true,则不设置alias的字段将不被输出,false表示原样输出
|
||||
*
|
||||
*
|
||||
* @param isOnlyAlias 是否只保留别名中的字段值
|
||||
* @return this
|
||||
* @since 4.1.22
|
||||
@ -460,8 +475,8 @@ 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,16 +512,16 @@ 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置列宽(单位为一个字符的宽度,例如传入width为10,表示10个字符的宽度)
|
||||
*
|
||||
*
|
||||
* @param columnIndex 列号(从0开始计数,-1表示所有列的默认宽度)
|
||||
* @param width 宽度(单位1~256个字符宽度)
|
||||
* @param width 宽度(单位1~256个字符宽度)
|
||||
* @return this
|
||||
* @since 4.0.8
|
||||
*/
|
||||
@ -518,10 +533,10 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置默认行高,值为一个点的高度
|
||||
*
|
||||
*
|
||||
* @param height 高度
|
||||
* @return this
|
||||
* @since 4.6.5
|
||||
@ -532,7 +547,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 设置行高,值为一个点的高度
|
||||
*
|
||||
*
|
||||
* @param rownum 行号(从0开始计数,-1表示所有行的默认高度)
|
||||
* @param height 高度
|
||||
* @return this
|
||||
@ -552,9 +567,9 @@ 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,26 +577,26 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 增加下拉列表
|
||||
*
|
||||
* @param x x坐标,列号,从0开始
|
||||
* @param y y坐标,行号,从0开始
|
||||
*
|
||||
* @param x x坐标,列号,从0开始
|
||||
* @param y y坐标,行号,从0开始
|
||||
* @param selectList 下拉列表
|
||||
* @return this
|
||||
* @since 4.6.2
|
||||
@ -592,8 +607,8 @@ 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);
|
||||
}
|
||||
|
||||
@ -618,7 +633,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 增加单元格控制,比如下拉列表、日期验证、数字范围验证等
|
||||
*
|
||||
*
|
||||
* @param dataValidation {@link DataValidation}
|
||||
* @return this
|
||||
* @since 4.6.2
|
||||
@ -631,7 +646,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 合并当前行的单元格<br>
|
||||
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
|
||||
*
|
||||
*
|
||||
* @param lastColumn 合并到的最后一个列号
|
||||
* @return this
|
||||
*/
|
||||
@ -643,9 +658,9 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 合并当前行的单元格,并写入对象到单元格<br>
|
||||
* 如果写到单元格中的内容非null,行号自动+1,否则当前行号不变<br>
|
||||
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
|
||||
*
|
||||
*
|
||||
* @param lastColumn 合并到的最后一个列号
|
||||
* @param content 合并单元格后的内容
|
||||
* @param content 合并单元格后的内容
|
||||
* @return this
|
||||
*/
|
||||
public ExcelWriter merge(int lastColumn, Object content) {
|
||||
@ -656,9 +671,9 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 合并某行的单元格,并写入对象到单元格<br>
|
||||
* 如果写到单元格中的内容非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);
|
||||
@ -712,17 +727,17 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加<br>
|
||||
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式<br>
|
||||
* 默认的,当当前行号为0时,写出标题(如果为Map或Bean),否则不写标题
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* data中元素支持的类型有:
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* 1. Iterable,即元素为一个集合,元素被当作一行,data表示多行<br>
|
||||
* 2. Map,即元素为一个Map,第一个Map的keys作为首行,剩下的行为Map的values,data表示多行 <br>
|
||||
* 3. Bean,即元素为一个Bean,第一个Bean的字段名列表会作为首行,剩下的行为Bean的字段值列表,data表示多行 <br>
|
||||
* 4. 其它类型,按照基本类型输出(例如字符串)
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param data 数据
|
||||
* @return this
|
||||
*/
|
||||
@ -734,18 +749,18 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加<br>
|
||||
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* data中元素支持的类型有:
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* 1. Iterable,即元素为一个集合,元素被当作一行,data表示多行<br>
|
||||
* 2. Map,即元素为一个Map,第一个Map的keys作为首行,剩下的行为Map的values,data表示多行 <br>
|
||||
* 3. Bean,即元素为一个Bean,第一个Bean的字段名列表会作为首行,剩下的行为Bean的字段值列表,data表示多行 <br>
|
||||
* 4. 其它类型,按照基本类型输出(例如字符串)
|
||||
* </pre>
|
||||
*
|
||||
* @param data 数据
|
||||
*
|
||||
* @param data 数据
|
||||
* @param isWriteKeyAsHead 是否强制写出标题行(Map或Bean)
|
||||
* @return this
|
||||
*/
|
||||
@ -766,18 +781,18 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加<br>
|
||||
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式<br>
|
||||
* data中元素支持的类型有:
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* 1. Map,即元素为一个Map,第一个Map的keys作为首行,剩下的行为Map的values,data表示多行 <br>
|
||||
* 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;
|
||||
@ -802,7 +817,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br>
|
||||
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
|
||||
*
|
||||
*
|
||||
* @param rowData 一行的数据
|
||||
* @return this
|
||||
*/
|
||||
@ -823,14 +838,14 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 写出一行,根据rowBean数据类型不同,写出情况如下:
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* 1、如果为Iterable,直接写出一行
|
||||
* 2、如果为Map,isWriteKeyAsHead为true写出两行,Map的keys做为一行,values做为第二行,否则只写出一行values
|
||||
* 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)
|
||||
@ -866,8 +881,8 @@ 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;
|
||||
@ -905,7 +920,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 本方法只是将数据写入Workbook中的Sheet,并不写出到文件<br>
|
||||
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1<br>
|
||||
* 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式
|
||||
*
|
||||
*
|
||||
* @param rowData 一行的数据
|
||||
* @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
|
||||
*/
|
||||
@ -930,9 +945,9 @@ 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
|
||||
@ -945,7 +960,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 为指定单元格创建样式
|
||||
*
|
||||
*
|
||||
* @param x X坐标,从0计数,即列号
|
||||
* @param y Y坐标,从0计数,即行号
|
||||
* @return {@link CellStyle}
|
||||
@ -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
|
||||
@ -974,18 +989,18 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
final CellLocation cellLocation = ExcelUtil.toLocation(locationRef);
|
||||
return setStyle(style, cellLocation.getX(), cellLocation.getY());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置某个单元格的样式<br>
|
||||
* 此方法用于多个单元格共享样式的情况<br>
|
||||
* 可以调用{@link #getOrCreateCellStyle(int, int)} 方法创建或取得一个样式对象。
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* 需要注意的是,共享样式会共享同一个{@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,9 +1010,23 @@ 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建字体
|
||||
*
|
||||
*
|
||||
* @return 字体
|
||||
* @since 4.1.0
|
||||
*/
|
||||
@ -1009,7 +1038,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 将Excel Workbook刷出到预定义的文件<br>
|
||||
* 如果用户未自定义输出的文件,将抛出{@link NullPointerException}<br>
|
||||
* 预定义文件可以通过{@link #setDestFile(File)} 方法预定义,或者通过构造定义
|
||||
*
|
||||
*
|
||||
* @return this
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
@ -1020,7 +1049,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
/**
|
||||
* 将Excel Workbook刷出到文件<br>
|
||||
* 如果用户未自定义输出的文件,将抛出{@link NullPointerException}
|
||||
*
|
||||
*
|
||||
* @param destFile 写出到的文件
|
||||
* @return this
|
||||
* @throws IORuntimeException IO异常
|
||||
@ -1033,7 +1062,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 将Excel Workbook刷出到输出流
|
||||
*
|
||||
*
|
||||
* @param out 输出流
|
||||
* @return this
|
||||
* @throws IORuntimeException IO异常
|
||||
@ -1044,8 +1073,8 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 将Excel Workbook刷出到输出流
|
||||
*
|
||||
* @param out 输出流
|
||||
*
|
||||
* @param out 输出流
|
||||
* @param isCloseOut 是否关闭输出流
|
||||
* @return this
|
||||
* @throws IORuntimeException IO异常
|
||||
@ -1090,9 +1119,10 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------- Private method start
|
||||
|
||||
/**
|
||||
* 为指定的key列表添加标题别名,如果没有定义key的别名,在onlyAlias为false时使用原key
|
||||
*
|
||||
*
|
||||
* @param rowMap 一行数据
|
||||
* @return 别名列表
|
||||
*/
|
||||
@ -1118,7 +1148,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 获取单例的别名比较器,比较器的顺序为别名加入的顺序
|
||||
*
|
||||
*
|
||||
* @return Comparator
|
||||
* @since 4.1.5
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user