当前位置:网站首页>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
边栏推荐
- [learning records of erudite Valley] Super summary, attentive sharing | collection
- MOOC翁恺 C语言 第三周:判断与循环:2.循环
- 分解路径为目录名和文件名的方法
- Operation document tree
- MySQL installation and use
- VLAN的配置
- Starting point Chinese website font anti crawling technology web page can display numbers and letters, and the web page code is garbled or blank
- Repair the faulty sector
- MOOC翁恺C语言 第六周:数组与函数:1.数组2.函数的定义与使用3.函数的参数和变量4.二维数组
- Implementation method of converting ast into word vector before converting word vector
猜你喜欢
随机推荐
Shell--第一天作业
DOM Foundation
Wechat applet custom compilation mode
360 compatibility issues
ES6 add -- > object
Standard C language learning summary 3
RAID磁盘阵列
MOOC翁恺 C语言 第三周:判断与循环:2.循环
metasploit渗透ms7_010练习
About gcc:multiple definition of
PXE无人值守安装管理
Implementation method of converting ast into word vector before converting word vector
JSON notes
Construction of Yum warehouse
Custom component -- communication between parent and child components
DHCP服务
Understanding of C language EOF
Applet navigator cannot jump (debug)
Shell script - sort, uniq, TR, array sort, cut, Eval command configuration
MOOC Weng Kai C language week 3: judgment and cycle: 1. Judgment









