前端代码
/** 导出按钮操作 */
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);
}
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表可以以下: