添加EasyPoi依賴
<properties>
<easypoi.version>3.1.0</easypoi.version>
</properties>
<dependencies>
<!--EasyPoi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>${easypoi.version}</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>${easypoi.version}</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>${easypoi.version}</version>
</dependency>
</dependencies>
EasyPoi工具類
/**
* Excel導入導出工具類
*/
public class ExcelUtils {
/**
* excel 導出
*
* @param list 數據列表
* @param fileName 導出時的excel名稱
* @param response
*/
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
defaultExport(list, fileName, response);
}
/**
* 默認的 excel 導出
*
* @param list 數據列表
* @param fileName 導出時的excel名稱
* @param response
*/
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
//把數據添加到excel表格中
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
downLoadExcel(fileName, response, workbook);
}
/**
* excel 導出
*
* @param list 數據列表
* @param pojoClass pojo類型
* @param fileName 導出時的excel名稱
* @param response
* @param exportParams 導出參數(標題、sheet名稱、是否創建表頭,表格類型)
*/
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException {
//把數據添加到excel表格中
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
downLoadExcel(fileName, response, workbook);
}
/**
* excel 導出
*
* @param list 數據列表
* @param pojoClass pojo類型
* @param fileName 導出時的excel名稱
* @param exportParams 導出參數(標題、sheet名稱、是否創建表頭,表格類型)
* @param response
*/
public static void exportExcel(List<?> list, Class<?> pojoClass, String fileName, ExportParams exportParams, HttpServletResponse response) throws IOException {
defaultExport(list, pojoClass, fileName, response, exportParams);
}
/**
* excel 導出
*
* @param list 數據列表
* @param title 表格內數據標題
* @param sheetName sheet名稱
* @param pojoClass pojo類型
* @param fileName 導出時的excel名稱
* @param response
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException {
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF));
}
/**
* excel 導出
*
* @param list 數據列表
* @param title 表格內數據標題
* @param sheetName sheet名稱
* @param pojoClass pojo類型
* @param fileName 導出時的excel名稱
* @param isCreateHeader 是否創建表頭
* @param response
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) throws IOException {
ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
/**
* excel下載
*
* @param fileName 下載時的文件名稱
* @param response
* @param workbook excel數據
*/
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
workbook.write(response.getOutputStream());
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
/**
* excel 導入
*
* @param file excel文件
* @param pojoClass pojo類型
* @param <T>
* @return
*/
public static <T> List<T> importExcel(MultipartFile file, Class<T> pojoClass) throws IOException {
return importExcel(file, 1, 1, pojoClass);
}
/**
* excel 導入
*
* @param filePath excel文件路徑
* @param titleRows 表格內數據標題行
* @param headerRows 表頭行
* @param pojoClass pojo類型
* @param <T>
* @return
*/
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
if (StringUtils.isBlank(filePath)) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
params.setNeedSave(true);
params.setSaveUrl("/excel/");
try {
return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
} catch (NoSuchElementException e) {
throw new IOException("模板不能為空");
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
/**
* excel 導入
*
* @param file 上傳的文件
* @param titleRows 表格內數據標題行
* @param headerRows 表頭行
* @param pojoClass pojo類型
* @param <T>
* @return
*/
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
if (file == null) {
return null;
}
try {
return importExcel(file.getInputStream(), titleRows, headerRows, pojoClass);
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
/**
* excel 導入
*
* @param inputStream 文件輸入流
* @param titleRows 表格內數據標題行
* @param headerRows 表頭行
* @param pojoClass pojo類型
* @param <T>
* @return
*/
public static <T> List<T> importExcel(InputStream inputStream, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
if (inputStream == null) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
params.setSaveUrl("/excel/");
params.setNeedSave(true);
try {
return ExcelImportUtil.importExcel(inputStream, pojoClass, params);
} catch (NoSuchElementException e) {
throw new IOException("excel文件不能為空");
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
實體類注解
@Data
public class DongBei implements Serializable {
@Excel(name = "序號",width = 20,orderNum = "0") // 首列
private String id;
@Excel(name = "名稱",width = 20,orderNum = "1")
private String keyName;
@Excel(name = "數據類型",width = 20,orderNum = "2")
private String dataType;
@Excel(name = "設備類型",width = 20,orderNum = "3")
private String deviceType;
}
工具類的使用
/**
* 導出
*/
@RequestMapping(value = "/downLoadExcel", method = RequestMethod.GET)
@ResponseBody
public void downLoadExcel(HttpServletRequest request , HttpServletResponse response) {
try {
String title = "信息表"; //標題名
String sheetName = "信息表"; //表名
ArrayList<Sys_User> list = new ArrayList<>();
ExcelUtils.exportExcel(list, title, sheetName, DongBei.class, "user" + System.currentTimeMillis(), response);
} catch (Exception e) {
log.error(e.getMessage(), e.fillInStackTrace());
}
}
/**
*導入
*/
@RequestMapping("/importXls")
@ResponseBody
public void importSS(@RequestParam("file") MultipartFile file) {
List<DongBei> iotCollectionPoints = null;
int filed = 0;
int success = 0;
try {
iotCollectionPoints = ExcelUtils.importExcel(file, DongBei.class);
IotCollPointIsMain coll = new IotCollPointIsMain();
for (DongBei pointIsMain : iotCollectionPoints) {
try {
coll.setMinValues("0");
coll.setMaxValues("10000");
coll.setCreateTime(date);
coll.setId(UniqueIdUtil.genId());
coll.setCreateUser(userId);
coll.setIsDelete("0");
coll.setIsReadOnly("110");
coll.setStatus("0");
//Tag名稱
coll.setKeyName(pointIsMain.getKeyName());
coll.setName(pointIsMain.getKeyName());
coll.setCreatetime(date);
coll.setCreateBy(userId);
iotCollPointIsMainService.saveColl(coll);
success++;
} catch (Exception e) {
filed++;
logger.error(e.getMessage(), e.fillInStackTrace());
}
}
System.out.println("成功導入數據:" + success + "條 , 失敗" + filed + "條");
} catch (Exception e) {
logger.error(e.getMessage(), e.fillInStackTrace());
}
本篇文章為自用而記錄