Easypoi report making tool is easy to operate and easy to use. It is different from that of POI report export, so you need to write your own corresponding tool class. Easypoi export is divided into three parts: basic export, template export and HTML export. The basic export is summarized first.
Basic export: we can export pictures. If the entity attribute contains entities or sets, let's see an example first.
Export step 1: add maven's dependency
<!--easypoi export excel rely on--> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.2.0</version> </dependency>
Step 2: add annotation to entity class
package com.xash.quartzDemo.entity; import java.util.List; import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.ExcelCollection; import cn.afterturn.easypoi.excel.annotation.ExcelTarget; @ExcelTarget("sysPERMISSION") @Data public class SysPermission { @Excel(name="Primary key id",needMerge = true) private int id; @Excel(name="Jurisdiction",orderNum="4",needMerge = true) private String permission; @Excel(name="describe",needMerge = true) private String description; @Excel(name="Role Association id",needMerge = true) private int rid; @Excel(name="Is it effective?",needMerge = true) private int available; @ExcelCollection(name="User roles") private List<SysRoles> role; }
The third step is to export excel. The specific code is as follows
@GetMapping("export1") public String export1(HttpServletRequest request,HttpServletResponse response) { List<SysPermission> list=permissionService.selectPermission(); Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("permissions information","Jurisdiction"), SysPermission .class, list); if(workbook==null) { return "fail"; } String excelName="ExportExcel"; // Reset response object response.reset(); // Current date, used to export file name SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String dateStr = "["+excelName+"-"+sdf.format(new Date())+"]"; // Specify file name to download -- set response header response.setHeader("Content-Disposition", "attachment;filename=" +dateStr+".xls"); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // Write out the data output flow to the page try { OutputStream output = response.getOutputStream(); BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output); workbook.write(bufferedOutPut); bufferedOutPut.flush(); bufferedOutPut.close(); output.close(); } catch (IOException e) { e.printStackTrace(); } return "index4"; }
Note: I use get request here. The back-end report is wrong, but the export report is normal. I need to use post request.
Meaning of specific notes: as follows
attribute | type | Default value | function |
---|---|---|---|
name |
String |
null |
Column name, support name ﹣ ID |
needMerge |
boolean |
fasle |
Whether to merge cells vertically (used to contain a single cell in the list and multiple rows created by merging the list) |
orderNum |
String |
"0" |
Column sorting, support name ﹣ ID |
replace |
String[] |
{} |
The value replacement export is {a {ID, B {ID} import in turn |
savePath |
String |
"upload" |
The path to save the imported file can be filled in if it is a picture. The default is upload/className/ IconEntity, which corresponds to upload/Icon./ |
type |
int |
1 |
Export type 1 is text, 2 is picture, 3 is function, 10 is number, default is text |
width |
double |
10 |
Column width |
height |
double |
10 |
Column height. It is planned to use the height of @ ExcelTarget uniformly later. This will be discarded. Note |
isStatistics |
boolean |
fasle |
Automatic statistics. Add a line of statistics to output all the data. This process will swallow the exception. Please pay attention to this point. |
isHyperlink |
boolean |
false |
Hyperlink, if you need to implement the interface return object |
isImportField |
boolean |
true |
Check the field to see if it exists in the imported Excel. If there is no Excel indicating that it is wrong, the reading fails, and the name UU ID is supported. |
exportFormat |
String |
"" |
The time format of the export determines whether the date needs to be formatted based on whether it is empty. |
importFormat |
String |
"" |
The time format of the import determines whether to format the date based on whether it is empty. |
format |
String |
"" |
Time format, equivalent to setting both exportFormat and importFormat |
databaseFormat |
String |
"yyyyMMddHHmmss" |
Export time settings. If the field is of Date type, you do not need to set the database. If the field is of string type, you need to set the database format to convert the time format for output. |
numFormat |
String |
"" |
Number format, the parameter is Pattern, and the object used is DecimalFormat |
imageType |
int |
1 |
Export type 1 read from file 2 read from database by default file import is the same |
suffix |
String |
"" |
Text suffix, such as% 90 becomes 90% |
isWrap |
boolean |
true |
Line wrap is supported \ n |
mergeRely |
int[] |
{} |
Merge cell dependencies. For example, if the second column is based on the first column, then {0} is OK. |
mergeVertical |
boolean |
fasle |
Merge cells with the same content vertically |
fixedIndex |
int |
-1 |
Column corresponding to excel, name ignored |
isColumnHidden |
boolean |
false |
Export hidden columns |
@ExcelTarget
A comment that defines a everywhere entity, as well as some general settings, acts on the outermost entity
attribute | type | Default value | function |
---|---|---|---|
value |
String |
null |
Define ID |
height |
double |
10 |
Set row height |
fontSize |
short |
11 |
Set text size |
@ExcelEntity
Whether the tag is exported to excel and marked as entity class is an internal attribute class. You can define the internal id to determine whether the tag continues to penetrate.
attribute | type | Default value | function |
---|---|---|---|
id |
String |
null |
Define ID |
@ExcelCollection
One to many set annotation to mark whether the set is sorted by data and the whole set
attribute | type | Default value | function |
---|---|---|---|
id |
String |
null |
Define ID |
name |
String |
null |
Define the set column name, support nanm? ID |
orderNum |
int |
0 |
Sorting, support name "Id" |
type |
Class<?> |
ArrayList.class |
Create objects on import using |
@ExcelIgnore
Ignore this property, and use it more in circular reference.
Now we will introduce the usage of an @ ExcelCollection annotation. demo is as follows
package com.xash.quartzDemo.entity; import java.util.List; import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.ExcelCollection; import cn.afterturn.easypoi.excel.annotation.ExcelTarget; @ExcelTarget("sysPERMISSION") @Data public class SysPermission { @Excel(name="Primary key id",needMerge = true) private int id; @Excel(name="Jurisdiction",orderNum="4",needMerge = true) private String permission; @Excel(name="describe",needMerge = true) private String description; @Excel(name="Role Association id",needMerge = true) private int rid; @Excel(name="Is it effective?",needMerge = true) private int available; @ExcelCollection(name="User roles") private List<SysRoles> role; } package com.xash.quartzDemo.entity; import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.ExcelTarget; import lombok.Data; @Data @ExcelTarget("Role information") public class SysRoles { @Excel(name="id") private int id; @Excel(name="role") private String role; @Excel(name="describe") private String description; @Excel(name="father id") private int pid; @Excel(name="Is it effective?") private int available; }
Specific export method:
/** * excel Export function */ @GetMapping("export") //@ResponseBody public String exportPerssionTable(HttpServletRequest request,HttpServletResponse response) { String rid=request.getParameter("rid"); List<SysPermission> list=permissionService.selectPermission(); List<SysPermission>list1=new ArrayList<>(); for(SysPermission permission:list) { List <SysRoles> rolelist=new ArrayList<>(); for(int i=0;i<3;i++) { SysRoles role = new SysRoles(); role.setId(i); role.setAvailable(1); role.setDescription("Administrators"); role.setPid(0); role.setRole("system administrator"); rolelist.add(role); } permission.setRole(rolelist); list1.add(permission); } // Workbook workbook1=new ExportSheet().exportSheets(rid, list1); Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("2412312", "test", "test"), SysPermission.class, list1); if(workbook==null) { return "fail"; } String excelName="test ExportExcel"; // Reset response object response.reset(); // Current date, used to export file name SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String dateStr = "["+excelName+"-"+sdf.format(new Date())+"]"; // Specify file name to download -- set response header response.setHeader("Content-Disposition", "attachment;filename=" +dateStr+".xls"); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // Write out the data output flow to the page try { OutputStream output = response.getOutputStream(); BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output); workbook.write(bufferedOutPut); bufferedOutPut.flush(); bufferedOutPut.close(); output.close(); } catch (IOException e) { e.printStackTrace(); } return "success"; }
In this way, you can enter the corresponding url to access and download
Next, you can easily export the pictures as follows
Create an entity class
package com.xash.quartzDemo.entity; import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.ExcelTarget; import lombok.Data; @ExcelTarget("PicEntity") @Data public class PicEntity { @Excel(name="Scenery") private String name; @Excel(name="address") private String address; @Excel(name="Design sketch" ,type=2,width=40,height=20,imageType=2) private Byte[] picCode; @Excel(name="Design sketch" ,type=2,width=40,height=20,imageType=1) private String picSrc; public PicEntity(String name, String address, String picSrc) { super(); this.name = name; this.address = address; this.picSrc = picSrc; } public PicEntity(String name, String address, Byte[] picCode) { super(); this.name = name; this.address = address; this.picCode = picCode; } }
Prepare the image resources and put them in the path specified by yourself: D:/google/3.jpg
Start exporting
@GetMapping("exportpic") public String exportpic(HttpServletResponse response) { List<PicEntity> list=new ArrayList<>(); list.add(new PicEntity("As boundless as the sea and sky", "Hainan Island", "D:/google/3.jpg")); list.add(new PicEntity("One page of a boat", "Jiangsu", "D:/google/2.jpg")); list.add(new PicEntity("Ten mile peach blossom", "Mount Huangshan", "D:/google/1.jpg")); ExportParams params=new ExportParams("Landscape painting","Appreciate"); params.setStyle(ExcelStyleUtil.class); Workbook workbook=ExcelExportUtil.exportExcel(params,PicEntity.class, list); if(workbook==null) { return "fail"; } ExcelExportStylerBorderImpl style =new ExcelExportStylerBorderImpl(workbook); style.getTitleStyle((short)200); style.getHeaderStyle((short)200); String excelName="ExportExcel"; // Reset response object response.reset(); // Current date, used to export file name SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String dateStr = "["+excelName+"-"+sdf.format(new Date())+"]"; // Specify file name to download -- set response header response.setHeader("Content-Disposition", "attachment;filename=" +dateStr+".xls"); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // Write out the data output flow to the page try { OutputStream output = response.getOutputStream(); BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output); workbook.write(bufferedOutPut); bufferedOutPut.flush(); bufferedOutPut.close(); output.close(); } catch (IOException e) { e.printStackTrace(); } return "index4"; }
type =2 the field type is picture, imageType=1 (it can be left blank by default), which means read from file. If the field type is a string type, you can use relative path or absolute path, and the absolute path takes precedence.
It is better to set the width and height of the image cell, which will be 100% scaled to the size of the cell, not the original size. Here, pay attention to the following
For specific connection, please refer to the official website: http://easypoi.mydoc.io/