前端代码
/** 导出按钮操作 */handleExport() {if (this.tableData.length == 0) {this.$message({message: '没有考生信息可导出',type: 'warning'})} else {this.$firm('确定导出所有统计信息吗?').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);portExcel(map, "表格文件名称", "表格文件名称", "export", response);}
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 = Property(OS_NAME);if (os != null && os.toLowerCase().startsWith(STR_WINDOWS)) {templateFileName = Class().getClassLoader().getResource("").getPath();// 获取resource目录下的模板位置templateFileName = templateFileName.substring(1);templateFileName = place("/", 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 = portExcel(params, paramsMap);//写文件流response.setCharacterEncoding(STR_UTF8);response.setHeader("content-Type", "application/vnd.ms-excel");response.setHeader(STR_CONTEND_DISPOSITION, STR_ATTACHMENT + de(goalName + ".xlsx", STR_UTF8));workbook.OutputStream());} catch (IOException e) {e.printStackTrace();//"按模板导出Excel数据异常:" + ereturn 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&le.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表可以以下:
本文发布于:2024-02-01 18:52:17,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170678473538730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |