当前位置:网站首页>Easyexcel sets the style of the last row [which can be expanded to each row]
Easyexcel sets the style of the last row [which can be expanded to each row]
2022-07-25 03:13:00 【Sleeping bamboo】
1、 Summary
This article mainly introduces the style setting of the last row of the table , According to the content of this article , Expand to the style setting of each line
2、 Demand is introduced
The original table is as follows , Now simply want to bold the total line

3、 Realization
stay Excel in , Set the style of the row by WriteCellStyle Object , You can see from the picture above , Now the generated table , The style of each line is consistent , The original style code is as follows :
// Table content style
WriteCellStyle bodyStyle = new WriteCellStyle();
// Font style
bodyStyle.setWriteFont(ExcelUtil.setFont(12, "Arial", false));
// Set borders
// bodyStyle.setBorderTop(BorderStyle.DOUBLE);
bodyStyle.setBorderLeft(BorderStyle.THIN);
bodyStyle.setBorderRight(BorderStyle.THIN);
bodyStyle.setBorderBottom(BorderStyle.THIN);
// Set style policy headStyle It's the head style , I'm not going to write it here
HorizontalCellStyleStrategy dataTableStrategy = new HorizontalCellStyleStrategy(headStyle, bodyStyle);setFont The method is my modified text style method , The code is as follows :
public class ExcelUtil{
/**
* @purpose Set the font
* @parameter name by null Indicates that the font is not set
*/
public static WriteFont setFont(int size, String name, boolean condition) {
WriteFont font = new WriteFont();
// size
font.setFontHeightInPoints((short) size);
// typeface
if (!ObjectUtils.isEmpty(name)) {
font.setFontName(name);
}
// Is it bold
font.setBold(condition);
return font;
}
}Transformation begins :
So let's see first Excel Of HorizontalCellStyleStrategy The source code of the strategy , You can see that easyExcel How to implement the style of each line , Source code is as follows :

Let's take a look at my previous style implementation , call HorizontalCellStyleStrategy What is the construction method of ?
public HorizontalCellStyleStrategy(WriteCellStyle headWriteCellStyle, WriteCellStyle contentWriteCellStyle) {
this.headWriteCellStyle = headWriteCellStyle;
if (contentWriteCellStyle != null) {
this.contentWriteCellStyleList = ListUtils.newArrayList(contentWriteCellStyle);
}
}Look at the source code if Code in statement :
this.contentWriteCellStyleList = ListUtils.newArrayList(contentWriteCellStyle);
That's why when I use this code , The style of each line is consistent :
new HorizontalCellStyleStrategy(headStyle, bodyStyle);
a key 1:
easyExcel The table style I set bodyStyle Perform traversal replication ! Generate a list, So as to achieve the style effect of each line
a key 2:
HorizontalCellStyleStrategy There is also a construction method , Support the direct input of a style collection List<WriteCellStyle> , To style each line !
public HorizontalCellStyleStrategy(WriteCellStyle headWriteCellStyle,
List<WriteCellStyle> contentWriteCellStyleList) {
this.headWriteCellStyle = headWriteCellStyle;
this.contentWriteCellStyleList = contentWriteCellStyleList;
}Is there a way of thinking ? To achieve it.
Code transformation :
// Table content style
WriteCellStyle bodyStyle = new WriteCellStyle();
// Font style
bodyStyle.setWriteFont(ExcelUtil.setFont(12, "Arial", false));
// Set borders
// bodyStyle.setBorderTop(BorderStyle.DOUBLE);
bodyStyle.setBorderLeft(BorderStyle.THIN);
bodyStyle.setBorderRight(BorderStyle.THIN);
bodyStyle.setBorderBottom(BorderStyle.THIN);
// 1> Create the style of the last line
WriteCellStyle lastRow = new WriteCellStyle();
// 2> Set bold , Keep consistent with other styles
lastRow.setWriteFont(ExcelUtil.setFont(12, "Arial", true));
lastRow.setBorderLeft(BorderStyle.THIN);
lastRow.setBorderRight(BorderStyle.THIN);
lastRow.setBorderBottom(BorderStyle.THIN);
// 3> According to the total number of rows ], Overlay to generate the style of the last line List
List<WriteCellStyle> preList = new ArrayList<>();
// 4>data Is the generated table data set ,data.size()-1 Indicates the number of rows except the last row
for (int i = 0; i < data.size()- 1; i++) {
preList.add(bodyStyle);
}
// 5> Add the style of the last line
preList.add(lastRow);
// 6> Create a strategy ,headStyle It's the head style , I'm not going to write it here
HorizontalCellStyleStrategy dataTableStrategy = new HorizontalCellStyleStrategy(headStyle, preList);effect :

The last line is bold !
4、 Development train of thought
This article just sets the style of the last line , In actual development , We can customize the style of each line , Of course , In practice , If there is 20 That's ok , It's impossible to set different colors for each line , It is usually an odd number of lines in one style , Even lines have one style ! The method is similar to the above code , Just judge Is it strange ( accidentally ) Number identification , Generate list Style collection !
边栏推荐
- Publish the project online and don't want to open a port
- mysql_ Backup restore_ Specify table_ Backup table_ Restore table_ innobackup
- Select sort / cardinality sort
- [stm32f103rct6] motor PWM drive module idea and code
- Enter an integer and a binary tree
- Dynamic planning of force buckle punch in summary
- Implementation principle of virtual DOM
- Vscode configuration, eslint+prettier combined with detailed configuration steps, standardized development
- Use and introduction of vim file editor
- mysql_ Record the executed SQL
猜你喜欢

Learning record 12

Learning record XIII

Vulntarget vulnerability shooting range -vulntarget-b

05 - MVVM model

Bgy development small example

Method of adding kernel in Jupiter notebook

Riotboard development board series notes (VIII) -- building desktop system

Keil compile download error: no algorithm found for: 08000000h - 08001233h solution

Riotboard development board series notes (4) -- using Vpu hardware decoding

Selenium framework operation steelth.min.js file hides browser fingerprint features
随机推荐
Implementation principle of virtual DOM
Mark down learning
hello csdn
Download the jar package of jsqlparser and PageHelper
JS written test questions -- random numbers, array de duplication
Map set learning
List title of force buckle summary
JS method encapsulation summary
Page performance: how to optimize pages systematically?
mysql_ User table_ Field meaning
Question D: pruning shrubs
Canvas record
Unified return data format
Test question f: statistical submatrix
Riotboard development board series notes (V) -- porting u-boot
Review all frames before sum of SSM frames
Publish the project online and don't want to open a port
Enter an integer and a binary tree
Use of stm32cubemonitor Part II - historical data storage and network access
Openlayers ol ext: Transform object, rotate, stretch, zoom in