介紹
easypoi功能如同名字easy,主打的功能就是容易,讓一個沒見接觸過poi的人員
就可以友善的寫出Excel導出,Excel模闆導出,Excel導入,Word模闆導出,通過簡單的注解和模闆
官網位址:http://easypoi.mydoc.io/
easypoi需要導入的包
<!--easypoi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
easypoi工具類
package com.meeno.framework.office.excel.easypoi.utils;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
/**
* @description: easypoiUtils
* @author: Wzq
* @create: 2019-11-08 14:54
*/
public class ExcelUtiles {
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,
String fileName, boolean isCreateHeader, HttpServletResponse response){
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,
HttpServletResponse response){
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){
defaultExport(list, fileName, response);
}
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName,
HttpServletResponse response, ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
if (workbook != null); downLoadExcel(fileName, response, workbook);
}
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
//throw new NormalException(e.getMessage());
}
}
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
if (workbook != null);
downLoadExcel(fileName, response, workbook);
}
public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){
if (StringUtils.isBlank(filePath)){
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
}catch (NoSuchElementException e){
//throw new NormalException("模闆不能為空");
} catch (Exception e) {
e.printStackTrace();
//throw new NormalException(e.getMessage());
} return list;
}
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
if (file == null){ return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
}catch (NoSuchElementException e){
// throw new NormalException("excel檔案不能為空");
} catch (Exception e) {
//throw new NormalException(e.getMessage());
System.out.println(e.getMessage());
}
return list;
}
}
建立導出excel模闆的實體類,也可以按照模闆導出具體看官網文檔
導出excel模闆的實體類代碼如下:
package com.meeno.framework.office.excel.easypoi.model;
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;
import java.io.Serializable;
/**
* @description: 簽到記錄ExecelModel
* @author: Wzq
* @create: 2020-01-07 11:34
*/
@Data
public class SignRecordExcelModel implements Serializable {
/**
* 序号
*/
@Excel(name = "序号",orderNum = "0")
private Integer number;
/**
* 人員名稱
*/
@Excel(name = "人員名稱",orderNum = "1")
private String employeeName;
/**
* 座位号
*/
@Excel(name = "座位号",orderNum = "2")
private Integer seatNum;
/**
* 簽到狀态
*/
@Excel(name = "簽到狀态",orderNum = "3")
private String signStatusStr;
}
使用easypoi導出excel
controller層調用代碼
/**
*@Description 導出登入記錄表
*@Param [session, request, response, data]
*@Return void
*@Author Wzq
*@Date 2020/1/7
*@Time 11:06
*/
@RequestMapping(value = "exportSignRecord.action")
public void exportSignRecord(final HttpSession session,
final HttpServletRequest request,
final HttpServletResponse response,
@RequestParam(value = "Data",required = false) String data){
JSONObject jsonObject = JSONObject.parseObject(data);
//會議id
Long meetingId = jsonObject.getLong("meetingId");
List<SignRecordExcelModel> signRecordExcelModels = this.signRecordService.exportSignRecord(meetingId);
Meeting meeting = this.meetingService.getMeetingDetail(meetingId);
//第一參數:導出的模闆類集合
//第二參數:excel中裡面内容合并單元格的title
//第三參數:seeetName名稱
//第四參數:導出模闆實體類的class
//第五參數:導出檔案檔案名
//第六參數:HttpServletResponse
ExcelUtiles.exportExcel(signRecordExcelModels,"","簽到表",SignRecordExcelModel.class,meeting.getName()+"簽到表.xlsx",response);
}