Java数据Exal表格导出数据

Java数据Exal表格导出数据实际处理类 传入 list 对象

大家好,欢迎来到IT知识分享网。

前端代码

 / 导出按钮操作 */ handleExport() { if (this.tableData.length == 0) { this.$message({ message: '没有考生信息可导出', type: 'warning' }) } else { this.$modal.confirm('确定导出所有统计信息吗?').then(() => { let fileName = '需要导出名称.xlsx' this.download('数据路径', this.form, fileName) //例: // this.download('/analyse/planCycle/export', this.form, fileName) }) } },

后端代码:

实际处理类:传入list对象

 / * 导出考生信息列表 */ @PostMapping("/export") public void export(HttpServletResponse response, StaPlanCycle staPlanCycle) throws IOException { int index = 0; // 查询需要导出的数据: List<StaPlanCycle> list = staPlanCycleService.selectStaPlanCycleList(staPlanCycle); AnalyseFileUtil analyseFileUtil = new AnalyseFileUtil(); Map<String, Object> map = new HashMap(); for (StaPlanCycle planCycle : list) { planCycle.setIndex(++index); } map.put("mapList", list); analyseFileUtil.exportExcel(map, "表格文件名称", "表格文件名称", "export", response); }

Java数据Exal表格导出数据

public class AnalyseFileUtil { public String srcDir = "/zip/"; private static final int BUFFER_SIZE = 2 * 1024; private static final String OS_NAME="os.name"; private static final String STR_WINDOWS="windows"; private static final String STR_TEMPLATES="templates"; private static final String STR_CONTEND_DISPOSITION="Content-Disposition"; private static final String STR_ATTACHMENT="attachment;filename="; private static final String STR_UTF8="UTF-8"; private String filePath = "/"; / * 按模板导出Excel数据(支持文件流读取文件) * * @param paramsMap 数据 * @param fileName 模板名称 * @param goalName 文件名称 * @param fileTemplateUrl 模板地址 * @return 文件存储地址 * @throws Exception */ public String exportExcel(Map<String, Object> paramsMap, String fileName, String goalName, String fileTemplateUrl, HttpServletResponse response) { String templateFileName = ""; try { String os = System.getProperty(OS_NAME); if (os != null && os.toLowerCase().startsWith(STR_WINDOWS)) { templateFileName = this.getClass().getClassLoader().getResource("").getPath(); // 获取resource目录下的模板位置 templateFileName = templateFileName.substring(1); templateFileName = templateFileName.replace("/", File.separator); } else { templateFileName = filePath; } templateFileName = templateFileName + STR_TEMPLATES + File.separator + fileTemplateUrl + File.separator + fileName + ".xlsx"; // 第二个参数true是为了开启多sheet扫描,就是同一个xls文件中有多个工作表的时候。 TemplateExportParams params = new TemplateExportParams(templateFileName, true); // 数据载入,生成excel文件 Workbook workbook = ExcelExportUtil.exportExcel(params, paramsMap); //写文件流 response.setCharacterEncoding(STR_UTF8); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader(STR_CONTEND_DISPOSITION, STR_ATTACHMENT + URLEncoder.encode(goalName + ".xlsx", STR_UTF8)); workbook.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); //"按模板导出Excel数据异常:" + e return null; } return goalName; } }

pom文件(如果不引可能会报错):

 <!-- poi-tl --> <dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.10.0</version> </dependency> <!-- easy-poi --> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1-jre</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> </dependencies>

如果要指定字段到Exal表可以以下:

Java数据Exal表格导出数据

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/139152.html

(0)
上一篇 2025-06-07 21:33
下一篇 2025-06-07 21:45

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信