官网地址
Easypoi开发文档
导出实例
@Data public class R implements Serializable, IExcelModel, IExcelDataModel {
private static final long serialVersionUID = -3099653964677586050L;
private int rowNum;
private String errorMsg;
@Excel(name = "导入字段", width = 20) @NotBlank private String field;
@Override public Integer getRowNum() { return rowNum; }
@Override public void setRowNum(Integer rowNum) { this.rowNum = rowNum; }
@Override public String getErrorMsg() { return errorMsg; }
@Override public void setErrorMsg(String errorMsg) { this.errorMsg = errorMsg; } }
|
public void exportExcel(HttpServletResponse response, HttpServletRequest request) throws IOException { Workbook workbook; String filename = "导出.xlsx"; ExportParams exportParams = new ExportParams(); exportParams.setDictHandler(deviceTypeHandler); workbook = ExcelExportUtil.exportExcel(exportParams, R.class, new ArrayList<>()); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=utf-8" + URLEncoder.encode(filename, "UTF-8")); response.setCharacterEncoding("UTF-8"); workbook.write(response.getOutputStream()); }
|
导入实例
public Result importExcel(MultipartFile file) throws Exception { ImportParams importParams = new ImportParams(); importParams.setNeedVerify(true); importParams.setDictHandler(deviceTypeHandler); ExcelImportResult<R> result = ExcelImportUtil.importExcelMore(file.getInputStream(), R.class, importParams); if (result.isVerifyFail()) { return Result.error(result.getFailList().stream().map(item -> "第" + item.getRowNum() + "行报错,错误信息:" + item.getErrorMsg()).collect(Collectors.joining(","))); } else { return Result.success("导入成功"); } }
|
最后
最近要写导出导入,长时间没用导致都忘了写法了,特此记录一下,方便日后使用!