mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-03 12:18:01 +08:00
add style for columns and rows
This commit is contained in:
parent
04e0e10fc4
commit
ae16121e46
@ -9,6 +9,7 @@
|
||||
* 【core 】 增加Convert.toSet方法(issue#I3XFG2@Gitee)
|
||||
* 【core 】 CsvWriter增加writeBeans方法(pr#345@Gitee)
|
||||
* 【core 】 新增JAXBUtil(pr#346@Gitee)
|
||||
* 【poi 】 ExcelWriter新增setColumnStyleIfHasData和setRowStyleIfHasData(pr#347@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【json 】 修复XML转义字符的问题(issue#I3XH09@Gitee)
|
||||
|
@ -1093,16 +1093,17 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 对数据行整行加自定义样式 仅对数据单元格设置 write后调用
|
||||
*
|
||||
* <p>
|
||||
* {@link cn.hutool.poi.excel.ExcelWriter#setRowStyle(int, org.apache.poi.ss.usermodel.CellStyle)}
|
||||
* 这个方法加的样式会使整行没有数据的单元格也有样式
|
||||
* 特别是加背景色时很不美观 且有数据的单元格样式会被StyleSet中的样式覆盖掉
|
||||
*
|
||||
* @param y 行坐标
|
||||
* @param style 自定义的样式
|
||||
* @return
|
||||
* @since
|
||||
* @return this
|
||||
* @since 5.7.3
|
||||
*/
|
||||
public ExcelWriter setRowStyleIfRowData(int y, CellStyle style) {
|
||||
public ExcelWriter setRowStyleIfHasData(int y, CellStyle style) {
|
||||
if (y < 0) {
|
||||
throw new IllegalArgumentException("Invalid row number (" + y + ")");
|
||||
}
|
||||
@ -1128,17 +1129,18 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
|
||||
/**
|
||||
* 设置整个列的样式 仅对数据单元格设置 write后调用
|
||||
*
|
||||
* <p>
|
||||
* {@link cn.hutool.poi.excel.ExcelWriter#setColumnStyle(int, org.apache.poi.ss.usermodel.CellStyle)}
|
||||
* 这个方法加的样式会使整列没有数据的单元格也有样式
|
||||
* 特别是加背景色时很不美观 且有数据的单元格样式会被StyleSet中的样式覆盖掉
|
||||
*
|
||||
* @param x 列的索引
|
||||
* @param y 行的索引
|
||||
* @param style
|
||||
* @return
|
||||
* @since
|
||||
* @param y 起始行
|
||||
* @param style 样式
|
||||
* @return this
|
||||
* @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) {
|
||||
throw new IllegalArgumentException("Invalid column number (" + x + ")");
|
||||
}
|
||||
@ -1146,8 +1148,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
throw new IllegalArgumentException("Invalid row number (" + y + ")");
|
||||
}
|
||||
int rowCount = this.getRowCount();
|
||||
int i = y;
|
||||
for(;i<rowCount;i++){
|
||||
for (int i = y; i < rowCount; i++) {
|
||||
this.setStyle(style, x, i);
|
||||
}
|
||||
return this;
|
||||
|
@ -38,6 +38,7 @@ import java.util.TreeMap;
|
||||
* @author looly
|
||||
*/
|
||||
public class ExcelWriteTest {
|
||||
|
||||
@Test
|
||||
// @Ignore
|
||||
public void testRowOrColumnCellStyle() {
|
||||
@ -48,7 +49,7 @@ public class ExcelWriteTest {
|
||||
List<?> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4", DateUtil.date(), 28.00);
|
||||
|
||||
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);
|
||||
|
||||
@ -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();
|
||||
StyleUtil.setBorder(cellStyle1, BorderStyle.THIN, IndexedColors.BLACK);
|
||||
|
Loading…
Reference in New Issue
Block a user