利用easypoi把数据导出:
1、引入easypoi包
<!-- https://mvnrepository.com/artifact/org.jeecg/easypoi-base --> <dependency> <groupId>org.jeecg</groupId> <artifactId>easypoi-base</artifactId> <version>2.4.0</version> </dependency>
2、导出数据实体类
@ExcelTarget("Data") public class Data { @Excel(name=" ") private Integer id; @Excel(name="标题") private String title; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
3、
@GetMapping("/data") public void data(HttpServletResponse response, String ids, OutputStream os) throws Exception { List lists = new ArrayList(); if (!Assert.isEmpty(idsStr)) { String[] ids = idsStr.split(","); List list = ***.exportByIds(ids); for(int i = 0;i<list.size();i++) { Data data = new Data(); data.setId(i+1); data.setTitle(list.get(i).getTitle()); lists.add(data); } } //定义导出参数 ExportParams params = new ExportParams(); params.setTitle("设置表头"); Workbook workbook = ExcelExportUtil.exportExcel(params, CollectFile_export.class, lists); String fname = "文件名"+ DateUtil.dateToString("yyyy-MM-dd", new Date()); fname = URLEncoder.encode(fname,"UTF-8"); response.reset();//清空输出流 response.setCharacterEncoding("UTF-8");//设置相应内容的编码格式 response.setHeader("Content-Disposition","attachment;filename="+new String(fname.getBytes("UTF-8"),"GBK")+".xls"); response.setContentType("application/msexcel");//定义输出类型 //将文件写入输出流 workbook.write(os); workbook.close(); os.close(); }
数据导入
@RequestMapping("admin/data") @ResponseBody public ReturnBean enterprise_export_insert(HttpServletRequest request, @RequestParam("excelFile") MultipartFile file) { ReturnBean rb; // 导入参数设置 ImportParams params = new ImportParams(); params.setTitleRows(0); params.setHeadRows(1); params.setNeedSave(false); try { List<Enterprise_export> dataList = ExcelImportUtil.importExcel(file.getInputStream(), Enterprise_export.class, params); System.out.println(dataList); enterpriseService.insertExport(dataList); rb = ReturnBean.getSuccessReturnBean(); } catch (Exception e) { rb = ReturnBean.getErrorReturnBean(); } return rb; }