easypoi
是一款簡化版的poi
工具,讓你不須要太多的poi
知識就可以使用poi完成Excel和word的各類操做。html
easypoi
官方文檔,點擊官方文檔java
easypoi
?springboot
項目,在pom.xml
中引入依賴<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.1.0</version>
</dependency>
複製代碼
easypoiutil
public class EasyPoiUtils {
/** * 導出excel * @param pojoClass * @param dataSet * @param path * @param filename * @throws IOException */
public static void exportExcel(Class<?> pojoClass, Collection<?> dataSet,String path,String filename) throws IOException {
File savefile = new File(path);
if (!savefile.exists()) {
savefile.mkdirs();
}
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), pojoClass, dataSet);
FileOutputStream fos = new FileOutputStream(path+filename);
workbook.write(fos);
fos.close();
}
/** * 根據Map建立對應的Excel(一個excel 建立多個sheet) * @param list list 多個Map key title 對應表格Title key entity 對應表格對應實體 key data * * Collection 數據 * @param path 路徑 * @param filename 文件名 * @throws IOException */
public static void exportExcel(List<Map<String, Object>> list,String path,String filename) throws IOException{
File savefile = new File(path);
if (!savefile.exists()) {
savefile.mkdirs();
}
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
FileOutputStream fos = new FileOutputStream(path+filename);
workbook.write(fos);
fos.close();
}
/** * 導入excel * @param file * @param pojoClass * @param params * @param <T> * @return */
public static <T>List<T> importExcel(File file, Class<?> pojoClass, ImportParams params){
long start = new Date().getTime();
List<T> list = ExcelImportUtil.importExcel(file,UserEntity.class, params);
return list;
}
}
複製代碼
建立實體類,在須要導出的字段上添加@Excel
註解。web
/** * 用戶實體類 */
public class UserEntity {
@Excel(name = "ID")
private int id;
@Excel(name = "姓名")
private String name;
@Excel(name = "電子郵件",width = 20)
private String email;
@Excel(name = "年齡")
private int age;
@Excel(name = "性別",replace={"男_1", "女_2"})
private int sex;
@Excel(name = "操做時間",format="yyyy-MM-dd HH:mm:ss",width = 20)
private Date time;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
@Override
public String toString() {
return "UserEntity{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
", sex=" + sex +
", time=" + time +
'}';
}
}
複製代碼
註解說明:spring
sheet
/** * 測試單sheet導出 * @throws IOException */
public static void testExportExcel() throws IOException {
List<UserEntity> list = new ArrayList<>();
int i = 0;
while (i < 10){
UserEntity user = new UserEntity();
user.setId(i+1);
user.setAge(20+i);
user.setEmail("abc@163.com");
user.setName("張三"+i);
user.setSex(i%2==0?1:2);
user.setTime(new Date());
list.add(user);
i++;
}
EasyPoiUtils.exportExcel(UserEntity.class,list,"src/main/resources/excel/","user.xls");
}
複製代碼
導出效果springboot
sheet
/** * 測試多sheet導出 * @throws IOException */
public static void testExportExcels() throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
for(int n=1;n<4;n++){
ExportParams exportParams = new ExportParams("用戶信息"+n,"用戶信息"+n);
Object entity = UserEntity.class;
List<UserEntity> data = new ArrayList<>();
int i = 0;
while (i < 10){
UserEntity user = new UserEntity();
user.setId(i*n+1);
user.setAge(20+i);
user.setEmail("abc@163.com");
user.setName("張三"+i*n);
user.setSex(i%2==0?1:2);
user.setTime(new Date());
data.add(user);
i++;
}
// 構建map
Map<String,Object> map = new HashMap<>();
map.put("title",exportParams);
map.put("entity",entity);
map.put("data",data);
list.add(map);
}
EasyPoiUtils.exportExcel(list,"src/main/resources/excel/","user1.xls");
}
複製代碼
導出效果:ide
excel
/** * 測試導入 */
public static void testImportExcel(){
List<UserEntity> list = EasyPoiUtils.importExcel(
new File("src/main/resources/excel/user.xls"),
UserEntity.class, new ImportParams());
list.forEach((user)->{
System.out.println(user);
});
}
複製代碼
導入時候可以直接幫咱們轉化爲實體類,這功能仍是很是讚的。咱們將結果輸出到控制檯,輸出信息以下:工具
以上就是easypoi
對excel
表格的簡單操做,固然easypoi
的功能遠不止這麼多,easypoi
的功能仍是很是強大的,有興趣的朋友能夠去官方學習。學習