feat:generate excel template, parsing excel data
This commit is contained in:
parent
3009ae206f
commit
f89381d318
@ -1,9 +1,9 @@
|
|||||||
package com.ensign.crm.framework.excel.core.util;
|
package com.ensign.crm.framework.excel.core.util;
|
||||||
|
|
||||||
import com.ensign.crm.framework.excel.core.handler.SelectSheetWriteHandler;
|
|
||||||
import com.alibaba.excel.EasyExcel;
|
import com.alibaba.excel.EasyExcel;
|
||||||
import com.alibaba.excel.converters.longconverter.LongStringConverter;
|
import com.alibaba.excel.converters.longconverter.LongStringConverter;
|
||||||
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
||||||
|
import com.ensign.crm.framework.excel.core.handler.SelectSheetWriteHandler;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
@ -32,6 +32,10 @@ public class ExcelUtils {
|
|||||||
*/
|
*/
|
||||||
public static <T> void write(HttpServletResponse response, String filename, String sheetName,
|
public static <T> void write(HttpServletResponse response, String filename, String sheetName,
|
||||||
Class<T> head, List<T> data) throws IOException {
|
Class<T> head, List<T> data) throws IOException {
|
||||||
|
|
||||||
|
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
|
||||||
|
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
|
||||||
|
|
||||||
// 输出 Excel
|
// 输出 Excel
|
||||||
EasyExcel.write(response.getOutputStream(), head)
|
EasyExcel.write(response.getOutputStream(), head)
|
||||||
.autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理
|
.autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理
|
||||||
@ -40,8 +44,6 @@ public class ExcelUtils {
|
|||||||
.registerConverter(new LongStringConverter()) // 避免 Long 类型丢失精度
|
.registerConverter(new LongStringConverter()) // 避免 Long 类型丢失精度
|
||||||
.sheet(sheetName).doWrite(data);
|
.sheet(sheetName).doWrite(data);
|
||||||
// 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了
|
// 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了
|
||||||
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
|
|
||||||
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> List<T> read(MultipartFile file, Class<T> head) throws IOException {
|
public static <T> List<T> read(MultipartFile file, Class<T> head) throws IOException {
|
||||||
|
@ -79,5 +79,10 @@
|
|||||||
<groupId>com.ensign</groupId>
|
<groupId>com.ensign</groupId>
|
||||||
<artifactId>ensign-spring-boot-starter-biz-tenant</artifactId>
|
<artifactId>ensign-spring-boot-starter-biz-tenant</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ensign</groupId>
|
||||||
|
<artifactId>ensign-spring-boot-starter-excel</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.ensign.crm.module.crm.controller.crm;
|
||||||
|
|
||||||
|
import com.ensign.crm.framework.common.pojo.CommonResult;
|
||||||
|
import com.ensign.crm.module.crm.model.DealerRegistrationModel;
|
||||||
|
import com.ensign.crm.module.crm.service.ExcelTemplateService;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 生成Excel模板,解析Excel
|
||||||
|
* @Date: 29/11/2024 2:27 pm
|
||||||
|
* @Created: by ZZSLL
|
||||||
|
*/
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Tag(name = "CRM - excel")
|
||||||
|
@RequestMapping("/crm-api/proxy/excel")
|
||||||
|
public class ExcelTemplateController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ExcelTemplateService excelTemplateService;
|
||||||
|
|
||||||
|
@GetMapping("/download-template")
|
||||||
|
@Operation(summary = "下载经销商登记引出模板")
|
||||||
|
public void generateTemplate(HttpServletResponse response) throws IOException {
|
||||||
|
excelTemplateService.generateDealerRegistration(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/upload/parsing")
|
||||||
|
@Operation(summary = "解析经销商登记Excel数据")
|
||||||
|
public CommonResult<List<DealerRegistrationModel>> parsing(MultipartFile file) throws IOException {
|
||||||
|
return CommonResult.success(excelTemplateService.parsingExcel(file));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.ensign.crm.module.crm.model;
|
||||||
|
|
||||||
|
import com.alibaba.excel.annotation.ExcelProperty;
|
||||||
|
import com.ensign.crm.framework.excel.core.annotations.DictFormat;
|
||||||
|
import com.ensign.crm.framework.excel.core.annotations.ExcelColumnSelect;
|
||||||
|
import com.ensign.crm.framework.excel.core.convert.DictConvert;
|
||||||
|
import com.ensign.crm.module.system.enums.DictTypeConstants;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 经销商登记分录
|
||||||
|
* @Date: 29/11/2024 2:36 pm
|
||||||
|
* @Created: by ZZSLL
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Accessors(chain = false)
|
||||||
|
public class DealerRegistrationModel {
|
||||||
|
|
||||||
|
@ExcelProperty("公司名称或客户姓名")
|
||||||
|
private String yem_tcompanyname_jxs;
|
||||||
|
|
||||||
|
@ExcelProperty("客户联系人")
|
||||||
|
private String yem_customerlxr_jxs;
|
||||||
|
|
||||||
|
@ExcelProperty("客户联系方式")
|
||||||
|
private String yem_customercontact_jxs;
|
||||||
|
|
||||||
|
@ExcelProperty(value = "区域", converter = DictConvert.class)
|
||||||
|
@DictFormat(DictTypeConstants.REGION_OPTION)
|
||||||
|
@ExcelColumnSelect(dictType = DictTypeConstants.REGION_OPTION)
|
||||||
|
private String yem_region_jxs;
|
||||||
|
|
||||||
|
@ExcelProperty(value = "意向强度", converter = DictConvert.class)
|
||||||
|
@DictFormat(DictTypeConstants.INTENTION_OPTION)
|
||||||
|
@ExcelColumnSelect(dictType = DictTypeConstants.INTENTION_OPTION)
|
||||||
|
private String yem_intentionality;
|
||||||
|
|
||||||
|
@ExcelProperty("转换客户")
|
||||||
|
private String yem_switching;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.ensign.crm.module.crm.model;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 区域下拉列 标题-值
|
||||||
|
* @Date: 29/11/2024 2:48 pm
|
||||||
|
* @Created: by ZZSLL
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class RegionOption { private String label; private String value;}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.ensign.crm.module.crm.service;
|
||||||
|
|
||||||
|
import com.ensign.crm.framework.excel.core.util.ExcelUtils;
|
||||||
|
import com.ensign.crm.module.crm.model.DealerRegistrationModel;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: TODO
|
||||||
|
* @Date: 29/11/2024 2:29 pm
|
||||||
|
* @Created: by ZZSLL
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ExcelTemplateService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 经销商登记
|
||||||
|
*
|
||||||
|
* @param response
|
||||||
|
*/
|
||||||
|
public void generateDealerRegistration(HttpServletResponse response) throws IOException {
|
||||||
|
|
||||||
|
List<DealerRegistrationModel> emptyData = new ArrayList<>();
|
||||||
|
ExcelUtils.write(response, "经销商登记引出模板.xlsx", "Sheet1", DealerRegistrationModel.class, emptyData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<DealerRegistrationModel> parsingExcel(MultipartFile file) throws IOException {
|
||||||
|
return ExcelUtils.read(file, DealerRegistrationModel.class);
|
||||||
|
}
|
||||||
|
}
|
@ -24,4 +24,7 @@ public interface DictTypeConstants {
|
|||||||
String SMS_SEND_STATUS = "system_sms_send_status"; // 短信发送状态
|
String SMS_SEND_STATUS = "system_sms_send_status"; // 短信发送状态
|
||||||
String SMS_RECEIVE_STATUS = "system_sms_receive_status"; // 短信接收状态
|
String SMS_RECEIVE_STATUS = "system_sms_receive_status"; // 短信接收状态
|
||||||
|
|
||||||
|
String REGION_OPTION = "crm_region_option";
|
||||||
|
|
||||||
|
String INTENTION_OPTION = "crm_intention_option";
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user