JAVA-easyexcel多sheet页导入

今天给宝子带来一套多sheet页导入的模板,话不多说直接上代码

java 复制代码
   String localFilePath = "file.xlsx";
      

        JSONObject jsonObject = JSON.parseObject(file);
        String useFile = jsonObject.getString("file");

        useFile=useFile.replace("\\\\","/");
        System.out.println("222222"+useFile);

        if (useFile.startsWith("http")||useFile.startsWith("https")||useFile.startsWith("ftp")){
            System.out.println("11111111111111111111");
            URL url = new URL(useFile);

            // 打开远程文件的输入流
            BufferedInputStream inputStream = new BufferedInputStream(url.openStream());

            // 创建本地文件的输出流
            FileOutputStream outputStream = new FileOutputStream(localFilePath);

            // 从输入流读取数据并写入输出流
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer, 0, 1024)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            // 关闭流
            inputStream.close();
            outputStream.close();

        }else {
            localFilePath=useFile;
        }
        
        EasyExcel.read(localFilePath, CustomerExcel.class,new CustomerExcelListener()).sheet(0).doRead();
        EasyExcel.read(localFilePath, CustomerBankExcel.class,new CustomerBankExcelListener()).sheet(0).doRead();
        EasyExcel.read(localFilePath, CustomerAddressExcel.class,new CustomerAddressExcelListener()).sheet(0).doRead();
        EasyExcel.read(localFilePath, CustomerCreditExcel.class,new CustomerCreditExcelListener()).sheet(0).doRead();
        EasyExcel.read(localFilePath, CustomerSaleExcel.class,new CustomerSaleExcelListener()).sheet(0).doRead();

        List<Customer> customers = CustomerExcelListener.list;
        List<CustomerBank> customerBanks = CustomerBankExcelListener.list;
        List<CustomerAddress> customerAddresses = CustomerAddressExcelListener.list;
        List<CustomerCredit> customerCredits = CustomerCreditExcelListener.list;
        List<CustomerSale> customerSales = CustomerSaleExcelListener.list;
        
        customers.stream().forEach(customer -> {
            LambdaQueryWrapper<Customer> eq = new QueryWrapper<Customer>().lambda()
                    .eq(Customer::getCode, customer.getCode());
            Customer one = customerService.getOne(eq);
            if (one!=null){
                BeanUtils.copyProperties(customer,one);
                customerService.updateById(one);
            }else {
                customerService.save(customer);
            }
        });

        customerBanks.stream().forEach(customerBank -> {
            LambdaQueryWrapper<CustomerBank> eq = new QueryWrapper<CustomerBank>().lambda()
                    .eq(CustomerBank::getCustomerCode, customerBank.getCustomerCode());
            CustomerBank one = customerBankService.getOne(eq);
            if (one!=null){
                BeanUtils.copyProperties(customerBank,one);
                customerBankService.updateById(one);
            }else {
                customerBankService.save(customerBank);
            }
        });
        customerAddresses.stream().forEach(customerAddress -> {
            LambdaQueryWrapper<CustomerAddress> eq = new QueryWrapper<CustomerAddress>().lambda()
                    .eq(CustomerAddress::getCustomerCode, customerAddress.getCustomerCode());
            CustomerAddress one = customerAddressService.getOne(eq);
            if (one!=null){
                BeanUtils.copyProperties(customerAddress,one);
                customerAddressService.updateById(one);
            }else {
                customerAddressService.save(customerAddress);
            }
        });

        customerCredits.stream().forEach(customerCredit -> {
            LambdaQueryWrapper<CustomerCredit> eq = new QueryWrapper<CustomerCredit>().lambda()
                    .eq(CustomerCredit::getCustomerCode, customerCredit.getCustomerCode());
            CustomerCredit one = customerCreditService.getOne(eq);
            if (one!=null){
                BeanUtils.copyProperties(customerCredit,one);
                customerCreditService.updateById(one);
            }else {
                customerCreditService.save(customerCredit);
            }
        });


        customerSales.stream().forEach(customerSale -> {
            LambdaQueryWrapper<CustomerSale> eq = new QueryWrapper<CustomerSale>().lambda()
                    .eq(CustomerSale::getCustomerCode, customerSale.getCustomerCode());
            CustomerSale one = customerSaleService.getOne(eq);
            if (one!=null){
                BeanUtils.copyProperties(customerSale,one);
                customerSaleService.updateById(one);
            }else {
                customerSaleService.save(customerSale);
            }
        });
        
        customerCredits.clear();
        customers.clear();
        customerSales.clear();
        customerAddresses.clear();
        customerBanks.clear();
        return JsonResponse.ok();
相关推荐
守护者17012 分钟前
JAVA学习-练习试用Java实现“使用Arrays.toString方法将数组转换为字符串并打印出来”
java·学习
源码哥_博纳软云14 分钟前
JAVA同城服务场馆门店预约系统支持H5小程序APP源码
java·开发语言·微信小程序·小程序·微信公众平台
禾高网络15 分钟前
租赁小程序成品|租赁系统搭建核心功能
java·人工智能·小程序
学会沉淀。21 分钟前
Docker学习
java·开发语言·学习
如若12322 分钟前
对文件内的文件名生成目录,方便查阅
java·前端·python
西猫雷婶1 小时前
python学opencv|读取图像(二十一)使用cv2.circle()绘制圆形进阶
开发语言·python·opencv
kiiila1 小时前
【Qt】对象树(生命周期管理)和字符集(cout打印乱码问题)
开发语言·qt
初晴~1 小时前
【Redis分布式锁】高并发场景下秒杀业务的实现思路(集群模式)
java·数据库·redis·分布式·后端·spring·
小_太_阳1 小时前
Scala_【2】变量和数据类型
开发语言·后端·scala·intellij-idea
直裾1 小时前
scala借阅图书保存记录(三)
开发语言·后端·scala