当前位置:网站首页>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 !
边栏推荐
- How to take the mold for the picture of 1.54 inch TFT st7789 LCD screen
- Banana pie bpi-m5 toss record (3) -- compile BSP
- DOM node type
- Learning notes - talking about the data structure and algorithm of MySQL index and the introduction of index
- Three ways to solve your performance management problems
- JS foundation -- object static method
- Web -- JDBC tool class writing
- Unity refers to a variable in another class (its own instance)
- ES6 - study notes
- JS password combination rule - 8-16 digit combination of numbers and characters, not pure numbers and pure English
猜你喜欢

NVM installation and use

Mark down learning

Wechat sports field reservation of the finished works of the applet graduation project (7) mid-term inspection report

Domestic edge computing organization and product research

Preliminary foundation JVM

How to use two queues to simulate the implementation of a stack

Learning record XIII

Learning record 10

Mid year summary and personal feelings

C: wechat chat software instance (wpf+websocket+webapi+entityframework)
随机推荐
Learning Record V
Vscode copy synchronization plug-in expansion
JS interview question - what is the difference between Es5 and ES6?
Backtracking to solve subset problem
From input URL to page presentation
Openlayers draw circles and ellipses
05 - MVVM model
Edit mathematical formulas in markdown
JS foundation -- JSON
Beginners must see the markdown User Guide
Daily three questions 7.16
mysql_ Record the executed SQL
Ffmpeg 4.3 add custom demuxer
Concurrent programming day01
DOM operation -- get elements and nodes
Brief understanding of operational amplifier
Bubble sort / heap sort
Domestic edge computing organization and product research
Dc-2-range practice
JS foundation -- hijacking of this keyword