# 后端代码

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
//Excel 生成代码 
// -------------------------------
// 发送到前端
ByteArrayOutputStream os = new ByteArrayOutputStream();
ServletOutputStream sout = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
InputStream is = null;
try {
wb.write(os);
byte[] content = os.toByteArray();
is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(excelName, "utf-8"));
response.setHeader("Content-Length", String.valueOf(is.available()));
response.setCharacterEncoding("UTF-8");
sout = response.getOutputStream();
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(sout);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
} finally {
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
if (sout != null) {
sout.close();
}
} catch (Exception e) {
}
}


# 前端下载

1
2
3

window.location.href="下载地址"

更新于 阅读次数