mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
commit
04e0e10fc4
@ -1091,6 +1091,28 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对数据行整行加自定义样式 仅对数据单元格设置 write后调用
|
||||||
|
*
|
||||||
|
* {@link cn.hutool.poi.excel.ExcelWriter#setRowStyle(int, org.apache.poi.ss.usermodel.CellStyle)}
|
||||||
|
* 这个方法加的样式会使整行没有数据的单元格也有样式
|
||||||
|
* 特别是加背景色时很不美观 且有数据的单元格样式会被StyleSet中的样式覆盖掉
|
||||||
|
* @param y 行坐标
|
||||||
|
* @param style 自定义的样式
|
||||||
|
* @return
|
||||||
|
* @since
|
||||||
|
*/
|
||||||
|
public ExcelWriter setRowStyleIfRowData(int y, CellStyle style) {
|
||||||
|
if(y < 0) {
|
||||||
|
throw new IllegalArgumentException("Invalid row number (" + y + ")");
|
||||||
|
}
|
||||||
|
int columnCount = this.getColumnCount();
|
||||||
|
for(int i=0;i<columnCount;i++){
|
||||||
|
this.setStyle(style, i, y);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置列的默认样式
|
* 设置列的默认样式
|
||||||
*
|
*
|
||||||
@ -1104,6 +1126,33 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置整个列的样式 仅对数据单元格设置 write后调用
|
||||||
|
*
|
||||||
|
* {@link cn.hutool.poi.excel.ExcelWriter#setColumnStyle(int, org.apache.poi.ss.usermodel.CellStyle)}
|
||||||
|
* 这个方法加的样式会使整列没有数据的单元格也有样式
|
||||||
|
* 特别是加背景色时很不美观 且有数据的单元格样式会被StyleSet中的样式覆盖掉
|
||||||
|
* @param x 列的索引
|
||||||
|
* @param y 行的索引
|
||||||
|
* @param style
|
||||||
|
* @return
|
||||||
|
* @since
|
||||||
|
*/
|
||||||
|
public ExcelWriter setColumnStyleIfColumnData(int x,int y, CellStyle style) {
|
||||||
|
if(x < 0) {
|
||||||
|
throw new IllegalArgumentException("Invalid column number (" + x + ")");
|
||||||
|
}
|
||||||
|
if(y < 0) {
|
||||||
|
throw new IllegalArgumentException("Invalid row number (" + y + ")");
|
||||||
|
}
|
||||||
|
int rowCount = this.getRowCount();
|
||||||
|
int i = y;
|
||||||
|
for(;i<rowCount;i++){
|
||||||
|
this.setStyle(style, x, i);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建字体
|
* 创建字体
|
||||||
*
|
*
|
||||||
|
@ -38,6 +38,49 @@ import java.util.TreeMap;
|
|||||||
* @author looly
|
* @author looly
|
||||||
*/
|
*/
|
||||||
public class ExcelWriteTest {
|
public class ExcelWriteTest {
|
||||||
|
@Test
|
||||||
|
// @Ignore
|
||||||
|
public void testRowOrColumnCellStyle(){
|
||||||
|
List<?> row1 = CollUtil.newArrayList("aaaaa", "bb", "cc", "dd", DateUtil.date(), 3.22676575765);
|
||||||
|
List<?> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1", DateUtil.date(), 250.7676);
|
||||||
|
List<?> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2", DateUtil.date(), 0.111);
|
||||||
|
List<?> row4 = CollUtil.newArrayList("aa3", "bb3", "cc3", "dd3", DateUtil.date(), 35);
|
||||||
|
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");
|
||||||
|
|
||||||
|
overtimeWriter.write(rows,true);
|
||||||
|
|
||||||
|
CellStyle cellStyle = overtimeWriter.getWorkbook().createCellStyle();
|
||||||
|
StyleUtil.setBorder(cellStyle, BorderStyle.THIN, IndexedColors.BLACK);
|
||||||
|
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||||
|
cellStyle.setFillForegroundColor((short)13);
|
||||||
|
cellStyle.setDataFormat((short)22);//时间格式
|
||||||
|
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||||
|
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||||
|
|
||||||
|
//原设置行、列样式的方法
|
||||||
|
// overtimeWriter.setRowStyle(2,cellStyle);
|
||||||
|
// overtimeWriter.setColumnStyle(1,cellStyle);
|
||||||
|
|
||||||
|
//现增加的设置行、列样式的方法
|
||||||
|
//给第三行加背景色
|
||||||
|
overtimeWriter.setRowStyleIfRowData(2,cellStyle);
|
||||||
|
//给第二列加背景色 从第一行开始加(用于控制有表头时)
|
||||||
|
overtimeWriter.setColumnStyleIfColumnData(1,0,cellStyle);
|
||||||
|
|
||||||
|
CellStyle cellStyle1 = overtimeWriter.getWorkbook().createCellStyle();
|
||||||
|
StyleUtil.setBorder(cellStyle1, BorderStyle.THIN, IndexedColors.BLACK);
|
||||||
|
cellStyle1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||||
|
cellStyle1.setFillForegroundColor((short)13);
|
||||||
|
cellStyle1.setDataFormat((short)2);//小数保留两位
|
||||||
|
cellStyle1.setAlignment(HorizontalAlignment.CENTER);
|
||||||
|
cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||||
|
overtimeWriter.setStyle(cellStyle1,5,2);//由于第6列是数字 上面应用了日期格式会错乱,这里单独设置下第六列的格式
|
||||||
|
|
||||||
|
overtimeWriter.close();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
|
Loading…
x
Reference in New Issue
Block a user