Просмотр исходного кода

Excel注解支持导入导出标题信息

RuoYi лет назад: 4
Родитель
Сommit
f4a2f909a7

+ 110 - 36
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@@ -36,6 +36,7 @@ import org.apache.poi.ss.usermodel.Sheet;
36
 import org.apache.poi.ss.usermodel.VerticalAlignment;
36
 import org.apache.poi.ss.usermodel.VerticalAlignment;
37
 import org.apache.poi.ss.usermodel.Workbook;
37
 import org.apache.poi.ss.usermodel.Workbook;
38
 import org.apache.poi.ss.usermodel.WorkbookFactory;
38
 import org.apache.poi.ss.usermodel.WorkbookFactory;
39
+import org.apache.poi.ss.util.CellRangeAddress;
39
 import org.apache.poi.ss.util.CellRangeAddressList;
40
 import org.apache.poi.ss.util.CellRangeAddressList;
40
 import org.apache.poi.util.IOUtils;
41
 import org.apache.poi.util.IOUtils;
41
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
42
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
@@ -104,6 +105,16 @@ public class ExcelUtil<T>
104
     private List<Object[]> fields;
105
     private List<Object[]> fields;
105
 
106
 
106
     /**
107
     /**
108
+     * 当前行号
109
+     */
110
+    private int rownum;
111
+    
112
+    /**
113
+     * 标题
114
+     */
115
+    private String title;
116
+
117
+    /**
107
      * 最大高度
118
      * 最大高度
108
      */
119
      */
109
     private short maxHeight;
120
     private short maxHeight;
@@ -128,7 +139,7 @@ public class ExcelUtil<T>
128
         this.clazz = clazz;
139
         this.clazz = clazz;
129
     }
140
     }
130
 
141
 
131
-    public void init(List<T> list, String sheetName, Type type)
142
+    public void init(List<T> list, String sheetName, String title, Type type)
132
     {
143
     {
133
         if (list == null)
144
         if (list == null)
134
         {
145
         {
@@ -137,8 +148,27 @@ public class ExcelUtil<T>
137
         this.list = list;
148
         this.list = list;
138
         this.sheetName = sheetName;
149
         this.sheetName = sheetName;
139
         this.type = type;
150
         this.type = type;
151
+        this.title = title;
140
         createExcelField();
152
         createExcelField();
141
         createWorkbook();
153
         createWorkbook();
154
+        createTitle();
155
+    }
156
+
157
+    /**
158
+     * 创建excel第一行标题
159
+     */
160
+    public void createTitle()
161
+    {
162
+        if (StringUtils.isNotEmpty(title))
163
+        {
164
+            Row titleRow = sheet.createRow(rownum == 0 ? rownum++ : 0);
165
+            titleRow.setHeightInPoints(30);
166
+            Cell titleCell = titleRow.createCell(0);
167
+            titleCell.setCellStyle(styles.get("title"));
168
+            titleCell.setCellValue(title);
169
+            sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(),
170
+                    this.fields.size() - 1));
171
+        }
142
     }
172
     }
143
 
173
 
144
     /**
174
     /**
@@ -149,33 +179,36 @@ public class ExcelUtil<T>
149
      */
179
      */
150
     public List<T> importExcel(InputStream is) throws Exception
180
     public List<T> importExcel(InputStream is) throws Exception
151
     {
181
     {
152
-        return importExcel(StringUtils.EMPTY, is);
182
+        return importExcel(is, 0);
183
+    }
184
+
185
+    /**
186
+     * 对excel表单默认第一个索引名转换成list
187
+     * 
188
+     * @param is 输入流
189
+     * @param titleNum 标题占用行数
190
+     * @return 转换后集合
191
+     */
192
+    public List<T> importExcel(InputStream is, int titleNum) throws Exception
193
+    {
194
+        return importExcel(StringUtils.EMPTY, is, titleNum);
153
     }
195
     }
154
 
196
 
155
     /**
197
     /**
156
      * 对excel表单指定表格索引名转换成list
198
      * 对excel表单指定表格索引名转换成list
157
      * 
199
      * 
158
      * @param sheetName 表格索引名
200
      * @param sheetName 表格索引名
201
+     * @param titleNum 标题占用行数
159
      * @param is 输入流
202
      * @param is 输入流
160
      * @return 转换后集合
203
      * @return 转换后集合
161
      */
204
      */
162
-    public List<T> importExcel(String sheetName, InputStream is) throws Exception
205
+    public List<T> importExcel(String sheetName, InputStream is, int titleNum) throws Exception
163
     {
206
     {
164
         this.type = Type.IMPORT;
207
         this.type = Type.IMPORT;
165
         this.wb = WorkbookFactory.create(is);
208
         this.wb = WorkbookFactory.create(is);
166
         List<T> list = new ArrayList<T>();
209
         List<T> list = new ArrayList<T>();
167
-        Sheet sheet = null;
168
-        if (StringUtils.isNotEmpty(sheetName))
169
-        {
170
-            // 如果指定sheet名,则取指定sheet中的内容.
171
-            sheet = wb.getSheet(sheetName);
172
-        }
173
-        else
174
-        {
175
-            // 如果传入的sheet名不存在则默认指向第1个sheet.
176
-            sheet = wb.getSheetAt(0);
177
-        }
178
-
210
+        // 如果指定sheet名,则取指定sheet中的内容 否则默认指向第1个sheet
211
+        Sheet sheet = StringUtils.isNotEmpty(sheetName) ? wb.getSheet(sheetName) : wb.getSheetAt(0);
179
         if (sheet == null)
212
         if (sheet == null)
180
         {
213
         {
181
             throw new IOException("文件sheet不存在");
214
             throw new IOException("文件sheet不存在");
@@ -189,7 +222,7 @@ public class ExcelUtil<T>
189
             // 定义一个map用于存放excel列的序号和field.
222
             // 定义一个map用于存放excel列的序号和field.
190
             Map<String, Integer> cellMap = new HashMap<String, Integer>();
223
             Map<String, Integer> cellMap = new HashMap<String, Integer>();
191
             // 获取表头
224
             // 获取表头
192
-            Row heard = sheet.getRow(0);
225
+            Row heard = sheet.getRow(titleNum);
193
             for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++)
226
             for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++)
194
             {
227
             {
195
                 Cell cell = heard.getCell(i);
228
                 Cell cell = heard.getCell(i);
@@ -222,7 +255,7 @@ public class ExcelUtil<T>
222
                     }
255
                     }
223
                 }
256
                 }
224
             }
257
             }
225
-            for (int i = 1; i <= rows; i++)
258
+            for (int i = titleNum + 1; i <= rows; i++)
226
             {
259
             {
227
                 // 从第2行开始取数据,默认第一行是表头.
260
                 // 从第2行开始取数据,默认第一行是表头.
228
                 Row row = sheet.getRow(i);
261
                 Row row = sheet.getRow(i);
@@ -331,11 +364,26 @@ public class ExcelUtil<T>
331
      * @return 结果
364
      * @return 结果
332
      * @throws IOException
365
      * @throws IOException
333
      */
366
      */
334
-    public void exportExcel(HttpServletResponse response, List<T> list, String sheetName) throws IOException
367
+    public void exportExcel(HttpServletResponse response, List<T> list, String sheetName)throws IOException
368
+    {
369
+        exportExcel(response, list, sheetName, StringUtils.EMPTY);
370
+    }
371
+
372
+    /**
373
+     * 对list数据源将其里面的数据导入到excel表单
374
+     * 
375
+     * @param response 返回数据
376
+     * @param list 导出数据集合
377
+     * @param sheetName 工作表的名称
378
+     * @param title 标题
379
+     * @return 结果
380
+     * @throws IOException
381
+     */
382
+    public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title) throws IOException
335
     {
383
     {
336
         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
384
         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
337
         response.setCharacterEncoding("utf-8");
385
         response.setCharacterEncoding("utf-8");
338
-        this.init(list, sheetName, Type.EXPORT);
386
+        this.init(list, sheetName, title, Type.EXPORT);
339
         exportExcel(response.getOutputStream());
387
         exportExcel(response.getOutputStream());
340
     }
388
     }
341
 
389
 
@@ -345,11 +393,29 @@ public class ExcelUtil<T>
345
      * @param sheetName 工作表的名称
393
      * @param sheetName 工作表的名称
346
      * @return 结果
394
      * @return 结果
347
      */
395
      */
396
+    /**
397
+     * 对list数据源将其里面的数据导入到excel表单
398
+     * 
399
+     * @param sheetName 工作表的名称
400
+     * @return 结果
401
+     */
348
     public void importTemplateExcel(HttpServletResponse response, String sheetName) throws IOException
402
     public void importTemplateExcel(HttpServletResponse response, String sheetName) throws IOException
349
     {
403
     {
404
+        importTemplateExcel(response, sheetName);
405
+    }
406
+
407
+    /**
408
+     * 对list数据源将其里面的数据导入到excel表单
409
+     * 
410
+     * @param sheetName 工作表的名称
411
+     * @param title 标题
412
+     * @return 结果
413
+     */
414
+    public void importTemplateExcel(HttpServletResponse response, String sheetName, String title) throws IOException
415
+    {
350
         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
416
         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
351
         response.setCharacterEncoding("utf-8");
417
         response.setCharacterEncoding("utf-8");
352
-        this.init(null, sheetName, Type.IMPORT);
418
+        this.init(null, sheetName, title, Type.IMPORT);
353
         exportExcel(response.getOutputStream());
419
         exportExcel(response.getOutputStream());
354
     }
420
     }
355
 
421
 
@@ -382,13 +448,13 @@ public class ExcelUtil<T>
382
     public void writeSheet()
448
     public void writeSheet()
383
     {
449
     {
384
         // 取出一共有多少个sheet.
450
         // 取出一共有多少个sheet.
385
-        double sheetNo = Math.ceil(list.size() / sheetSize);
386
-        for (int index = 0; index <= sheetNo; index++)
451
+        int sheetNo = Math.max(1, (int) Math.ceil(list.size() * 1.0 / sheetSize));
452
+        for (int index = 0; index < sheetNo; index++)
387
         {
453
         {
388
             createSheet(sheetNo, index);
454
             createSheet(sheetNo, index);
389
 
455
 
390
             // 产生一行
456
             // 产生一行
391
-            Row row = sheet.createRow(0);
457
+            Row row = sheet.createRow(rownum);
392
             int column = 0;
458
             int column = 0;
393
             // 写入各个字段的列头名称
459
             // 写入各个字段的列头名称
394
             for (Object[] os : fields)
460
             for (Object[] os : fields)
@@ -416,7 +482,7 @@ public class ExcelUtil<T>
416
         int endNo = Math.min(startNo + sheetSize, list.size());
482
         int endNo = Math.min(startNo + sheetSize, list.size());
417
         for (int i = startNo; i < endNo; i++)
483
         for (int i = startNo; i < endNo; i++)
418
         {
484
         {
419
-            row = sheet.createRow(i + 1 - startNo);
485
+            row = sheet.createRow(i + 1 + rownum - startNo);
420
             // 得到导出对象.
486
             // 得到导出对象.
421
             T vo = (T) list.get(i);
487
             T vo = (T) list.get(i);
422
             int column = 0;
488
             int column = 0;
@@ -444,6 +510,16 @@ public class ExcelUtil<T>
444
         CellStyle style = wb.createCellStyle();
510
         CellStyle style = wb.createCellStyle();
445
         style.setAlignment(HorizontalAlignment.CENTER);
511
         style.setAlignment(HorizontalAlignment.CENTER);
446
         style.setVerticalAlignment(VerticalAlignment.CENTER);
512
         style.setVerticalAlignment(VerticalAlignment.CENTER);
513
+        Font titleFont = wb.createFont();
514
+        titleFont.setFontName("Arial");
515
+        titleFont.setFontHeightInPoints((short) 16);
516
+        titleFont.setBold(true);
517
+        style.setFont(titleFont);
518
+        styles.put("title", style);
519
+
520
+        style = wb.createCellStyle();
521
+        style.setAlignment(HorizontalAlignment.CENTER);
522
+        style.setVerticalAlignment(VerticalAlignment.CENTER);
447
         style.setBorderRight(BorderStyle.THIN);
523
         style.setBorderRight(BorderStyle.THIN);
448
         style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
524
         style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
449
         style.setBorderLeft(BorderStyle.THIN);
525
         style.setBorderLeft(BorderStyle.THIN);
@@ -471,7 +547,7 @@ public class ExcelUtil<T>
471
         headerFont.setColor(IndexedColors.WHITE.getIndex());
547
         headerFont.setColor(IndexedColors.WHITE.getIndex());
472
         style.setFont(headerFont);
548
         style.setFont(headerFont);
473
         styles.put("header", style);
549
         styles.put("header", style);
474
-        
550
+
475
         style = wb.createCellStyle();
551
         style = wb.createCellStyle();
476
         style.setAlignment(HorizontalAlignment.CENTER);
552
         style.setAlignment(HorizontalAlignment.CENTER);
477
         style.setVerticalAlignment(VerticalAlignment.CENTER);
553
         style.setVerticalAlignment(VerticalAlignment.CENTER);
@@ -840,10 +916,9 @@ public class ExcelUtil<T>
840
     {
916
     {
841
         if (statistics.size() > 0)
917
         if (statistics.size() > 0)
842
         {
918
         {
843
-            Cell cell = null;
844
             Row row = sheet.createRow(sheet.getLastRowNum() + 1);
919
             Row row = sheet.createRow(sheet.getLastRowNum() + 1);
845
             Set<Integer> keys = statistics.keySet();
920
             Set<Integer> keys = statistics.keySet();
846
-            cell = row.createCell(0);
921
+            Cell cell = row.createCell(0);
847
             cell.setCellStyle(styles.get("total"));
922
             cell.setCellStyle(styles.get("total"));
848
             cell.setCellValue("合计");
923
             cell.setCellValue("合计");
849
 
924
 
@@ -939,7 +1014,7 @@ public class ExcelUtil<T>
939
         this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
1014
         this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
940
         this.maxHeight = getRowHeight();
1015
         this.maxHeight = getRowHeight();
941
     }
1016
     }
942
-    
1017
+
943
     /**
1018
     /**
944
      * 根据注解获取最大行高
1019
      * 根据注解获取最大行高
945
      */
1020
      */
@@ -971,6 +1046,9 @@ public class ExcelUtil<T>
971
     public void createWorkbook()
1046
     public void createWorkbook()
972
     {
1047
     {
973
         this.wb = new SXSSFWorkbook(500);
1048
         this.wb = new SXSSFWorkbook(500);
1049
+        this.sheet = wb.createSheet();
1050
+        wb.setSheetName(0, sheetName);
1051
+        this.styles = createStyles(wb);
974
     }
1052
     }
975
 
1053
 
976
     /**
1054
     /**
@@ -979,17 +1057,13 @@ public class ExcelUtil<T>
979
      * @param sheetNo sheet数量
1057
      * @param sheetNo sheet数量
980
      * @param index 序号
1058
      * @param index 序号
981
      */
1059
      */
982
-    public void createSheet(double sheetNo, int index)
1060
+    public void createSheet(int sheetNo, int index)
983
     {
1061
     {
984
-        this.sheet = wb.createSheet();
985
-        this.styles = createStyles(wb);
986
         // 设置工作表的名称.
1062
         // 设置工作表的名称.
987
-        if (sheetNo == 0)
988
-        {
989
-            wb.setSheetName(index, sheetName);
990
-        }
991
-        else
1063
+        if (sheetNo > 1 && index > 0)
992
         {
1064
         {
1065
+            this.sheet = wb.createSheet();
1066
+            this.createTitle();
993
             wb.setSheetName(index, sheetName + index);
1067
             wb.setSheetName(index, sheetName + index);
994
         }
1068
         }
995
     }
1069
     }