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();
相关推荐
mghio5 小时前
Dubbo 中的集群容错
java·微服务·dubbo
咖啡教室10 小时前
java日常开发笔记和开发问题记录
java
咖啡教室10 小时前
java练习项目记录笔记
java
鱼樱前端11 小时前
maven的基础安装和使用--mac/window版本
java·后端
RainbowSea11 小时前
6. RabbitMQ 死信队列的详细操作编写
java·消息队列·rabbitmq
RainbowSea11 小时前
5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
java·消息队列·rabbitmq
我不会编程55513 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
李少兄13 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
此木|西贝13 小时前
【设计模式】原型模式
java·设计模式·原型模式