一、準備工作
1、pom依賴
在pom.xml中加入POI的依賴
1
2
3
4
5
6
7
8
9
10
|
< dependency > < groupId >org.apache.poi</ groupId > < artifactId >poi-ooxml</ artifactId > < version >3.11-beta1</ version > </ dependency > < dependency > < groupId >org.apache.poi</ groupId > < artifactId >poi-ooxml-schemas</ artifactId > < version >3.11-beta1</ version > </ dependency > |
2、自定義注解
自定義注解,用于定義excel單元格的相關信息,用在需要導出的類上。
大家可以根據自己的實際需求來定義更多的內容。
1
2
3
4
5
6
7
8
9
10
11
12
|
@Retention (RetentionPolicy.RUNTIME) int order() default 9999 ; //定義字段在excel的單元格列坐標位置 String title() default "" ; //定義列坐標對應的標題 int cloumn() default 100 ; //定義列寬 String pattern() default "" ; //定義日期顯示格式 } |
3、定義需要導出的實體
舉例說明@ExcelResources 的應用場景,我們創建一個demoModel,包含姓名、年齡、性別、日期。
后邊的excel導出例子也采用這個實體類來舉例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Data public class ExcelDemoModel { @ExcelResources (order= 0 ,title = "姓名" ,cloumn = 10 ) private String name; @ExcelResources (order= 1 ,title = "年齡" ,cloumn = 10 ) private Integer age; @ExcelResources (order= 2 ,title = "創建時間" ,cloumn = 24 ,pattern = "yyyy-MM-dd HH:mm:ss" ) private Date createTime; @ExcelResources (order= 3 ,title = "性別" ,cloumn = 10 ) private SexType sex; //枚舉 } |
4、定義導出輔助類
用于存放導出的excel對應標題和列寬
1
2
3
4
5
6
7
8
9
|
@Data @NoArgsConstructor @AllArgsConstructor public class TitleAndCloumn { private String title; //標題 private int cloumn; //列寬 } |
二、具體的導出方法
1、導出主要方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
@Service public class ExcelService { private static float title_row_height= 30 ; //標題行高 private static float data_row_height= 25 ; //數據行高 public void exportExcel(HttpServletRequest request, HttpServletResponse response, String fileName ,List<?> excelDatas,Class<?> clz ) { try { HSSFWorkbook resultWb= new HSSFWorkbook(); HSSFSheet sheet=resultWb.createSheet(); //創建sheet //根據類類型信息獲取導出的excel對應的標題和列寬 key-列號,value-標題和列寬 HashMap<Integer, TitleAndCloumn> orderTitleAndCloumnMap=getTitleAndCloumnMap(clz); //設置列寬 orderTitleAndCloumnMap.forEach((k,v) -> { sheet.setColumnWidth(k, v.getCloumn()* 256 ); }); HSSFRow row0=sheet.createRow( 0 ); //設置標題行高 row0.setHeightInPoints(title_row_height); //創建標題單元格格式 HSSFCellStyle titleCellStyle=getCellStyle(resultWb, 11 , true ,HSSFColor.BLACK.index); //填充標題行內容 orderTitleAndCloumnMap.forEach((k,v) -> { HSSFCell row0Cell=row0.createCell(k); row0Cell.setCellValue(v.getTitle()); row0Cell.setCellStyle(titleCellStyle); }); //創建正文單元格格式 HSSFCellStyle dataStyle = getCellStyle(resultWb, 11 , false ,HSSFColor.BLACK.index); //將正文轉換為excel數據 int rowNum= 1 ; for (Object data:excelDatas){ HSSFRow row=sheet.createRow(rowNum++); row.setHeightInPoints(data_row_height); //獲取對象值 key-列號 value-String值 HashMap<Integer,String> orderValueMap=getValueMap(data); orderValueMap.forEach((k,v) ->{ HSSFCell cell=row.createCell(k); cell.setCellValue(v); cell.setCellStyle(dataStyle); } ); } String downFileName=fileName+ ".xls" ; response.setContentType( "application/vnd.ms-excel; charset=UTF-8" ); // application/x-download response.setHeader( "Content-Disposition" , "attachment; " +encodeFileName(request, downFileName)); OutputStream outputStream = response.getOutputStream(); resultWb.write(outputStream); outputStream.flush(); outputStream.close(); resultWb.close(); } catch (Exception e1) { e1.printStackTrace(); } } } |
2、通過反射獲取excel標題和列寬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
/** * 獲取類的屬性對應單元格標題和列寬 * @param * @return */ private static HashMap<Integer, TitleAndCloumn> getTitleAndCloumnMap(Class<?> clz) { HashMap<Integer, TitleAndCloumn> orderTitleAndCloumnMap= new HashMap<>(); Field[] fs = clz.getDeclaredFields(); for (Field f:fs) { f.setAccessible( true ); if (f.isAnnotationPresent(ExcelResources. class )) { Integer order=f.getAnnotation(ExcelResources. class ).order(); String id="codetool">
3、創建CellStyle 通過傳入參數定義簡單地CellStyle
4、通過反射獲取對象信息并處理成String字符串 我這里只涉及到基本數據類型和Date以及枚舉的值獲取和轉換,小伙伴可以根據自己的實際情況進行修改。
5、枚舉的定義 如果有用到枚舉存儲在數據庫的小伙伴,可以自定義枚舉的toString方法來實現excel導出時候相應的內容
6、encodeFileName
三、方法調用案例 1、方法調用
2、導出效果
到此這篇關于SpringBoot整合POI導出通用Excel的方法示例的文章就介紹到這了,更多相關SpringBoot整合POI導出Excel內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家! 原文鏈接:https://blog.csdn.net/mengdi_cao/article/details/108143004 延伸 · 閱讀
精彩推薦
|