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();
相关推荐
乌啼霜满天249几秒前
JDBC编程---Java
java·开发语言·sql
色空大师13 分钟前
23种设计模式
java·开发语言·设计模式
闲人一枚(学习中)14 分钟前
设计模式-创建型-建造者模式
java·设计模式·建造者模式
Bruce小鬼25 分钟前
QT文件基本操作
开发语言·qt
2202_7544215431 分钟前
生成MPSOC以及ZYNQ的启动文件BOOT.BIN的小软件
java·linux·开发语言
蓝染-惣右介34 分钟前
【MyBatisPlus·最新教程】包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段
java·数据库·tomcat·mybatis
小林想被监督学习34 分钟前
idea怎么打开两个窗口,运行两个项目
java·ide·intellij-idea
HoneyMoose36 分钟前
IDEA 2024.3 版本更新主要功能介绍
java·ide·intellij-idea
我只会发热38 分钟前
Java SE 与 Java EE:基础与进阶的探索之旅
java·开发语言·java-ee
是老余39 分钟前
本地可运行,jar包运行错误【解决实例】:通过IDEA的maven package打包多模块项目
java·maven·intellij-idea·jar