当前位置:网站首页>Easypoi one to many, merge cells, and adapt the row height according to the content
Easypoi one to many, merge cells, and adapt the row height according to the content
2022-07-28 07:08:00 【Fu Hua-】
Make a note of
effect



One 、 Introduce dependencies
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.1.3</version>
</dependency>
Two 、 Entity class
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import lombok.Data;
import java.util.List;
@Data
public class TestExportMainVo {
@Excel(name = " project ",width = 20,needMerge = true)
private String project;
@ExcelCollection(name = "")
private List<TestExportSub1Vo> sub1VoList;
public TestExportMainVo(String project, List<TestExportSub1Vo> sub1VoList) {
this.project = project;
this.sub1VoList = sub1VoList;
}
}
@Data
public class TestExportSub1Vo {
@Excel(name = " Serial number ",width = 8,needMerge = true)
private String sort;
@Excel(name = " basis ",width = 30,needMerge = true)
private String basis;
@ExcelCollection(name = "")
private List<TestExportSub2Vo> sub2VoList;
public TestExportSub1Vo(String sort, String basis, List<TestExportSub2Vo> sub2VoList) {
this.sort = sort;
this.basis = basis;
this.sub2VoList = sub2VoList;
}
}
@Data
public class TestExportSub2Vo {
@Excel(name = " Operation steps ",width = 60)
private String step;
@Excel(name = " Clause ",width = 12)
private String clause;
public TestExportSub2Vo(String step, String clause) {
this.step = step;
this.clause = clause;
}
}
@Excel In the annotations ,needMerge =true Used to merge cells
3、 ... and 、excel Tool class
1、 Style tool class ExcelExportStylerUitl
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
import org.apache.poi.ss.usermodel.*;
/** * @author fuHua * @date 2021 year 05 month 27 Japan 14:55 */
public class ExcelExportStylerUitl implements IExcelExportStyler {
private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT");
private static final short FONT_SIZE_TEN = 10;
private static final short FONT_SIZE_ELEVEN = 11;
private static final short FONT_SIZE_TWELVE = 12;
/** * Headline style */
private CellStyle headerStyle;
/** * Each column header style */
private CellStyle titleStyle;
/** * Data line style */
private CellStyle styles;
public ExcelExportStylerUitl(Workbook workbook) {
this.init(workbook);
}
/** * Initialize style * @param workbook */
private void init(Workbook workbook) {
this.headerStyle = initHeaderStyle(workbook);
this.titleStyle = initTitleStyle(workbook);
this.styles = initStyles(workbook);
}
/** * Headline style * @param color * @return */
@Override
public CellStyle getHeaderStyle(short color) {
return headerStyle;
}
/** * Each column header style * @param color * @return */
@Override
public CellStyle getTitleStyle(short color) {
return titleStyle;
}
/** * Data line style * @param parity Can be used to represent even and odd rows * @param entity The data content * @return style */
@Override
public CellStyle getStyles(boolean parity, ExcelExportEntity entity) {
return styles;
}
/** * Get style method * @param dataRow data row * @param obj object * @param data data */
@Override
public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) {
return getStyles(true, entity);
}
/** * The style settings used by the template */
@Override
public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) {
return null;
}
/** * initialization -- Headline style * @param workbook * @return */
private CellStyle initHeaderStyle(Workbook workbook) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, (short) 14, true));
return style;
}
/** * initialization -- Each column header style * @param workbook * @return */
private CellStyle initTitleStyle(Workbook workbook) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true));
// Background color
// style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
return style;
}
/** * initialization -- Data line style * @param workbook * @return */
private CellStyle initStyles(Workbook workbook) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, FONT_SIZE_ELEVEN, false));
style.setDataFormat(STRING_FORMAT);
return style;
}
/** * Basic style * @return */
private CellStyle getBaseCellStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
// Under the frame
style.setBorderBottom(BorderStyle.THIN);
// The left margin
style.setBorderLeft(BorderStyle.THIN);
// On the border
style.setBorderTop(BorderStyle.THIN);
// Right margin
style.setBorderRight(BorderStyle.THIN);
// Horizontal center
style.setAlignment(HorizontalAlignment.CENTER);
// Center up and down
style.setVerticalAlignment(VerticalAlignment.CENTER);
// Set auto wrap
style.setWrapText(true);
return style;
}
/** * Font style * @param size font size * @param isBold Is it bold * @return */
private Font getFont(Workbook workbook, short size, boolean isBold) {
Font font = workbook.createFont();
// Font style
font.setFontName(" Song style ");
// Is it bold
font.setBold(isBold);
// font size
font.setFontHeightInPoints(size);
return font;
}
}
2、 Export tool class ExcelUtil
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
/** * @author fuHua * @date 2021 year 06 month 04 Japan 14:09 */
public class ExcelUtil {
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, ExcelType type, boolean isOneToMany, HttpServletResponse response){
ExportParams exportParams = new ExportParams(title, sheetName,type);
exportParams.setStyle(ExcelExportStylerUitl.class);
Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
if (workbook != null);
// Judge whether it is one to many
if (isOneToMany){
setRowHeight(workbook);
}
downLoadExcel(fileName, response, workbook);
}
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean setRowHeight, HttpServletResponse response){
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setStyle(ExcelExportStylerUitl.class);
Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
if (workbook != null);
// Judge whether the line height is adaptive according to the content
if (setRowHeight){
Sheet sheet = workbook.getSheetAt(0);
for(int i = 0; i <= sheet.getLastRowNum(); i ++) {
Row row = sheet.getRow(i);
setRowHeight(row);
}
}
downLoadExcel(fileName, response, workbook);
}
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
/** * One to many , Set row height */
private static void setRowHeight(Workbook workbook){
Sheet sheet = workbook.getSheetAt(0);
// Set the first 4 The column width of the column is 60( Subscript from 0 Start ),TestExportSub2Vo I don't know why the column width is set but it doesn't work , It can only be set separately here
sheet.setColumnWidth(3,60*256);
for(int i = 0; i <= sheet.getLastRowNum(); i ++) {
Row row = sheet.getRow(i);
if (i==0){
// Set the height of the first row ( Table title )
row.setHeightInPoints(35);
}else if (i == 1){
// Set the height of the second row ( Table header )
row.setHeightInPoints(25);
}else {
// Set the row height of other rows to be adaptive according to the content
setRowHeight(row);
}
}
}
private static void setRowHeight(Row row){
// Set the line height according to the content length
int enterCnt = 0;
for(int j = 0; j < row.getPhysicalNumberOfCells(); j ++) {
int rwsTemp = row.getCell(j).toString().length();
// Here, take the characters of the column with the largest character length in each row
if (rwsTemp > enterCnt) {
enterCnt = rwsTemp;
}
}
// Set the default row height to 35
row.setHeightInPoints(35);
// If the character length is greater than 35, How many times larger is the judgment , Set the corresponding row height according to the multiple
if (enterCnt>35){
float d = enterCnt/35;
float f = 35*d;
/*if (d>2 && d<4){ f = 35*2; }else if(d>=4 && d<6){ f = 35*3; }else if (d>=6 && d<8){ f = 35*4; }*/
row.setHeightInPoints(f);
}
}
}
as for easyexcel To merge cells , It has no image. easypoi equally , There is a property that controls the merge ,easyexcel You need to write your own code to control . Then, if the line height is adaptive according to the content ,easyexcel It should be similar to this , Haven't studied , Go and have a look next time
边栏推荐
- 登录进oracle10g的oem,想管理监听程序却总是弹出帐号密码输入页面
- Wangeditor (@4.7.15) - lightweight rich text editor
- DOM - Events
- Icc2 analysis timing artifact analyze_ design_ violations
- Starting point Chinese website font anti crawling technology web page can display numbers and letters, and the web page code is garbled or blank
- easypoi一对多,合并单元格,并且根据内容自适应行高
- DHCP service
- The.Joernindex database has no content after Joern runs
- Result fill in the blank (dfs*c language)
- Blue bridge code error ticket
猜你喜欢

kali下安装nessus

静态和浮动路由

Sysevr environment configuration: joern-0.3.1, neo4j-2.1.5, py2neo2.0

Applet custom components - data, methods, and properties

Event_ Loop event loop mechanism

joern运行后.joernindex数据库无内容

主动扫描技术nmap详解

VLAN configuration

Group management and permission management

codesensor:将代码转化为ast后再转化为文本向量
随机推荐
远程访问云服务器上Neo4j等服务的本地网址
joern运行后.joernindex数据库无内容
DHCP service
codesensor:将代码转化为ast后再转化为文本向量
DOM -- page rendering, style attribute operation, preloading and lazy loading, anti shake and throttling
Neo4j运行报错Error occurred during initialization of VM Incompatible minimum and maximum heap sizes spec
Bond mode configuration
DOM -- event chain, event bubble and capture, event proxy
DOM Foundation
Canvas drawing 1
Erudite Valley Learning Records] Super summary, attentive sharing | common APIs
Ubuntu MySQL setting remote access permissions
shell---条件语句练习
DHCP服务
起点中文网 字体反爬技术 网页可以显示数字字母 网页代码是乱码或空格
YUM仓库的搭建
Asynchronous programming promise
JS data type detection and modification detection
Wechat applet custom compilation mode
ES6 add -- > object