原文:https://www.jianshu.com/p/5d67fb720ece
在開發中會遇到導入Excel文件或者DBF文件等,這里記錄如何更簡單的在springboot中導入導出Excel文件,不同於用poi,還需要寫大段工具類代碼
此處運用別人造好的輪子【easypoi】
pom引入
需要的jar包

編寫實體類
- 此處注意必須要有空構造函數,否則會報錯“對象創建錯誤”(我並未考證)
- 關於注解@Excel,其他還有@ExcelCollection,@ExcelEntity ,@ExcelIgnore,@ExcelTarget等,此處我們用不到,可以去官方查看更多
public class ExcelBean { @Excel(name="姓名", orderNum="0") private String name; @Excel(name="性別", replace={"男_2","女_1"}, orderNum="1") private String sex; @Excel(name="生日", orderNum="2") private Date birthday; public ExcelBean() { super(); // TODO Auto-generated constructor stub } public ExcelBean(String name, String sex, Date birthday) { super(); this.name = name; this.sex = sex; this.birthday = birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
工具類(雖然比往常用poi簡介一些,但還是需要寫工具類)
public class ExcelUtils { /*public static void exportExcel(){ }*/ 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); } public 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); } } public 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) { // TODO Auto-generated catch block e.printStackTrace(); } } public 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); } } /** * 泛型方法 * @param filePath * @param titleRows * @param headerRows * @param pojoClass * @return */ public static<T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){ if (StringUtils.nonEmptyString(filePath)) { return null; } ImportParams params=new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); List<T> list=null; list=ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); return list; } /** * 泛型方法 * @param file * @param titleRows * @param headerRows * @param pojoClass * @return */ 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 (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }
進行測試(導出文件已測試成功,但導入並沒有測試)
@Value("${com.excel.name}")
private String name;
/**
* 導出Excel文件
* @param response
*/
@RequestMapping("/export")
public void export(HttpServletResponse response){
List<ExcelBean> list=new ArrayList<ExcelBean>();
ExcelBean excel=new ExcelBean(name, "1", new Date());
ExcelBean excel1=new ExcelBean(name, "2", DateUtils.addDays(new Date(), 3));
list.add(excel);
list.add(excel1);
ExcelUtils.exportExcel(list, "文件標題", "文件頭", ExcelBean.class, "Excel文件.xls", response);
}
/**
* 導入Excel文件
*/
@RequestMapping("/import")
public void importExcel(){
String filename="C:\\Users\\pc\\Desktop\\流程圖.xlsx";
List<ExcelBean> list=ExcelUtils.importExcel(filename, 1, 1, ExcelBean.class);
}
上述代碼中
@Value("${com.excel.name}")
private String name;
的寫法是把name定義再application中,個人喜好
Excel文件導出測試結果

因為沒有把Date()轉化格式,所以顯示是英文
