文件上传+easyExcel批量导入+ 文件删除,批量导出+文件下载

本文介绍了如何利用Spring框架结合EasyExcel库,实现文件的上传功能,通过EasyExcel进行数据的批量导入和导出,并详细阐述了文件删除及下载的操作流程,为实际项目中的文件管理提供了实用方案。
//批量导出+文件下载+文件删除
@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) {

    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值