- 基于maven从零集成Excel功能
- maven集成
- Excel导入
- Excel导出
- vue-Excel导入对话框模板
- axios-文件下载(使用blob下转换)
开发Excel流程4.4.0 cn.afterturn easypoi-spring-boot-starter${easy.poi.version} cn.afterturn easypoi-base${easy.poi.version} cn.afterturn easypoi-web${easy.poi.version} cn.afterturn easypoi-annotation${easy.poi.version}
- 下载Excel导入模板
- Excel导入
- Excel导出
- 本文只用Excel模板的方式导出Excel,因为用注解或者代码配置Excel样式实在是太丑了
为了部署方便,一般会将这些模板文件直接放在jar包中,但是jar只能读取InputStream,所以就需要一个工具类来转换一下
java-jar包resource转文件
新建Excel处理Biz定义Biz接口我一般会吧excel的 *** 作类新建一个batch文件夹,然后在按照biz的方式新建,这样代码可以比较简洁
public interface DataDictValueBatchBiz { void downloadimportTemplate(HttpServletResponse response, Long dataDictId); ExcelimportResultVO batchimport(MultipartFile file, Long dataDictId); }实现下载Excel-Biz方法
@Override public void downloadimportTemplate(HttpServletResponse response, Long dataDictId) { DataDict dataDict = dataDictBiz.getById(dataDictId); if (null == dataDict) { throw new DirtyException("xx信息不存在"); } setFileDownloadHeader(response, String.format("%s-导入模板.xlsx", dataDict.getInfo())); TemplateExportParams params = new TemplateExportParams(JarResourceTool.fileByResource("excel-template/xxx.xlsx").getAbsolutePath()); DataDictExcelTemplateExportDTO data = new DataDictExcelTemplateExportDTO(); data.setDate(DateUtil.formatDateTime(new Date())); data.setVersion("1.0"); data.setName(dataDict.getName()); // 开启列遍历 params.setColForEach(true); ListController层fields = new ArrayList<>(); // 动态列 data.setColList(fields); if (null != dataDict.getFieldConfig()) { fields.addAll(dataDict.getFieldConfig().getList().stream().filter(DataDictFiledConfig.FiledConfig::isHasExcel).collect(Collectors.toList())); } Workbook workbook = ExcelExportUtil.exportExcel(params, BeanUtil.beanToMap(data)); try { workbook.write(response.getOutputStream()); } catch (IOException e) { log.error("teacher import template error", e); } } public static void setFileDownloadHeader(HttpServletResponse response, String filename) { String headerValue = "attachment;"; headerValue += " filename="" + encodeURIComponent(filename) + "";"; headerValue += " filename*=utf-8''" + encodeURIComponent(filename); response.setHeader("Content-Disposition", headerValue); //expose header response.setHeader("Access-Control-Expose-Headers", "Content-Disposition,Content-Type"); response.setHeader("Content-Type", "application/octet-stream"); } public static String encodeURIComponent(String value) { try { return URLEncoder.encode(value, "UTF-8").replaceAll("\+", "%20"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
@PostMapping(value = "/downloadimportTemplate") @ApiOperation("下载导入模板") public void downloadimportTemplate(HttpServletResponse response, @RequestParam("dataDictId") Long dataDictId) { dataDictValueBatchBiz.downloadimportTemplate(response, dataDictId); }Excel导入 定义Excel导入结果类
@Getter @Setter public class ExcelimportResultVO implements Serializable { private Integer successCount; private Integer errorCount; private Integer total; private String remark; }实现batchimport方法
@Override public ExcelimportResultVO batchimport(MultipartFile file, Long dataDictId) { DataDict dataDict = dataDictBiz.getById(dataDictId); if (null == dataDict) { throw new DirtyException("信息不存在"); } if (null == dataDict.getFieldConfig() || null == dataDict.getFieldConfig().getList()) { throw new DirtyException("字段信息不存在"); } ListController层importsField = dataDict.getFieldConfig().getList() .stream() .filter(DataDictFiledConfig.FiledConfig::isHasExcel) .map(DataDictFiledConfig.FiledConfig::getName) .collect(Collectors.toList()); if (CollectionUtils.isEmpty(importsField)) { throw new DirtyException("字典导出字段信息不存在"); } String[] importsFieldArr = importsField.toArray(new String[0]); checkFileSize(file, FILE_MAX); checkFileFormat(file, EXCEL_ALLOW_FORMAT); importParams params = new importParams(); params.setTitleRows(1); params.setHeadRows(1); params.setReadRows(100000); params.setimportFields(importsFieldArr); List
@PostMapping(value = "/excelimport") public RestObjectResponseexcelimport(@RequestParam("file") MultipartFile file, @RequestParam("dataDictId") Long dataDictId) { ExcelimportResultVO result = dataDictValueBatchBiz.batchimport(file, dataDictId); return RestObjectResponse.ok(result); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)