今天给宝子带来一套多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();