客户资源后台导出数据到excel和导出excel模板

master
river 4 years ago
parent c20898fe81
commit daf76a07bb
  1. 20
      dq-financial-api/src/main/java/com/daqing/financial/crms/CustomerControllerApi.java
  2. BIN
      dq-financial-api/target/classes/com/daqing/financial/crms/CustomerControllerApi.class
  3. 32
      dq-financial-crms/src/main/java/com/daqing/financial/crms/controller/CustomerController.java
  4. 1
      dq-financial-crms/src/main/java/com/daqing/financial/crms/dao/CustomerDao.java
  5. 3
      dq-financial-crms/src/main/java/com/daqing/financial/crms/feign/HrmsFeignService.java
  6. 9
      dq-financial-crms/src/main/java/com/daqing/financial/crms/service/CustomerService.java
  7. 69
      dq-financial-crms/src/main/java/com/daqing/financial/crms/service/impl/CustomerServiceImpl.java
  8. 7
      dq-financial-crms/src/main/resources/mapper/crms/CustomerDao.xml
  9. BIN
      dq-financial-crms/target/classes/com/daqing/financial/crms/controller/CustomerController.class
  10. BIN
      dq-financial-crms/target/classes/com/daqing/financial/crms/dao/CustomerDao.class
  11. BIN
      dq-financial-crms/target/classes/com/daqing/financial/crms/service/CustomerService.class
  12. BIN
      dq-financial-crms/target/classes/com/daqing/financial/crms/service/impl/CustomerServiceImpl.class
  13. 7
      dq-financial-crms/target/classes/mapper/crms/CustomerDao.xml
  14. 9
      dq-financial-hrms/src/main/java/com/daqing/financial/hrms/controller/EmployeeController.java
  15. 2
      dq-financial-hrms/src/main/java/com/daqing/financial/hrms/dao/EmployeeDao.java
  16. 3
      dq-financial-hrms/src/main/java/com/daqing/financial/hrms/service/EmployeeService.java
  17. 7
      dq-financial-hrms/src/main/java/com/daqing/financial/hrms/service/impl/EmployeeServiceImpl.java
  18. 6
      dq-financial-hrms/src/main/resources/mapper/hrms/EmployeeDao.xml
  19. 5
      dq-framework-model/pom.xml
  20. 21
      dq-framework-model/src/main/java/com/daqing/framework/domain/crms/ext/CustomerCompanyVO.java
  21. 27
      dq-framework-model/src/main/java/com/daqing/framework/domain/crms/ext/CustomerPersonalVO.java
  22. 2
      dq-framework-model/src/main/java/com/daqing/framework/domain/crms/ext/CustomerTO.java
  23. 2
      dq-framework-model/src/main/java/com/daqing/framework/domain/hrms/ext/EmployeeVO.java
  24. BIN
      dq-framework-model/target/classes/com/daqing/framework/domain/crms/ext/CustomerCompanyVO.class
  25. BIN
      dq-framework-model/target/classes/com/daqing/framework/domain/crms/ext/CustomerPersonalVO.class
  26. BIN
      dq-framework-model/target/classes/com/daqing/framework/domain/crms/ext/CustomerTO.class
  27. BIN
      dq-framework-model/target/classes/com/daqing/framework/domain/hrms/ext/EmployeeVO.class
  28. 16
      dq-framework-utils/pom.xml
  29. 11
      dq-framework-utils/src/main/java/com/daqing/framework/utils/excel/ExcelException.java
  30. 61
      dq-framework-utils/src/main/java/com/daqing/framework/utils/excel/ExcelListener.java
  31. 148
      dq-framework-utils/src/main/java/com/daqing/framework/utils/excel/ExcelUtil.java
  32. 51
      dq-framework-utils/src/main/java/com/daqing/framework/utils/excel/ExcelWriterFactroy.java
  33. 2
      dq-framework-utils/src/main/java/com/daqing/framework/utils/oss/OssFileUtils.java
  34. 29
      dq-framework-utils/src/main/java/com/daqing/framework/utils/poi/ExcelAttribute.java
  35. 87
      dq-framework-utils/src/main/java/com/daqing/framework/utils/poi/ExcelExportUtil.java
  36. 122
      dq-framework-utils/src/main/java/com/daqing/framework/utils/poi/ExcelImportUtil.java
  37. BIN
      dq-framework-utils/target/classes/META-INF/dq-framework-utils.kotlin_module
  38. BIN
      dq-framework-utils/target/classes/com/daqing/framework/utils/excel/ExcelException.class
  39. BIN
      dq-framework-utils/target/classes/com/daqing/framework/utils/excel/ExcelListener.class
  40. BIN
      dq-framework-utils/target/classes/com/daqing/framework/utils/excel/ExcelUtil.class
  41. BIN
      dq-framework-utils/target/classes/com/daqing/framework/utils/excel/ExcelWriterFactroy.class
  42. BIN
      dq-framework-utils/target/classes/com/daqing/framework/utils/oss/OssFileUtils.class

@ -7,7 +7,9 @@ import com.daqing.framework.domain.crms.request.CustomerRequest;
import com.daqing.framework.model.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
@ -61,4 +63,22 @@ public interface CustomerControllerApi {
*/
@ApiOperation(value = "更新企业类型客户信息", notes = "更新企业类型客户信息")
ResponseResult updateCompany(CustomerCompanyTO customerCompanyTO);
/**
* 导入excel数据
*/
@ApiOperation(value = "导入excel数据", notes = "导入excel数据")
void excelImportCustomer(MultipartFile file);
/**
* 导出数据到excel
*/
@ApiOperation(value = "导出数据到excel", notes = "导出数据到excel")
void excelExportCustomer(HttpServletResponse response);
/**
* 导出excel模板
*/
@ApiOperation(value = "导出excel模板", notes = "导出excel模板")
void excelTemplate(HttpServletResponse response);
}

@ -9,7 +9,9 @@ import com.daqing.framework.model.response.ResponseResult;
import com.daqing.framework.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@ -107,4 +109,34 @@ public class CustomerController implements CustomerControllerApi {
boolean result = customerService.updateCustomerCompany(customerCompanyTO.getCustomerEntity(), customerCompanyTO.getCompanyCustomerEntity());
return result ? ResponseResult.SUCCESS() : ResponseResult.FAIL();
}
/**
* 导出数据到excel
*/
@Override
@GetMapping("/excelExport")
public void excelExportCustomer(HttpServletResponse response){
customerService.excelExportCustomer(response);
}
/**
* 导出excel模板
*/
@Override
@GetMapping("/excelTemplate")
public void excelTemplate(HttpServletResponse response){
customerService.excelTemplate(response);
}
/**
* 导入excel数据
*/
@PostMapping("/excelImport")
@Override
public void excelImportCustomer(MultipartFile file){
customerService.excelImportCustomer(file);
}
}

@ -33,4 +33,5 @@ public interface CustomerDao extends BaseMapper<CustomerEntity> {
boolean updateCustomer(CustomerEntity customerEntity);
List<Long> listCustomerId();
}

@ -2,6 +2,7 @@ package com.daqing.financial.crms.feign;
import com.daqing.framework.domain.hrms.EmployeeEntity;
import com.daqing.framework.domain.hrms.ext.EmployeeTO;
import com.daqing.framework.domain.hrms.ext.EmployeeVO;
import com.daqing.framework.model.response.ResponseResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
@ -25,4 +26,6 @@ public interface HrmsFeignService {
@GetMapping("/hrms/employee/getEmployeeById")
ResponseResult<EmployeeEntity> getEmployeeById(@RequestParam("id") Long id);
@GetMapping("/hrms/employee/listEmployeeName")
ResponseResult<List<EmployeeVO>> listEmployeeName();
}

@ -7,7 +7,10 @@ import com.daqing.framework.domain.crms.request.CustomerRequest;
import com.daqing.framework.model.response.ResponseResult;
import com.daqing.framework.utils.PageUtils;
import com.daqing.framework.domain.crms.CustomerEntity;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@ -33,5 +36,11 @@ public interface CustomerService extends IService<CustomerEntity> {
boolean updateCustomerPersonal(CustomerEntity customerEntity, PersonalCustomerEntity personalCustomerEntity);
boolean updateCustomerCompany(CustomerEntity customerEntity, CompanyCustomerEntity companyCustomerEntity);
void excelExportCustomer(HttpServletResponse response);
void excelImportCustomer(MultipartFile excel);
void excelTemplate(HttpServletResponse response);
}

@ -18,14 +18,18 @@ import com.daqing.framework.domain.crms.ext.CustomerCompanyVO;
import com.daqing.framework.domain.crms.ext.CustomerPersonalVO;
import com.daqing.framework.domain.crms.request.CustomerRequest;
import com.daqing.framework.domain.hrms.ext.EmployeeTO;
import com.daqing.framework.domain.hrms.ext.EmployeeVO;
import com.daqing.framework.model.response.ResponseResult;
import com.daqing.framework.utils.PageUtils;
import com.daqing.framework.utils.SnowflakeIdUtils;
import com.daqing.framework.utils.excel.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;
@ -202,6 +206,67 @@ public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity
return customerDao.updateCustomer(customerEntity) && companyCustomerDao.updateCompanyCustomer(companyCustomerEntity);
}
/**
* 导出数据到excel
* @param response
*/
@Override
public void excelExportCustomer(HttpServletResponse response){
//TODO 查询所有的客户详细信息,并按照类型封装,后续有待优化,消耗时间太长
List<CustomerCompanyVO> companyList = new ArrayList<>();
List<CustomerPersonalVO> personalList = new ArrayList<>();
List<Long> longList = customerDao.listCustomerId();
for (Long id : longList) {
ResponseResult responseResult = this.queryCustomerById(id);
if ((responseResult.getData()).getClass() == CustomerCompanyVO.class){
companyList.add((CustomerCompanyVO) responseResult.getData());
}else {
personalList.add((CustomerPersonalVO) responseResult.getData());
}
}
ExcelUtil.writeExcelWithSheets(response,personalList,"客户资源信息一览表","个人类型",new CustomerPersonalVO())
.write(companyList,"企业类型",new CustomerCompanyVO())
.finish();
}
/**
* 导出excel模板
*/
@Override
public void excelTemplate(HttpServletResponse response) {
List<CustomerCompanyVO> companyList = new ArrayList<>();
List<CustomerPersonalVO> personalList = new ArrayList<>();
ExcelUtil.writeExcelWithSheets(response,personalList,"客户资源信息表模板","个人类型",new CustomerPersonalVO())
.write(companyList,"企业类型",new CustomerCompanyVO())
.finish();
}
/**
* 导入excel数据
* @param excel
*/
@Override
public void excelImportCustomer(MultipartFile excel) {
//TODO 导入excel数据到数据库,思路耗时太长,有待优化
List<Object> personalList = ExcelUtil.readExcel(excel, new CustomerPersonalVO(), 1);
List<Object> companyList = ExcelUtil.readExcel(excel, new CustomerCompanyVO(), 2);
List<EmployeeVO> nameList = hrmsFeignService.listEmployeeName().getData();
List<CustomerEntity> customerList = new ArrayList<>();
CustomerEntity customer;
List<PersonalCustomerEntity> pcList = new ArrayList<>();
PersonalCustomerEntity pc;
for (Object personal : personalList) {
customer = new CustomerEntity();
pc = new PersonalCustomerEntity();
BeanUtils.copyProperties(personal,customer);
BeanUtils.copyProperties(personal,pc);
customerList.add(customer);
pcList.add(pc);
}
System.out.println(customerList);
System.out.println(pcList);
}
/**
* 选择某段时间内客户信息的时间参数校验
*
@ -316,7 +381,7 @@ public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity
customerPersonalVO.setType(customer.getType());
customerPersonalVO.setManager(name);
customerPersonalVO.setName(customer.getName());
customerPersonalVO.setAddress(customer.getAddr());
customerPersonalVO.setAddr(customer.getAddr());
customerPersonalVO.setPhone(customer.getPhone());
customerPersonalVO.setIdCard(personal.getIdCard());
customerPersonalVO.setAge(personal.getAge());
@ -350,7 +415,7 @@ public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity
customerCompanyVO.setType(customer.getType());
customerCompanyVO.setManager(name);
customerCompanyVO.setName(customer.getName());
customerCompanyVO.setAddress(customer.getAddr());
customerCompanyVO.setAddr(customer.getAddr());
customerCompanyVO.setPhone(customer.getPhone());
customerCompanyVO.setRegisteredCapital(company.getRegisteredCapital());
customerCompanyVO.setIndustry(company.getIndustry());

@ -83,4 +83,11 @@
WHERE id = #{id}
</update>
<!-- 查询所有的客户基本信息 -->
<select id="listCustomerId" resultType="long">
SELECT id
FROM crms_customer
WHERE del_or_not = 0;
</select>
</mapper>

@ -83,4 +83,11 @@
WHERE id = #{id}
</update>
<!-- 查询所有的客户基本信息 -->
<select id="listCustomerId" resultType="long">
SELECT id
FROM crms_customer
WHERE del_or_not = 0;
</select>
</mapper>

@ -4,6 +4,7 @@ import com.daqing.financial.hrms.EmployeeControllerApi;
import com.daqing.financial.hrms.service.EmployeeService;
import com.daqing.framework.domain.hrms.EmployeeEntity;
import com.daqing.framework.domain.hrms.ext.EmployeeTO;
import com.daqing.framework.domain.hrms.ext.EmployeeVO;
import com.daqing.framework.model.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -101,4 +102,12 @@ public class EmployeeController implements EmployeeControllerApi {
public ResponseResult getEmployeeById(@RequestParam("id") Long id) {
return new ResponseResult<EmployeeEntity>().SUCCESS(employeeService.getEmployeeById(id));
}
/**
* 查询所有员工姓名和id
*/
@GetMapping("/listEmployeeName")
public ResponseResult listEmployeeName() {
return new ResponseResult<List<EmployeeVO>>().SUCCESS(employeeService.listEmployeeName());
}
}

@ -24,4 +24,6 @@ public interface EmployeeDao extends BaseMapper<EmployeeEntity> {
EmployeeEntity getEmployeeById(Long id);
List<EmployeeVO> listEmployeeByDeptId(Long id);
List<EmployeeVO> listEmployeeName();
}

@ -2,6 +2,7 @@ package com.daqing.financial.hrms.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.daqing.framework.domain.hrms.ext.EmployeeTO;
import com.daqing.framework.domain.hrms.ext.EmployeeVO;
import com.daqing.framework.utils.PageUtils;
import com.daqing.framework.domain.hrms.EmployeeEntity;
@ -20,5 +21,7 @@ public interface EmployeeService extends IService<EmployeeEntity> {
List<EmployeeTO> getEmployeeAndDeptById(List<Long> ids);
EmployeeEntity getEmployeeById(Long id);
List<EmployeeVO> listEmployeeName();
}

@ -1,6 +1,7 @@
package com.daqing.financial.hrms.service.impl;
import com.daqing.framework.domain.hrms.ext.EmployeeTO;
import com.daqing.framework.domain.hrms.ext.EmployeeVO;
import org.springframework.stereotype.Service;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -32,4 +33,10 @@ public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao, EmployeeEntity
return this.getBaseMapper().getEmployeeById(id);
}
@Override
public List<EmployeeVO> listEmployeeName() {
return this.getBaseMapper().listEmployeeName();
}
}

@ -57,4 +57,10 @@
ON ed.dept_id = d.id
WHERE d.id = #{id}
</select>
<!-- 查询所有的员工姓名和id -->
<select id="listEmployeeName" resultType="com.daqing.framework.domain.hrms.ext.EmployeeVO">
SELECT id,name
FROM hrms_employee
</select>
</mapper>

@ -16,5 +16,10 @@
<artifactId>dq-framework-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.daqing.framework</groupId>
<artifactId>dq-framework-utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

@ -1,5 +1,7 @@
package com.daqing.framework.domain.crms.ext;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.Data;
import lombok.ToString;
@ -13,66 +15,81 @@ import java.io.Serializable;
*/
@Data
@ToString
public class CustomerCompanyVO implements Serializable {
public class CustomerCompanyVO extends BaseRowModel implements Serializable {
/**
* 客户编号
*/
@ExcelProperty(value = "编号",index = 0)
private String code;
/**
* 客户类型1企业类型0个人类型
*/
@ExcelProperty(value = "企业类型",index = 1)
private Integer type;
/**
* 客户经理
*/
@ExcelProperty(value = "客户经理",index = 2)
private String manager;
/**
* 客户名称
*/
@ExcelProperty(value = "姓名",index = 3)
private String name;
/**
* 联系地址
*/
private String address;
@ExcelProperty(value = "地址",index = 4)
private String addr;
/**
* 联系电话
*/
@ExcelProperty(value = "电话",index = 5)
private String phone;
/**
* 注册资金
*/
@ExcelProperty(value = "注册资金",index = 6)
private String registeredCapital;
/**
* 所属行业
*/
@ExcelProperty(value = "所属行业",index = 7)
private String industry;
/**
* 成立年限
*/
@ExcelProperty(value = "成立年限",index = 8)
private Integer years;
/**
* 所在区域
*/
@ExcelProperty(value = "所在区域",index = 9)
private String region;
/**
* 股东名称
*/
@ExcelProperty(value = "股东名称",index = 10)
private String shareholder;
/**
* 关联企业
*/
@ExcelProperty(value = "关联企业",index = 11)
private String affiliatedCompany;
/**
* 员工个数
*/
@ExcelProperty(value = "员工个数",index = 12)
private String employeeNumber;
/**
* 联系人
*/
@ExcelProperty(value = "联系人",index = 13)
private String linkman;
/**
* 业务来源
*/
@ExcelProperty(value = "业务来源",index = 14)
private String businessSource;
}

@ -1,5 +1,7 @@
package com.daqing.framework.domain.crms.ext;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.Data;
import lombok.ToString;
@ -13,90 +15,111 @@ import java.io.Serializable;
*/
@Data
@ToString
public class CustomerPersonalVO implements Serializable {
public class CustomerPersonalVO extends BaseRowModel implements Serializable {
/**
* 客户编号
*/
@ExcelProperty(value = "编号",index = 0)
private String code;
/**
* 客户类型1企业类型0个人类型
*/
@ExcelProperty(value = "个人类型",index = 1)
private Integer type;
/**
* 客户经理
*/
@ExcelProperty(value = "客户经理",index = 2)
private String manager;
/**
* 客户名称
*/
@ExcelProperty(value = "姓名",index = 3)
private String name;
/**
* 联系地址
*/
private String address;
@ExcelProperty(value = "地址",index = 4)
private String addr;
/**
* 联系电话
*/
@ExcelProperty(value = "电话",index = 5)
private String phone;
/**
* 身份证号
*/
@ExcelProperty(value = "身份证号",index = 6)
private String idCard;
/**
* 年龄
*/
@ExcelProperty(value = "年龄",index = 7)
private Integer age;
/**
* 性别10
*/
@ExcelProperty(value = "性别(1:男,0:女)",index = 8)
private Integer gender;
/**
* 婚姻状况1已婚0未婚
*/
@ExcelProperty(value = "婚姻状况(1:已婚,0:未婚)",index = 9)
private Integer maritalStatus;
/**
* 学历
*/
@ExcelProperty(value = "学历",index = 10)
private String education;
/**
* 工作单位
*/
@ExcelProperty(value = "工作单位",index = 11)
private String employer;
/**
* 职务
*/
@ExcelProperty(value = "职务",index = 12)
private String position;
/**
* 工作年限
*/
@ExcelProperty(value = "工作年限",index = 13)
private Integer workingYears;
/**
* 社保账号
*/
@ExcelProperty(value = "社保账号",index = 14)
private String socialSecurityNum;
/**
* 居住情况
*/
@ExcelProperty(value = "居住情况",index = 15)
private String livingSituation;
/**
* 户籍地址
*/
@ExcelProperty(value = "户籍地址",index = 16)
private String residenceAddr;
/**
* 业务来源
*/
@ExcelProperty(value = "业务来源",index = 17)
private String businessSource;
/**
* 紧急联系人
*/
@ExcelProperty(value = "紧急联系人",index = 18)
private String emergencyLinkman;
/**
* 紧急联系人关系
*/
@ExcelProperty(value = "紧急联系人关系",index = 19)
private String emergencyLinkmanRelationship;
/**
* 紧急联系人电话
*/
@ExcelProperty(value = "紧急联系人电话",index = 20)
private String emergencyLinkmanPhone;
}

@ -1,5 +1,7 @@
package com.daqing.framework.domain.crms.ext;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.Data;
import lombok.ToString;

@ -1,5 +1,7 @@
package com.daqing.framework.domain.hrms.ext;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.Data;
import lombok.ToString;

@ -38,19 +38,9 @@
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.1.2-beta4</version>
</dependency>
</dependencies>
</project>

@ -0,0 +1,11 @@
package com.daqing.framework.utils.excel;
/**
* 异常
*/
public class ExcelException extends RuntimeException {
public ExcelException(String message) {
super(message);
}
}

@ -0,0 +1,61 @@
package com.daqing.framework.utils.excel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;
/**
* 监听
*/
public class ExcelListener extends AnalysisEventListener {
//自定义用于暂时存储data。
//可以通过实例获取该值
private List<Object> datas = new ArrayList<>();
/**
* 通过 AnalysisContext 对象还可以获取当前 sheet当前行等数据
*/
@Override
public void invoke(Object object, AnalysisContext context) {
//数据存储到list,供批量处理,或后续自己业务逻辑处理。
datas.add(object);
//根据业务自行 do something
doSomething();
/*
如数据过大可以进行定量分批处理
if(datas.size()<=100){
datas.add(object);
}else {
doSomething();
datas = new ArrayList<Object>();
}
*/
}
/**
* 根据业务自行实现该方法
*/
private void doSomething() {
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
/*
datas.clear();
解析结束销毁不用的资源
*/
}
public List<Object> getDatas() {
return datas;
}
public void setDatas(List<Object> datas) {
this.datas = datas;
}
}

@ -0,0 +1,148 @@
package com.daqing.framework.utils.excel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
/**
* 基本工具类
*/
public class ExcelUtil {
/**
* 读取 Excel(多个 sheet)
*
* @param excel 文件
* @param rowModel 实体类映射继承 BaseRowModel
* @return Excel 数据 list
*/
public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel) {
ExcelListener excelListener = new ExcelListener();
ExcelReader reader = getReader(excel, excelListener);
if (reader == null) {
return null;
}
for (Sheet sheet : reader.getSheets()) {
if (rowModel != null) {
sheet.setClazz(rowModel.getClass());
}
reader.read(sheet);
}
return excelListener.getDatas();
}
/**
* 读取某个 sheet Excel
*
* @param excel 文件
* @param rowModel 实体类映射继承 BaseRowModel
* @param sheetNo sheet 的序号 从1开始
* @return Excel 数据 list
*/
public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo) {
return readExcel(excel, rowModel, sheetNo, 1);
}
/**
* 读取某个 sheet Excel
*
* @param excel 文件
* @param rowModel 实体类映射继承 BaseRowModel
* @param sheetNo sheet 的序号 从1开始
* @param headLineNum 表头行数默认为1
* @return Excel 数据 list
*/
public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo,
int headLineNum) {
ExcelListener excelListener = new ExcelListener();
ExcelReader reader = getReader(excel, excelListener);
if (reader == null) {
return null;
}
reader.read(new Sheet(sheetNo, headLineNum, rowModel.getClass()));
return excelListener.getDatas();
}
/**
* 导出 Excel 一个 sheet带表头
*
* @param response HttpServletResponse
* @param list 数据 list每个元素为一个 BaseRowModel
* @param fileName 导出的文件名
* @param sheetName 导入文件的 sheet
* @param object 映射实体类Excel 模型
*/
public static void writeExcel(HttpServletResponse response, List<? extends BaseRowModel> list,
String fileName, String sheetName, BaseRowModel object) {
ExcelWriter writer = new ExcelWriter(getOutputStream(fileName, response), ExcelTypeEnum.XLSX);
Sheet sheet = new Sheet(1, 0, object.getClass());
sheet.setSheetName(sheetName);
writer.write(list, sheet);
writer.finish();
}
/**
* 导出 Excel 多个 sheet带表头
*
* @param response HttpServletResponse
* @param list 数据 list每个元素为一个 BaseRowModel
* @param fileName 导出的文件名
* @param sheetName 导入文件的 sheet
* @param object 映射实体类Excel 模型
*/
public static ExcelWriterFactroy writeExcelWithSheets(HttpServletResponse response, List<? extends BaseRowModel> list,
String fileName, String sheetName, BaseRowModel object) {
ExcelWriterFactroy writer = new ExcelWriterFactroy(getOutputStream(fileName, response), ExcelTypeEnum.XLSX);
Sheet sheet = new Sheet(1, 0, object.getClass());
sheet.setSheetName(sheetName);
writer.write(list, sheet);
return writer;
}
/**
* 导出文件时为Writer生成OutputStream
*/
private static OutputStream getOutputStream(String fileName, HttpServletResponse response) {
//创建本地文件
String filePath = fileName + ".xlsx";
File dbfFile = new File(filePath);
try {
if (!dbfFile.exists() || dbfFile.isDirectory()) {
dbfFile.createNewFile();
}
fileName = new String(filePath.getBytes(), "ISO-8859-1");
response.addHeader("Content-Disposition", "filename=" + fileName);
return response.getOutputStream();
} catch (IOException e) {
throw new ExcelException("创建文件失败!");
}
}
/**
* 返回 ExcelReader
*
* @param excel 需要解析的 Excel 文件
* @param excelListener new ExcelListener()
*/
private static ExcelReader getReader(MultipartFile excel,
ExcelListener excelListener) {
String filename = excel.getOriginalFilename();
if (filename == null || (!filename.toLowerCase().endsWith(".xls") && !filename.toLowerCase().endsWith(".xlsx"))) {
throw new ExcelException("文件格式错误!");
}
InputStream inputStream;
try {
inputStream = new BufferedInputStream(excel.getInputStream());
return new ExcelReader(inputStream, null, excelListener, false);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

@ -0,0 +1,51 @@
package com.daqing.framework.utils.excel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
/**
* 多sheet工具类
*/
public class ExcelWriterFactroy extends ExcelWriter {
private OutputStream outputStream;
private int sheetNo = 1;
public ExcelWriterFactroy(OutputStream outputStream, ExcelTypeEnum typeEnum) {
super(outputStream, typeEnum);
this.outputStream = outputStream;
}
public ExcelWriterFactroy write(List<? extends BaseRowModel> list, String sheetName,
BaseRowModel object) {
this.sheetNo++;
try {
Sheet sheet = new Sheet(sheetNo, 0, object.getClass());
sheet.setSheetName(sheetName);
this.write(list, sheet);
} catch (Exception ex) {
ex.printStackTrace();
try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
return this;
}
@Override
public void finish() {
super.finish();
try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -1,4 +1,4 @@
package com.daqing.framework.utils;
package com.daqing.framework.utils.oss;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectRequest;

@ -1,29 +0,0 @@
package com.daqing.framework.utils.poi;
/**
* @auther River
* @date 2020/9/16 17:10
*/
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelAttribute {
/**
* 对应的列名称
*/
String name() default "";
/**
* excel列的索引
*/
int sort();
/**
* 字段类型对应的格式
*/
String format() default "";
}

@ -1,87 +0,0 @@
package com.daqing.framework.utils.poi;
import lombok.Data;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 导出Excel工具类
* 基于模板打印的方式导出
*/
@Data
public class ExcelExportUtil<T> {
private int rowIndex; //写入数据的起始行
private int styleIndex; //需要提取的样式所在的行号
private Class clazz; //对象的字节码
private Field fields[]; //对象中的所有属性
public ExcelExportUtil(Class clazz, int rowIndex, int styleIndex) {
this.clazz = clazz;
this.rowIndex = rowIndex;
this.styleIndex = styleIndex;
fields = clazz.getDeclaredFields();
}
/**
* 基于注解导出
* 参数
* response
* InputStream:模板的输入流
* objs数据
* fileName生成的文件名
*/
public void export(HttpServletResponse response, InputStream is, List<T> objs, String fileName) throws Exception {
//1.根据模板创建工作簿
XSSFWorkbook workbook = new XSSFWorkbook(is);
//2.读取工作表
Sheet sheet = workbook.getSheetAt(0);
//3.提取公共的样式
CellStyle[] styles = getTemplateStyles(sheet.getRow(styleIndex));
//4.根据数据创建每一行和每一个单元格的数据2
AtomicInteger datasAi = new AtomicInteger(rowIndex); //数字
for (T t : objs) {
//datasAi.getAndIncrement() :获取数字,并++ i++
Row row = sheet.createRow(datasAi.getAndIncrement());
for (int i = 0; i < styles.length; i++) {
Cell cell = row.createCell(i);
cell.setCellStyle(styles[i]);
for (Field field : fields) {
if (field.isAnnotationPresent(ExcelAttribute.class)) {
field.setAccessible(true);
ExcelAttribute ea = field.getAnnotation(ExcelAttribute.class);
if (i == ea.sort()) {
if (field.get(t) != null) {
cell.setCellValue(field.get(t).toString());
}
}
}
}
}
}
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("ISO8859-1")));
response.setHeader("filename", fileName);
workbook.write(response.getOutputStream());
}
CellStyle[] getTemplateStyles(Row row) {
CellStyle[] styles = new CellStyle[row.getLastCellNum()];
for (int i = 0; i < row.getLastCellNum(); i++) {
styles[i] = row.getCell(i).getCellStyle();
}
return styles;
}
}

@ -1,122 +0,0 @@
package com.daqing.framework.utils.poi;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ExcelImportUtil<T> {
private Class clazz;
private Field fields[];
public ExcelImportUtil(Class clazz) {
this.clazz = clazz;
fields = clazz.getDeclaredFields();
}
/**
* 基于注解读取excel
*/
public List<T> readExcel(InputStream is, int rowIndex, int cellIndex) {
List<T> list = new ArrayList<>();
T entity = null;
try {
XSSFWorkbook workbook = new XSSFWorkbook(is);
Sheet sheet = workbook.getSheetAt(0);
// 不准确
// int rowLength = sheet.getLastRowNum();
// System.out.println(sheet.getLastRowNum());
for (int rowNum = rowIndex; rowNum <= sheet.getLastRowNum(); rowNum++) {
Row row = sheet.getRow(rowNum);
entity = (T) clazz.newInstance();
// System.out.println(row.getLastCellNum());
for (int j = cellIndex; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
for (Field field : fields) {
if (field.isAnnotationPresent(ExcelAttribute.class)) {
field.setAccessible(true);
ExcelAttribute ea = field.getAnnotation(ExcelAttribute.class);
if (j == ea.sort()) {
field.set(entity, covertAttrType(field, cell));
}
}
}
}
Field field = entity.getClass().getDeclaredField("index");
field.setAccessible(true);
field.set(entity, rowNum);
list.add(entity);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* 类型转换 将cell 单元格格式转为 字段类型
*/
private Object covertAttrType(Field field, Cell cell) throws Exception {
String fieldType = field.getType().getSimpleName();
if ("String".equals(fieldType)) {
return getValue(cell);
} else if ("Date".equals(fieldType)) {
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(getValue(cell));
} else if ("int".equals(fieldType) || "Integer".equals(fieldType)) {
return Integer.parseInt(getValue(cell));
} else if ("double".equals(fieldType) || "Double".equals(fieldType)) {
return Double.parseDouble(getValue(cell));
} else {
return null;
}
}
/**
* 格式转为String
*
* @param cell
* @return
*/
public String getValue(Cell cell) {
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
case STRING:
return cell.getRichStringCellValue().getString().trim();
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
Date dt = DateUtil.getJavaDate(cell.getNumericCellValue());
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(dt);
} else {
// 防止数值变成科学计数法
String strCell = "";
Double num = cell.getNumericCellValue();
BigDecimal bd = new BigDecimal(num.toString());
if (bd != null) {
strCell = bd.toPlainString();
}
// 去除 浮点型 自动加的 .0
if (strCell.endsWith(".0")) {
strCell = strCell.substring(0, strCell.indexOf("."));
}
return strCell;
}
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
default:
return "";
}
}
}
Loading…
Cancel
Save