阿里Exclel
模板导入
- 思路:map<该坑位的属性名,Integer[]>,数组第一位:行索引,第二位:列索引。
常见问题
1:表格导入,数据对象用lombok,没有显式的get,set方法,属性为空。
需要加注解:
自由选择导出哪几列
设计思路:
1:前端把filed属性名和列的标题(如需国际化)传给前端;
2:再调阿里Excel的
模板导出
注意点
- 实体类属性不能是Object,会爆转化异常:"Convert" support class Object
- 默认解析03版,07版文件注意报错信息
- 记得最后要调用:excelWriter.finish();一定记得,不然不报错,只是空白,wps爆文档需修复
- 这个方法有坑,此代码是导出成功的写法,
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateDirPath+templateName)
.autoCloseStream(Boolean.FALSE).build(); - 单个变量:
- 列表属性变量:{.属性名} 多个点
获取文件磁盘绝对路径
springboot
org.springframework.core.io.Resource resource =resourceLoader.getResource(filePath);
String fileAbsPath = resource.getURL().getPath();
ssm项目
String templateDirPath = request.getSession().getServletContext().getRealPath("/")
+ File.separator + "exporttemp" +File.separator;
controller
点击查看代码
/**
* 导出
* @param id
* @param type 和faultRecordRelation.js的showOneFile函数值保持一致
* @param request
* @param response
* @throws Exception
*/
@RequestMapping("/exportData")
@ResponseBody
public void exportData(@RequestParam String id,@RequestParam String type,
HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
onesluiceOneFileDirecService.exportData(id,type,request,response);
} catch (Exception e) {
e.printStackTrace();
logger.error("一闸一档_导出失败", e);
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().println("导出失败,系统内部错误!");
}
}
html
点击查看代码
parent.window.location = ctx+'/pdms/oneFile/exportData?id='+id+'&type='+type;
Service
点击查看代码
/**
* 导出 表格
* @param id
* @param type
* @param request
* @param response
*/
public void exportData(String id, String type, HttpServletRequest request, HttpServletResponse response) throws IOException {
String fileName = "一闸一档_";
String templateDirPath = request.getSession().getServletContext().getRealPath("/")
+ File.separator + "exporttemp" +File.separator;
String templateName = "";
if("1".equals(type)){
fileName += "AT方式";
templateName = "yizhayidang-at.xls";
}else if("2".equals(type)){
fileName += "直供方式";
// templateName = "yizhayidang-zhigong.xls";
}else if("3".equals(type)){
fileName += "开闭所越级跳闸";
// templateName = "yizhayidang-.xls";
}
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
fileName = URLEncoder.encode(fileName+".xls", "utf-8");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
// responseHeader(response, fileName); 上面四行可抽成方法
//读取Excel模板
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateDirPath+templateName)
.autoCloseStream(Boolean.FALSE).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
if("1".equals(type)){
OnesluiceOneFileAt at = findById(OnesluiceOneFileAt.class, id);
excelWriter.fill(at, writeSheet);
}else {
OnesluiceOneFileDirec direc = findById(OnesluiceOneFileDirec.class, id);
excelWriter.fill(direc, writeSheet);
}
// 当模板还需要动态对表格处理时才加 (跨行跨列)fill.writeContext().writeWorkbookHolder().getWorkbook().getSheetAt(0).createRow(20).createCell(1).setCellValue("03:00");
// 一定记得,不然不报错,只是空白,wps爆文档需修复
excelWriter.finish();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南