Skip to content

Commit 085870b

Browse files
committed
添加easyPOI方法
1 parent d2a914a commit 085870b

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed

pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@
4949
</dependency>
5050

5151

52+
<!-- easypoi -->
53+
<dependency>
54+
<groupId>cn.afterturn</groupId>
55+
<artifactId>easypoi-base</artifactId>
56+
<version>3.0.3</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>cn.afterturn</groupId>
60+
<artifactId>easypoi-web</artifactId>
61+
<version>3.0.3</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>cn.afterturn</groupId>
65+
<artifactId>easypoi-annotation</artifactId>
66+
<version>3.0.3</version>
67+
</dependency>
68+
69+
70+
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
71+
<dependency>
72+
<groupId>javax.servlet</groupId>
73+
<artifactId>javax.servlet-api</artifactId>
74+
<version>3.1.0</version>
75+
</dependency>
76+
77+
5278
</dependencies>
5379

5480

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.chen.easypoi;
2+
3+
import com.chen.util.FileUtil;
4+
import org.apache.commons.lang3.time.DateUtils;
5+
import org.springframework.stereotype.Controller;
6+
7+
import javax.servlet.http.HttpServletResponse;
8+
import java.util.ArrayList;
9+
import java.util.Date;
10+
import java.util.List;
11+
12+
/**
13+
* @author chen weijie
14+
* @date 2018-01-19 12:08 AM
15+
*/
16+
@Controller
17+
public class EasyPoiTestController {
18+
19+
public void export(HttpServletResponse response) {
20+
//模拟从数据库获取需要导出的数据
21+
List<Person> personList = new ArrayList<>();
22+
Person person1 = new Person("路飞", "1", new Date());
23+
Person person2 = new Person("娜美", "2", DateUtils.addDays(new Date(), 3));
24+
Person person3 = new Person("索隆", "1", DateUtils.addDays(new Date(), 10));
25+
Person person4 = new Person("小狸猫", "1", DateUtils.addDays(new Date(), -10));
26+
personList.add(person1);
27+
personList.add(person2);
28+
personList.add(person3);
29+
personList.add(person4);
30+
31+
//导出操作
32+
FileUtil.exportExcel(personList,"花名册","草帽一伙",Person.class,"海贼王.xls",response);
33+
}
34+
35+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.chen.easypoi;
2+
3+
//import org.jeecgframework.poi.excel.annotation.Excel;
4+
5+
import java.util.Date;
6+
7+
/**
8+
* @author chen weijie
9+
* @date 2018-01-19 12:06 AM
10+
*/
11+
public class Person {
12+
// @Excel(name = "姓名", orderNum = "0")
13+
private String name;
14+
15+
// @Excel(name = "性别", replace = {"男_1", "女_2"}, orderNum = "1")
16+
private String sex;
17+
18+
// @Excel(name = "生日", exportFormat = "yyyy-MM-dd", orderNum = "2")
19+
private Date birthday;
20+
21+
public Person(String name, String sex, Date birthday) {
22+
this.name = name;
23+
this.sex = sex;
24+
this.birthday = birthday;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
public String getSex() {
36+
return sex;
37+
}
38+
39+
public void setSex(String sex) {
40+
this.sex = sex;
41+
}
42+
43+
public Date getBirthday() {
44+
return birthday;
45+
}
46+
47+
public void setBirthday(Date birthday) {
48+
this.birthday = birthday;
49+
}
50+
51+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.chen.util;
2+
3+
import cn.afterturn.easypoi.excel.ExcelExportUtil;
4+
import cn.afterturn.easypoi.excel.ExcelImportUtil;
5+
import cn.afterturn.easypoi.excel.entity.ExportParams;
6+
import cn.afterturn.easypoi.excel.entity.ImportParams;
7+
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
8+
import com.sun.deploy.net.URLEncoder;
9+
import org.apache.commons.lang3.StringUtils;
10+
import org.apache.poi.ss.usermodel.Workbook;
11+
import org.springframework.web.multipart.MultipartFile;
12+
13+
import javax.servlet.http.HttpServletResponse;
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.NoSuchElementException;
19+
20+
/**
21+
* @author chen weijie
22+
* @date 2018-01-19 12:22 AM
23+
*/
24+
public class FileUtil {
25+
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response){
26+
ExportParams exportParams = new ExportParams(title, sheetName);
27+
exportParams.setCreateHeadRows(isCreateHeader);
28+
defaultExport(list, pojoClass, fileName, response, exportParams);
29+
30+
}
31+
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){
32+
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
33+
}
34+
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){
35+
defaultExport(list, fileName, response);
36+
}
37+
38+
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
39+
Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
40+
if (workbook != null);
41+
downLoadExcel(fileName, response, workbook);
42+
}
43+
44+
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook){
45+
try {
46+
response.setCharacterEncoding("UTF-8");
47+
response.setHeader("content-Type", "application/vnd.ms-excel");
48+
response.setHeader("Content-Disposition",
49+
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
50+
workbook.write(response.getOutputStream());
51+
} catch (IOException e) {
52+
// throw new IOException(e.getMessage());
53+
}
54+
}
55+
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
56+
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
57+
if (workbook != null);
58+
downLoadExcel(fileName, response, workbook);
59+
}
60+
61+
public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){
62+
if (StringUtils.isBlank(filePath)){
63+
return null;
64+
}
65+
ImportParams params = new ImportParams();
66+
params.setTitleRows(titleRows);
67+
params.setHeadRows(headerRows);
68+
List<T> list = null;
69+
try {
70+
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
71+
}catch (NoSuchElementException e){
72+
// throw new NormalException("模板不能为空");
73+
} catch (Exception e) {
74+
e.printStackTrace();
75+
// throw new NormalException(e.getMessage());
76+
}
77+
return list;
78+
}
79+
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
80+
if (file == null){
81+
return null;
82+
}
83+
ImportParams params = new ImportParams();
84+
params.setTitleRows(titleRows);
85+
params.setHeadRows(headerRows);
86+
List<T> list = null;
87+
try {
88+
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
89+
}catch (NoSuchElementException e){
90+
// throw new NormalException("excel文件不能为空");
91+
} catch (Exception e) {
92+
// throw new NormalException(e.getMessage());
93+
}
94+
return list;
95+
}
96+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.chen.test;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-03-27 12:07 AM
6+
*/
7+
public class TestStringFormat {
8+
9+
public static void main(String[] args) {
10+
11+
String index = String.format("%02d", 1 % 10);
12+
String index1 = String.format("%02d", 2 % 10);
13+
String index2 = String.format("%02d", 10 % 10);
14+
String index3 = String.format("%02d", 10000 % 10);
15+
String index4 = String.format("%02d", 6 % 10);
16+
String index5 = String.format("%02d", 9 % 10);
17+
System.out.println(index);
18+
System.out.println(index1);
19+
System.out.println(index2);
20+
System.out.println(index3);
21+
System.out.println(index4);
22+
System.out.println(index5);
23+
24+
}
25+
26+
}

0 commit comments

Comments
 (0)