easyexcel A new idea of mine, 我的一个新想法

kgsdhlau  于 2个月前  发布在  其他
关注(0)|答案(1)|浏览(25)

A new idea of mine

When I solved the problem of #3481 , I found that it is very troublesome to implement this business function. It is recommended that a new feature be added to this product, which is to use the following code to set the header styles of each column at the same time when inserting, such as color background, etc.

EasyExcel.write("example1.xlsx", TeamInfo.class)
                .withTemplate(excelFilePath)
                .sheet("Sheet1").needHead(false).doWrite(list);

我的一个新想法

​ 在我解决 #3481 问题的时候,我发现如果实现这个业务功能是十分麻烦的。建议这个产品增加一个新的功能就是使用以下代码进行插入的时候可以设置同时设置每个列的标题头各个的样式,如颜色背景等。

EasyExcel.write("example1.xlsx", TeamInfo.class)
                .withTemplate(excelFilePath)
                .sheet("Sheet1").needHead(false).doWrite(list);
oewdyzsn

oewdyzsn1#

// Example code to set header styles for each column
Sheet sheet = workbook.getSheetAt(0); // Assuming you have a workbook
Row headerRow = sheet.getRow(0); // Assuming the header is in the first row

CellStyle headerCellStyle = workbook.createCellStyle();
headerCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
headerCellStyle.setFont(font);

for (int columnIndex = 0; columnIndex < headerRow.getLastCellNum(); columnIndex++) {
Cell cell = headerRow.getCell(columnIndex);
cell.setCellStyle(headerCellStyle);
}

// Then you can proceed with inserting your data using EasyExcel
EasyExcel.write("example1.xlsx", TeamInfo.class)
.withTemplate(excelFilePath)
.sheet("Sheet1").needHead(false).doWrite(list);

相关问题