add style for columns and rows

This commit is contained in:
Looly 2021-06-24 23:23:26 +08:00
parent 04e0e10fc4
commit ae16121e46
3 changed files with 46 additions and 43 deletions

View File

@ -9,6 +9,7 @@
* 【core 】 增加Convert.toSet方法issue#I3XFG2@Gitee * 【core 】 增加Convert.toSet方法issue#I3XFG2@Gitee
* 【core 】 CsvWriter增加writeBeans方法pr#345@Gitee * 【core 】 CsvWriter增加writeBeans方法pr#345@Gitee
* 【core 】 新增JAXBUtilpr#346@Gitee * 【core 】 新增JAXBUtilpr#346@Gitee
* 【poi 】 ExcelWriter新增setColumnStyleIfHasData和setRowStyleIfHasDatapr#347@Gitee
### 🐞Bug修复 ### 🐞Bug修复
* 【json 】 修复XML转义字符的问题issue#I3XH09@Gitee * 【json 】 修复XML转义字符的问题issue#I3XH09@Gitee

View File

@ -1093,16 +1093,17 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 对数据行整行加自定义样式 仅对数据单元格设置 write后调用 * 对数据行整行加自定义样式 仅对数据单元格设置 write后调用
* * <p>
* {@link cn.hutool.poi.excel.ExcelWriter#setRowStyle(int, org.apache.poi.ss.usermodel.CellStyle)} * {@link cn.hutool.poi.excel.ExcelWriter#setRowStyle(int, org.apache.poi.ss.usermodel.CellStyle)}
* 这个方法加的样式会使整行没有数据的单元格也有样式 * 这个方法加的样式会使整行没有数据的单元格也有样式
* 特别是加背景色时很不美观 且有数据的单元格样式会被StyleSet中的样式覆盖掉 * 特别是加背景色时很不美观 且有数据的单元格样式会被StyleSet中的样式覆盖掉
*
* @param y 行坐标 * @param y 行坐标
* @param style 自定义的样式 * @param style 自定义的样式
* @return * @return this
* @since * @since 5.7.3
*/ */
public ExcelWriter setRowStyleIfRowData(int y, CellStyle style) { public ExcelWriter setRowStyleIfHasData(int y, CellStyle style) {
if (y < 0) { if (y < 0) {
throw new IllegalArgumentException("Invalid row number (" + y + ")"); throw new IllegalArgumentException("Invalid row number (" + y + ")");
} }
@ -1128,17 +1129,18 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 设置整个列的样式 仅对数据单元格设置 write后调用 * 设置整个列的样式 仅对数据单元格设置 write后调用
* * <p>
* {@link cn.hutool.poi.excel.ExcelWriter#setColumnStyle(int, org.apache.poi.ss.usermodel.CellStyle)} * {@link cn.hutool.poi.excel.ExcelWriter#setColumnStyle(int, org.apache.poi.ss.usermodel.CellStyle)}
* 这个方法加的样式会使整列没有数据的单元格也有样式 * 这个方法加的样式会使整列没有数据的单元格也有样式
* 特别是加背景色时很不美观 且有数据的单元格样式会被StyleSet中的样式覆盖掉 * 特别是加背景色时很不美观 且有数据的单元格样式会被StyleSet中的样式覆盖掉
*
* @param x 列的索引 * @param x 列的索引
* @param y 的索引 * @param y 起始
* @param style * @param style 样式
* @return * @return this
* @since * @since 5.7.3
*/ */
public ExcelWriter setColumnStyleIfColumnData(int x,int y, CellStyle style) { public ExcelWriter setColumnStyleIfHasData(int x, int y, CellStyle style) {
if (x < 0) { if (x < 0) {
throw new IllegalArgumentException("Invalid column number (" + x + ")"); throw new IllegalArgumentException("Invalid column number (" + x + ")");
} }
@ -1146,8 +1148,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
throw new IllegalArgumentException("Invalid row number (" + y + ")"); throw new IllegalArgumentException("Invalid row number (" + y + ")");
} }
int rowCount = this.getRowCount(); int rowCount = this.getRowCount();
int i = y; for (int i = y; i < rowCount; i++) {
for(;i<rowCount;i++){
this.setStyle(style, x, i); this.setStyle(style, x, i);
} }
return this; return this;

View File

@ -38,6 +38,7 @@ import java.util.TreeMap;
* @author looly * @author looly
*/ */
public class ExcelWriteTest { public class ExcelWriteTest {
@Test @Test
// @Ignore // @Ignore
public void testRowOrColumnCellStyle() { public void testRowOrColumnCellStyle() {
@ -48,7 +49,7 @@ public class ExcelWriteTest {
List<?> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4", DateUtil.date(), 28.00); List<?> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4", DateUtil.date(), 28.00);
List<List<?>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5); List<List<?>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);
BigExcelWriter overtimeWriter = ExcelUtil.getBigWriter("e:/excel/single_line.xlsx"); BigExcelWriter overtimeWriter = ExcelUtil.getBigWriter("d:/test/style_line.xlsx");
overtimeWriter.write(rows, true); overtimeWriter.write(rows, true);
@ -66,9 +67,9 @@ public class ExcelWriteTest {
//现增加的设置行列样式的方法 //现增加的设置行列样式的方法
//给第三行加背景色 //给第三行加背景色
overtimeWriter.setRowStyleIfRowData(2,cellStyle); overtimeWriter.setRowStyleIfHasData(2, cellStyle);
//给第二列加背景色 从第一行开始加用于控制有表头时 //给第二列加背景色 从第一行开始加用于控制有表头时
overtimeWriter.setColumnStyleIfColumnData(1,0,cellStyle); overtimeWriter.setColumnStyleIfHasData(1, 0, cellStyle);
CellStyle cellStyle1 = overtimeWriter.getWorkbook().createCellStyle(); CellStyle cellStyle1 = overtimeWriter.getWorkbook().createCellStyle();
StyleUtil.setBorder(cellStyle1, BorderStyle.THIN, IndexedColors.BLACK); StyleUtil.setBorder(cellStyle1, BorderStyle.THIN, IndexedColors.BLACK);