//批量导出+文件下载+文件删除
@RequestMapping("/downLoad")
public String dasa(HttpServletResponse response) throws IOException {
String path = ResourceUtils.getURL("classpath:").getPath()+"/static/files";
String dataDirPath = path+"/"+new SimpleDateFormat("yyy-MM-dd").format(new Date());
File dateFile = new File(dataDirPath);
if(!dateFile.exists()){
dateFile.mkdirs();
}
String dateFile1 = dateFile+"/"+"save.xlsx";
ExcelWriterBuilder workBook = EasyExcel.write(dateFile1,Student.class);
List<Student> s = new ArrayList<>();
//这里设置需要导出的数据,可以从数据库导入
ExcelWriterSheetBuilder sheet = workBook.sheet();
sheet.doWrite(s);
File date = new File(dateFile1);
FileInputStream is = new FileInputStream(date);
response.setHeader("Content-Disposition",
"attachment; filename=" + java.net.URLEncoder.encode("save.xlsx", "UTF-8"));
ServletOutputStream os = response.getOutputStream();
IOUtils.copy(is,os);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
if(date.exists()){
date.delete();
}
return path;
}
//上传加批量导入加文件删除
@RequestMapping("/upLoad")
public String dab(MultipartFile multipartFile) throws IOException {
String oldName = multipartFile.getOriginalFilename();
String path = ResourceUtils.getURL("classpath:").getPath()+"/static/files";
String dataDirPath = path+"/"+new SimpleDateFormat("yyy-MM-dd").format(new Date());
File dateFile = new File(dataDirPath);
if(!dateFile.exists()){
dateFile.mkdirs();
}
multipartFile.transferTo(new File(dataDirPath,oldName));
String patha = dataDirPath+"/"+oldName;
ExcelReaderBuilder workBook = EasyExcel.read(patha, Student.class, new StudentListener());
ExcelReaderSheetBuilder sheet = workBook.sheet();
sheet.doRead();
File date = new File(dataDirPath,oldName);
if(date.exists()){
date.delete();
}
return path;
}
public class StudentListener extends AnalysisEventListener<Student> {
@Override
public void invoke(Student student, AnalysisContext analysisContext) {
System.out.println("student:"+student);
//这里得到数据批量导入数据,进行相应的操作,可以存入数据库等
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}