add setRowStyle

This commit is contained in:
Looly 2020-10-15 09:59:27 +08:00
parent 97d48dde0b
commit 3237df0855
2 changed files with 170 additions and 139 deletions

View File

@ -18,6 +18,7 @@
* 【core 】 完善注释pr#193@Gitee * 【core 】 完善注释pr#193@Gitee
* 【core 】 优化Combination.countAllpr#1159@Github * 【core 】 优化Combination.countAllpr#1159@Github
* 【core 】 优化针对list的split方法pr#194@Gitee * 【core 】 优化针对list的split方法pr#194@Gitee
* 【poi 】 ExcelWriter增加setRowStyle方法
### Bug修复 ### Bug修复
* 【core 】 解决农历判断节日未判断大小月导致的问题issue#I1XHSF@Gitee * 【core 】 解决农历判断节日未判断大小月导致的问题issue#I1XHSF@Gitee

View File

@ -55,22 +55,37 @@ import java.util.concurrent.atomic.AtomicInteger;
*/ */
public class ExcelWriter extends ExcelBase<ExcelWriter> { public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** 目标文件 */ /**
* 目标文件
*/
protected File destFile; protected File destFile;
/** 当前行 */ /**
* 当前行
*/
private AtomicInteger currentRow = new AtomicInteger(0); private AtomicInteger currentRow = new AtomicInteger(0);
/** 标题行别名 */ /**
* 标题行别名
*/
private Map<String, String> headerAlias; private Map<String, String> headerAlias;
/** 是否只保留别名对应的字段 */ /**
* 是否只保留别名对应的字段
*/
private boolean onlyAlias; private boolean onlyAlias;
/** 标题顺序比较器 */ /**
* 标题顺序比较器
*/
private Comparator<String> aliasComparator; private Comparator<String> aliasComparator;
/** 样式集,定义不同类型数据样式 */ /**
* 样式集定义不同类型数据样式
*/
private StyleSet styleSet; private StyleSet styleSet;
/** 标题项对应列号缓存,每次写标题更新此缓存 */ /**
* 标题项对应列号缓存每次写标题更新此缓存
*/
private Map<String, Integer> headLocationCache; private Map<String, Integer> headLocationCache;
// -------------------------------------------------------------------------- Constructor start // -------------------------------------------------------------------------- Constructor start
/** /**
* 构造默认生成xls格式的Excel文件<br> * 构造默认生成xls格式的Excel文件<br>
* 此构造不传入写出的Excel文件路径只能调用{@link #flush(OutputStream)}方法写出到流<br> * 此构造不传入写出的Excel文件路径只能调用{@link #flush(OutputStream)}方法写出到流<br>
@ -326,7 +341,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @return 单元格样式 * @return 单元格样式
*/ */
public CellStyle getCellStyle() { public CellStyle getCellStyle() {
if(null == this.styleSet){ if (null == this.styleSet) {
return null; return null;
} }
return this.styleSet.cellStyle; return this.styleSet.cellStyle;
@ -353,11 +368,11 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @return Content-Disposition值 * @return Content-Disposition值
*/ */
public String getDisposition(String fileName, Charset charset) { public String getDisposition(String fileName, Charset charset) {
if(null == charset) { if (null == charset) {
charset = CharsetUtil.CHARSET_UTF_8; charset = CharsetUtil.CHARSET_UTF_8;
} }
if(StrUtil.isBlank(fileName)) { if (StrUtil.isBlank(fileName)) {
// 未提供文件名使用随机UUID作为文件名 // 未提供文件名使用随机UUID作为文件名
fileName = IdUtil.fastSimpleUUID(); fileName = IdUtil.fastSimpleUUID();
} }
@ -485,7 +500,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @return this * @return this
* @since 5.2.5 * @since 5.2.5
*/ */
public ExcelWriter setFreezePane(int rowSplit){ public ExcelWriter setFreezePane(int rowSplit) {
return setFreezePane(0, rowSplit); return setFreezePane(0, rowSplit);
} }
@ -497,7 +512,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @return this * @return this
* @since 5.2.5 * @since 5.2.5
*/ */
public ExcelWriter setFreezePane(int colSplit, int rowSplit){ public ExcelWriter setFreezePane(int colSplit, int rowSplit) {
getSheet().createFreezePane(colSplit, rowSplit); getSheet().createFreezePane(colSplit, rowSplit);
return this; return this;
} }
@ -606,10 +621,10 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
final DataValidation dataValidation = validationHelper.createValidation(constraint, regions); final DataValidation dataValidation = validationHelper.createValidation(constraint, regions);
//处理Excel兼容性问题 //处理Excel兼容性问题
if(dataValidation instanceof XSSFDataValidation) { if (dataValidation instanceof XSSFDataValidation) {
dataValidation.setSuppressDropDownArrow(true); dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true); dataValidation.setShowErrorBox(true);
}else { } else {
dataValidation.setSuppressDropDownArrow(false); dataValidation.setSuppressDropDownArrow(false);
} }
@ -694,7 +709,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
Assert.isFalse(this.isClosed, "ExcelWriter has been closed!"); Assert.isFalse(this.isClosed, "ExcelWriter has been closed!");
CellStyle style = null; CellStyle style = null;
if(null != this.styleSet){ if (null != this.styleSet) {
style = (isSetHeaderStyle && null != this.styleSet.headCellStyle) ? this.styleSet.headCellStyle : this.styleSet.cellStyle; style = (isSetHeaderStyle && null != this.styleSet.headCellStyle) ? this.styleSet.headCellStyle : this.styleSet.cellStyle;
} }
CellUtil.mergingCells(this.sheet, firstRow, lastRow, firstColumn, lastColumn, style); CellUtil.mergingCells(this.sheet, firstRow, lastRow, firstColumn, lastColumn, style);
@ -777,7 +792,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @return this * @return this
* @since 3.2.3 * @since 3.2.3
*/ */
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({"rawtypes", "unchecked"})
public ExcelWriter write(Iterable<?> data, Comparator<String> comparator) { public ExcelWriter write(Iterable<?> data, Comparator<String> comparator) {
Assert.isFalse(this.isClosed, "ExcelWriter has been closed!"); Assert.isFalse(this.isClosed, "ExcelWriter has been closed!");
boolean isFirstRow = true; boolean isFirstRow = true;
@ -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()); final Row row = RowUtil.getOrCreateRow(this.sheet, this.currentRow.getAndIncrement());
Integer location; Integer location;
for (Entry<?, ?> entry : aliasMap.entrySet()) { for (Entry<?, ?> entry : aliasMap.entrySet()) {
location = this.headLocationCache.get(StrUtil.toString(entry.getKey())); 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); CellUtil.setCellValue(CellUtil.getOrCreateCell(row, location), entry.getValue(), this.styleSet, false);
} }
} }
} else{ } else {
writeRow(aliasMap.values()); writeRow(aliasMap.values());
} }
return this; return this;
@ -995,6 +1010,20 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
return this; 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;
}
/** /**
* 创建字体 * 创建字体
* *
@ -1090,6 +1119,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
} }
// -------------------------------------------------------------------------- Private method start // -------------------------------------------------------------------------- Private method start
/** /**
* 为指定的key列表添加标题别名如果没有定义key的别名在onlyAlias为false时使用原key * 为指定的key列表添加标题别名如果没有定义key的别名在onlyAlias为false时使用原key
* *