一、get请求获取文件

1.后台代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
@ApiOperation(value = "生成拾物报表", notes = "生成拾物报表")
@GetMapping("/create/pick/reportForm")
public String createPickReportForm(
HttpServletResponse response,
@ApiParam(name = "type", value = "统计类型 1-年度报表 2-月度报表 3-季度报表", required = true)
@PathParam("type") Integer type,
@ApiParam(name = "reportFormType", value = "报表类型 1-领取比例 2-滞留时间", required = true)
@PathParam("reportFormType") Integer reportFormType,
@ApiParam(name = "year", value = "年份,如:2019")
@PathParam("year") String year,
@ApiParam(name = "month", value = "月份,如:2019-01")
@PathParam("month") String month,
@ApiParam(name = "quarter", value = "季度 1-春季(01,02,03) 2-夏季(04,05,06) 3-秋季(07,08,09) 4-冬季(10,11,12)")
@PathParam("quarter") Integer quarter) {
log.info("ReportFormRest.createPickReportForm()---生成拾物报表");

try {
String reportFormName = reportFormType==1?"拾物领取比例":"拾物滞留时间";
String fileName = "拾物领取报表.xls";
ReportFormExportParamVO param = new ReportFormExportParamVO();
param.setType(type);
if(type == 1) {
param.setYear(year);
fileName = year+"年"+reportFormName+"年度报表"+".xls";
}else if(type == 2) {
param.setMonth(month);
fileName = month.replace("-", "年")+"月"+reportFormName+"月度报表"+".xls";
}else if(type == 3) {
String[] strs = null;
String quarterName = null;
QuarterEnum[] quarterEnums = BusEnums.QuarterEnum.values();
for(QuarterEnum quarterEnum : quarterEnums) {
if(quarterEnum.getCode() == quarter) {
strs = quarterEnum.getQuarter();
quarterName = quarterEnum.getValue();
param.setQuarterName(quarterName);
break;
}
}
List<String> quarters = new ArrayList<>();
for(String str : strs) {
quarters.add(str);
}
param.setYear(year);
param.setQuarters(quarters);
fileName = year+"年"+quarterName+reportFormName+"季度报表"+".xls";
}

response.setContentType("application/force-download");
// 设置文件名
response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());

if(reportFormType != null && reportFormType == 1) {
reportFormService.exportPickReportForm(param, outputStream, false);
}else if(reportFormType != null && reportFormType == 2) {
reportFormService.exportRetentionTimePickReportForm(param, outputStream, false);
}

outputStream.flush();
outputStream.close();
} catch (Exception e) {
log.error("生成拾物报表出错。error={}", e);
return "{}";
}

return "{}";
}

2.页面代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
createReportForm() {
var _this = this;
let type = this.exportDialog.vo.type;
let reportFormType = this.exportDialog.vo.reportFormType;
let year = util.formatDate.format(this.exportDialog.vo.year, "yyyy");
let month = util.formatDate.format(this.exportDialog.vo.month, "yyyy-MM")
let quarter = this.exportDialog.vo.quarter;
let url = "/ws/sp/reportForm/create/pick/reportForm?"+"access-token="+this.$store.getters.auth_tokenId+"&reportFormType="+reportFormType+"&type="+type;
if(type == 1){
url = url + "&year=" + year;
}else if(type == 2){
url = url + "&month=" + month;
}else if(type == 3){
url = url + "&year=" + year + "&quarter=" + quarter;
}
this.downloadFile(url, function() {
_this.$Wmsg.error("生成台账失败");
});
},
downloadFile(url, callback) {
var haveIframe = $("iframe");
if (haveIframe) {
haveIframe.remove();
}
try {
var elemIF = document.createElement("iframe");
elemIF.src = url;
elemIF.style.display = "none";
document.body.appendChild(elemIF);
} catch (e) {
callback && callback(e);
}
}

二、post请求获取文件

1.后台代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@ApiOperation("导出迟到")
@PostMapping("/late")
public void exportLate(QueryParamVO queryParamVO, HttpServletResponse response) {
if (queryParamVO != null) {
String startDateStr = queryParamVO.getStartDateStr();
if (StrUtil.isNotBlank(startDateStr)) {
queryParamVO.setBeginTime(DateUtil.parse(startDateStr, "yyyyMMdd"));
}
String endDateStr = queryParamVO.getEndDateStr();
if (StrUtil.isNotBlank(endDateStr)) {
DateTime dateTime = DateUtil.parse(endDateStr, "yyyyMMdd");
queryParamVO.setEndTime(DateUtil.offsetDay(dateTime, 1));
}
String dayStr = queryParamVO.getDayStr();
if (StrUtil.isNotBlank(dayStr)) {
DateTime dateTime = DateUtil.parse(dayStr, "yyyyMMdd");
queryParamVO.setBeginTime(dateTime);
queryParamVO.setEndTime(DateUtil.offsetDay(dateTime, 1));
}
Integer week = queryParamVO.getWeek();
if (week != null && week > 0) {
DateTime beginTime = DateUtil.offsetWeek(
DateUtil.beginOfWeek(DateUtil.parse(FrameworkUtils.beginningOfTermDate(), "yyyy-MM-dd")),
week - 1);
queryParamVO.setBeginTime(beginTime);
Date endTime = DateUtil.offsetDay(beginTime, WEEK_LENGTH);
queryParamVO.setEndTime(endTime);
}
}

ServletOutputStream out = null;
String title = "迟到统计_" + DateUtil.format(DateUtil.date(), "yyyyMMdd");
String fileName = title + ".xlsx";
try {
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));// 设置文件名
out = response.getOutputStream();
weekService.exportLate(title, queryParamVO, out);
} catch (IOException e) {
logger.error("WeekRest.exportLate出错,error={}", e);
}
}

2.页面代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
doExport() {
let type = this.form.type;
let dayStr = null;
let week = null;
let month = null;
let startDateStr = null;
let endDateStr = null;
if (type == '天' && this.form.date) {
dayStr = util.formatDate.format(this.form.date, 'yyyyMMdd');
} else if (type == "周" && this.form.week) {
week = this.form.week
} else if (type == "月" && this.form.month) {
month = this.form.month;
startDateStr = util.formatDate.format(month, 'yyyyMMdd');
endDateStr = util.formatDate.format(new Date(month.getFullYear(), month.getMonth() + 1, 0), 'yyyyMMdd');
}
let deptUuid = this.form.deptUuid
let grade = deptUuid ? null : this.form.grade;

let params = {
dayStr,
week,
startDateStr,
endDateStr,
deptUuid,
grade
};
let url = '/ws/sp/week/late?access-token=' + this.tokenId;
this.postExcelFile(params, url);
},
postExcelFile(params, url) {
//params是post请求需要的参数,url是请求url地址
var form = document.createElement("form");
form.style.display = "none";
form.action = url;
form.method = "post";
document.body.appendChild(form);
// 动态创建input并给value赋值
for (var key in params) {
var input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = params[key];
form.appendChild(input);
}
form.submit();
form.remove();
},