前端导出下载方法
// 导出
exporttable () {
const apiUrl = "http://localhost:9999/export/telDetails";
//console.log(this.form)
let param = new URLSearchParams();
param.append('startTime', this.addForm.startTime);
param.append('endTime', this.addForm.endTime);
this.$http.post(apiUrl, param, { responseType: 'blob' }).then((res) => {
//console.log(res.data)
const link = document.createElement('a')
let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
let num = ''
for (let i = 0; i < 10; i++) {
num += Math.ceil(Math.random() * 10)
}
link.setAttribute('download', '话务详情表_' + num + '.xlsx')
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
},
- data数据定义
data () {
return {
addForm: {
endTime: '',
landingNumber: '',
startTime: '',
trunkNumber: '',
called: '1',
extension: '',
direction: '',
callResult: '',
batchNumber: '',
userData: '',
seatNumber: '',
calling: '',
groupId: '',
ringingTime: ''
//trunkNumber:"2"
/*verifiedAccount:'3434' 2020-08-03 00:00:00*/
},
dialogAddgsVisible: false,
title: ''
}
},
后端导出下载代码
- service层代码,此处是调用其他service方法获取数据,header跟临时文件存放路径需要自行修改
package com.kaxin.appmanage.service.impl;
import com.kaxin.appmanage.pojo.TelDetails;
import com.kaxin.appmanage.service.ExportService;
import com.kaxin.appmanage.service.TelDetailsService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
@Service
public class ExportServiceImpl implements ExportService {
//创建临时文件存放的路径
private String temp = "c:\\temp\\excel\\";
@Autowired
private TelDetailsService telDetailsService;
@Override
public String exportStu(TelDetails telDetails) {
List<TelDetails> list = telDetailsService.conditionalQuery(telDetails);
//创建工作簿
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
//创建工作表
XSSFSheet sheet = xssfWorkbook.createSheet();
xssfWorkbook.setSheetName(0, "话务统计表");
//创建表头
XSSFRow head = sheet.createRow(0);
String[] heads = {"方向", "主叫号码", "被叫号码", "成功标志", "开始时间","结束时间","总时长","振铃时长","中继号","落地号"};
for (int i = 0; i < 10; i++) {
XSSFCell cell = head.createCell(i);
cell.setCellValue(heads[i]);
}
for (int i = 1; i <= list.size(); i++) {
TelDetails telDetails1 = list.get(i - 1);
//创建行,从第二行开始,所以for循环的i从1开始取
XSSFRow row = sheet.createRow(i);
//创建单元格,并填充数据
XSSFCell cell = row.createCell(0);
cell.setCellValue(telDetails1.getDirection());
cell = row.createCell(1);
cell.setCellValue(telDetails1.getCalling());
cell = row.createCell(2);
cell.setCellValue(telDetails1.getCalled());
cell = row.createCell(3);
cell.setCellValue(telDetails1.getSuccessSign());
cell = row.createCell(4);
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//String dateString = dateFormat.format(telDetails1.getStartTime());
cell.setCellValue(telDetails1.getStartTime());
cell = row.createCell(5);
// String dateString1 = dateFormat.format(telDetails1.getEndTime());
cell.setCellValue(telDetails1.getEndTime());
cell = row.createCell(6);
cell.setCellValue(telDetails1.getTotalTime());
cell = row.createCell(7);
cell.setCellValue(telDetails1.getRingingTime());
cell = row.createCell(8);
cell.setCellValue(telDetails1.getTrunkNumber());
cell = row.createCell(9);
cell.setCellValue(telDetails1.getLandingNumber());
}
//创建临时文件的目录
File file = new File(temp);
if (!file.exists()) {
file.mkdirs();
}
//临时文件路径/文件名
String downloadPath = file + "\\" + System.currentTimeMillis() + UUID.randomUUID();
OutputStream outputStream = null;
try {
//使用FileOutputStream将内存中的数据写到本地,生成临时文件
outputStream = new FileOutputStream(downloadPath);
xssfWorkbook.write(outputStream);
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return downloadPath;
}
}
- 设置样式需自行修改
- [样式设置可参考](https://blog.csdn.net/qq_41793064/article/details/89415205?biz_id=102&utm_term=%E5%88%A9%E7%94%A8poi%E8%AE%BE%E7%BD%AEexcel%E6%A0%B7%E5%BC%8F&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-89415205&spm=1018.2118.3001.4187)
controller层代码,文件名需自己设置
package com.kaxin.appmanage.controller;
import com.kaxin.appmanage.pojo.TelDetails;
import com.kaxin.appmanage.service.ExportService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@Controller
@CrossOrigin
@RequestMapping("/export")
public class ExportController {
@Resource
private ExportService exportService;
@RequestMapping("/telDetails")
public void exportStu(HttpServletResponse response, TelDetails telDetails) {
//设置默认的下载文件名
String name = "话务详情表.xlsx";
try {
//避免文件名中文乱码,将UTF8打散重组成ISO-8859-1编码方式 @RequestBody
name = new String(name.getBytes("UTF8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//设置响应头的类型
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//让浏览器下载文件,name是上述默认文件下载名
response.addHeader("Content-Disposition", "attachment;filename=\"" + name + "\"");
InputStream inputStream = null;
OutputStream outputStream = null;
//在service层中已经将数据存成了excel临时文件,并返回了临时文件的路径
String downloadPath = exportService.exportStu(telDetails);
//根据临时文件的路径创建File对象,FileInputStream读取时需要使用
File file = new File(downloadPath);
try {
//通过FileInputStream读临时文件,ServletOutputStream将临时文件写给浏览器
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
int len = -1;
byte[] b = new byte[1024];
while ((len = inputStream.read(b)) != -1) {
outputStream.write(b);
}
//刷新
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭输入输出流
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//最后才能,删除临时文件,如果流在使用临时文件,file.delete()是删除不了的
file.delete();
}
}
注意:谷歌浏览器是默认下载路径的,需要自己设置要自行修改,换其他浏览器会自动弹窗,用户可自选下载路径