commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.core.util; |
|
26 |
|
|
27 |
import cn.afterturn.easypoi.excel.ExcelExportUtil; |
|
28 |
import cn.afterturn.easypoi.excel.ExcelImportUtil; |
|
29 |
import cn.afterturn.easypoi.excel.entity.ExportParams; |
|
30 |
import cn.afterturn.easypoi.excel.entity.ImportParams; |
|
31 |
import cn.hutool.core.io.FileUtil; |
|
32 |
import cn.hutool.core.util.CharsetUtil; |
|
33 |
import cn.hutool.core.util.ObjectUtil; |
|
34 |
import cn.hutool.log.Log; |
|
35 |
import org.apache.poi.ss.usermodel.Workbook; |
|
36 |
import org.springframework.web.multipart.MultipartFile; |
|
37 |
|
|
38 |
import javax.servlet.ServletOutputStream; |
|
39 |
import javax.servlet.http.HttpServletResponse; |
|
40 |
import java.io.File; |
|
41 |
import java.io.FileOutputStream; |
|
42 |
import java.io.IOException; |
|
43 |
import java.net.URLEncoder; |
|
44 |
import java.util.Collection; |
|
45 |
import java.util.List; |
|
46 |
|
|
47 |
/** |
|
48 |
* 简单导入导出工具类 |
|
49 |
* |
|
50 |
* @author xuyuxiang |
|
51 |
* @date 2020/6/30 17:25 |
|
52 |
*/ |
|
53 |
public class PoiUtil { |
|
54 |
|
|
55 |
private static final Log log = Log.get(); |
|
56 |
|
|
57 |
/** |
|
58 |
* 使用流的方式导出excel |
|
59 |
* |
|
60 |
* @param excelName 要导出的文件名称,如Users.xls |
|
61 |
* @param pojoClass Excel实体类 |
|
62 |
* @param data 要导出的数据集合 |
|
63 |
* @author xuyuxiang |
|
64 |
* @date 2020/7/1 10:00 |
|
65 |
*/ |
|
66 |
public static void exportExcelWithStream(String excelName, Class<?> pojoClass, Collection<?> data) { |
|
67 |
try { |
|
68 |
HttpServletResponse response = HttpServletUtil.getResponse(); |
|
69 |
String fileName = URLEncoder.encode(excelName, CharsetUtil.UTF_8); |
|
70 |
response.reset(); |
|
71 |
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); |
|
72 |
response.setContentType("application/octet-stream;charset=UTF-8"); |
|
73 |
ServletOutputStream outputStream = response.getOutputStream(); |
|
74 |
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), pojoClass, data); |
|
75 |
workbook.write(outputStream); |
|
76 |
outputStream.close(); |
|
77 |
} catch (IOException e) { |
|
78 |
log.error(">>> 导出数据异常:{}", e.getMessage()); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
/** |
|
83 |
* 使用文件的方式导出excel |
|
84 |
* |
|
85 |
* @param filePath 文件路径,如 d:/demo/demo.xls |
|
86 |
* @param pojoClass Excel实体类 |
|
87 |
* @param data 要导出的数据集合 |
|
88 |
* @author xuyuxiang |
|
89 |
* @date 2020/7/1 9:58 |
|
90 |
*/ |
|
91 |
public static void exportExcelWithFile(String filePath, Class pojoClass, Collection data) { |
|
92 |
|
|
93 |
try { |
|
94 |
//先创建父文件夹 |
|
95 |
FileUtil.mkParentDirs(filePath); |
|
96 |
File file = FileUtil.file(filePath); |
|
97 |
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), pojoClass, data); |
|
98 |
FileOutputStream fos = new FileOutputStream(file); |
|
99 |
workbook.write(fos); |
|
100 |
fos.close(); |
|
101 |
} catch (IOException e) { |
|
102 |
log.error(">>> 导出数据异常:{}", e.getMessage()); |
|
103 |
} |
|
104 |
|
|
105 |
} |
|
106 |
|
|
107 |
/** |
|
108 |
* 根据文件路径来导入Excel |
|
109 |
* |
|
110 |
* @param filePath 文件路径 |
|
111 |
* @param titleRows 表标题的行数 |
|
112 |
* @param headerRows 表头行数 |
|
113 |
* @param pojoClass Excel实体类 |
|
114 |
* @author xuyuxiang |
|
115 |
* @date 2020/7/1 9:58 |
|
116 |
*/ |
|
117 |
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) { |
|
118 |
//判断文件是否存在 |
|
119 |
if (ObjectUtil.isEmpty(filePath)) { |
|
120 |
return null; |
|
121 |
} |
|
122 |
ImportParams params = new ImportParams(); |
|
123 |
params.setTitleRows(titleRows); |
|
124 |
params.setHeadRows(headerRows); |
|
125 |
List<T> list = null; |
|
126 |
try { |
|
127 |
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); |
|
128 |
} catch (Exception e) { |
|
129 |
log.error(">>> 导入数据异常:{}", e.getMessage()); |
|
130 |
} |
|
131 |
return list; |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* 根据接收的Excel文件来导入Excel,并封装成实体类 |
|
136 |
* |
|
137 |
* @param file 上传的文件 |
|
138 |
* @param titleRows 表标题的行数 |
|
139 |
* @param headerRows 表头行数 |
|
140 |
* @param pojoClass Excel实体类 |
|
141 |
* @author xuyuxiang |
|
142 |
* @date 2020/7/1 9:57 |
|
143 |
*/ |
|
144 |
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) { |
|
145 |
if (ObjectUtil.isNull(file)) { |
|
146 |
return null; |
|
147 |
} |
|
148 |
ImportParams params = new ImportParams(); |
|
149 |
params.setTitleRows(titleRows); |
|
150 |
params.setHeadRows(headerRows); |
|
151 |
List<T> list = null; |
|
152 |
try { |
|
153 |
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params); |
|
154 |
} catch (Exception e) { |
|
155 |
log.error(">>> 导入数据异常:{}", e.getMessage()); |
|
156 |
} |
|
157 |
return list; |
|
158 |
} |
|
159 |
|
|
160 |
} |