Преглед изворни кода

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

RuoYi пре 4 година
родитељ
комит
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 36
 import org.apache.poi.ss.usermodel.VerticalAlignment;
37 37
 import org.apache.poi.ss.usermodel.Workbook;
38 38
 import org.apache.poi.ss.usermodel.WorkbookFactory;
39
+import org.apache.poi.ss.util.CellRangeAddress;
39 40
 import org.apache.poi.ss.util.CellRangeAddressList;
40 41
 import org.apache.poi.util.IOUtils;
41 42
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
@@ -104,6 +105,16 @@ public class ExcelUtil<T>
104 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 120
     private short maxHeight;
@@ -128,7 +139,7 @@ public class ExcelUtil<T>
128 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 144
         if (list == null)
134 145
         {
@@ -137,8 +148,27 @@ public class ExcelUtil<T>
137 148
         this.list = list;
138 149
         this.sheetName = sheetName;
139 150
         this.type = type;
151
+        this.title = title;
140 152
         createExcelField();
141 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 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 198
      * 对excel表单指定表格索引名转换成list
157 199
      * 
158 200
      * @param sheetName 表格索引名
201
+     * @param titleNum 标题占用行数
159 202
      * @param is 输入流
160 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 207
         this.type = Type.IMPORT;
165 208
         this.wb = WorkbookFactory.create(is);
166 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 212
         if (sheet == null)
180 213
         {
181 214
             throw new IOException("文件sheet不存在");
@@ -189,7 +222,7 @@ public class ExcelUtil<T>
189 222
             // 定义一个map用于存放excel列的序号和field.
190 223
             Map<String, Integer> cellMap = new HashMap<String, Integer>();
191 224
             // 获取表头
192
-            Row heard = sheet.getRow(0);
225
+            Row heard = sheet.getRow(titleNum);
193 226
             for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++)
194 227
             {
195 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 260
                 // 从第2行开始取数据,默认第一行是表头.
228 261
                 Row row = sheet.getRow(i);
@@ -331,11 +364,26 @@ public class ExcelUtil<T>
331 364
      * @return 结果
332 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 384
         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
337 385
         response.setCharacterEncoding("utf-8");
338
-        this.init(list, sheetName, Type.EXPORT);
386
+        this.init(list, sheetName, title, Type.EXPORT);
339 387
         exportExcel(response.getOutputStream());
340 388
     }
341 389
 
@@ -345,11 +393,29 @@ public class ExcelUtil<T>
345 393
      * @param sheetName 工作表的名称
346 394
      * @return 结果
347 395
      */
396
+    /**
397
+     * 对list数据源将其里面的数据导入到excel表单
398
+     * 
399
+     * @param sheetName 工作表的名称
400
+     * @return 结果
401
+     */
348 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 416
         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
351 417
         response.setCharacterEncoding("utf-8");
352
-        this.init(null, sheetName, Type.IMPORT);
418
+        this.init(null, sheetName, title, Type.IMPORT);
353 419
         exportExcel(response.getOutputStream());
354 420
     }
355 421
 
@@ -382,13 +448,13 @@ public class ExcelUtil<T>
382 448
     public void writeSheet()
383 449
     {
384 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 454
             createSheet(sheetNo, index);
389 455
 
390 456
             // 产生一行
391
-            Row row = sheet.createRow(0);
457
+            Row row = sheet.createRow(rownum);
392 458
             int column = 0;
393 459
             // 写入各个字段的列头名称
394 460
             for (Object[] os : fields)
@@ -416,7 +482,7 @@ public class ExcelUtil<T>
416 482
         int endNo = Math.min(startNo + sheetSize, list.size());
417 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 487
             T vo = (T) list.get(i);
422 488
             int column = 0;
@@ -444,6 +510,16 @@ public class ExcelUtil<T>
444 510
         CellStyle style = wb.createCellStyle();
445 511
         style.setAlignment(HorizontalAlignment.CENTER);
446 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 523
         style.setBorderRight(BorderStyle.THIN);
448 524
         style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
449 525
         style.setBorderLeft(BorderStyle.THIN);
@@ -471,7 +547,7 @@ public class ExcelUtil<T>
471 547
         headerFont.setColor(IndexedColors.WHITE.getIndex());
472 548
         style.setFont(headerFont);
473 549
         styles.put("header", style);
474
-        
550
+
475 551
         style = wb.createCellStyle();
476 552
         style.setAlignment(HorizontalAlignment.CENTER);
477 553
         style.setVerticalAlignment(VerticalAlignment.CENTER);
@@ -840,10 +916,9 @@ public class ExcelUtil<T>
840 916
     {
841 917
         if (statistics.size() > 0)
842 918
         {
843
-            Cell cell = null;
844 919
             Row row = sheet.createRow(sheet.getLastRowNum() + 1);
845 920
             Set<Integer> keys = statistics.keySet();
846
-            cell = row.createCell(0);
921
+            Cell cell = row.createCell(0);
847 922
             cell.setCellStyle(styles.get("total"));
848 923
             cell.setCellValue("合计");
849 924
 
@@ -939,7 +1014,7 @@ public class ExcelUtil<T>
939 1014
         this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
940 1015
         this.maxHeight = getRowHeight();
941 1016
     }
942
-    
1017
+
943 1018
     /**
944 1019
      * 根据注解获取最大行高
945 1020
      */
@@ -971,6 +1046,9 @@ public class ExcelUtil<T>
971 1046
     public void createWorkbook()
972 1047
     {
973 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 1057
      * @param sheetNo sheet数量
980 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 1067
             wb.setSheetName(index, sheetName + index);
994 1068
         }
995 1069
     }