开发必不可少的功能模板下载,不过菜菜鸟也是偷偷借来的,哈哈哈哈
在我看来模板下载逻辑没有导入导出得复杂(可能没有见过牛逼的),就简单记录一下下自己的熟悉的。
Controller
@ApiOperation(value = "导出乡镇附加信息模板", notes = "导出乡镇附加信息模板")
@PreAuthorize("@pms.hasPermission('baseTownMapping_template')")
@GetMapping("/exportTemplate")
public void exportTemplate(HttpServletResponse response) {
try {
// 直接用浏览器或postman
response.setContentType(BaseConstants.EXPORT_CONTENT_TYPE);
response.setCharacterEncoding(BaseConstants.EXPORT_CHARACTER_ENCODING);
// URLEncoder.encode防止中文乱码
String fileName = URLEncoder.encode(BaseConstants.BASE_TOWN_MAPPING_EXPORT_TEMPLATE_NAME, BaseConstants.EXPORT_CHARACTER_ENCODING);
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), BaseTownMappingExcelReadDTO.class).sheet(BaseConstants.BASE_TOWN_MAPPING_EXPORT_TEMPLATE_NAME).doWrite(new ArrayList());
} catch (Exception e) {
log.error("下载文件失败");
}
}
工具类
public interface BaseConstants {
String EXPORT_CONTENT_TYPE = "application/vnd.ms-excel";
String EXPORT_CHARACTER_ENCODING = "UTF-8";
String BASE_TOWN_MAPPING_EXPORT_TEMPLATE_NAME = "关系映射数据模板";
}
DTO
@Data
public class BaseTownMappingExcelReadDTO {
/**
* 系统编码
*/
@ApiModelProperty(value = "系统编码")
@ExcelProperty(index = 0,value = "系统编码(必填)")
private String townCode;
/**
* 系统名称
*/
@ApiModelProperty(value = "系统名称")
@ExcelProperty(index = 1,value = "系统名称")
private String townName;
/**
* 客户编码
*/
@ApiModelProperty(value = "客户编码")
@ExcelProperty(index = 2,value = "客户编码(必填)")
private String swireCode;
/**
* 客户名称
*/
@ApiModelProperty(value = "客户名称")
@ExcelProperty(index = 3,value = "客户名称(必填)")
private String swireName;
@ApiModelProperty(value = "创建人")
@ExcelProperty(index = 4,value = "创建人")
private String createBy;
}