excel下载|excel下载 前端:vue 后台:java
好久没写东西了,项目中要用到前端下载excel,我们的前端选择的是VUE,后端选择的是java,我就写一下实现吧,方便后来人少走一些弯路。
java后台实现代码
@ApiOperation(value = "https://www.it610.com/article/导出日志",notes = "导出列表")
@RequestMapping(value = "https://www.it610.com/exportLogs",method = RequestMethod.GET)
@ResponseBody
public ApiResult> exportExcel(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
if (request ==null||response == null){
return ApiResult.fail(ErrorCode.DATA_OP_EXCEPTION, "数据库操作异常!");
}
try {
String startTime = request.getParameter("startLong");
String endTime = request.getParameter("endLong");
String serialNum = request.getParameter("serialNum");
String type = request.getParameter("type");
String operator = request.getParameter("operator");
LogBean logBean = new LogBean();
logBean.setPageNumber(1);
logBean.setPageSize(Integer.MAX_VALUE);
if (startTime != null && !startTime.equals("")) {
logBean.setStartTime(new Date(Long.valueOf(startTime)));
}
if (endTime != null && !endTime.equals("")) {
logBean.setEndTime(new Date(Long.valueOf(endTime)));
}
if (serialNum !=null&&!serialNum.equals("")){
logBean.setSerialNum(serialNum);
}
if (type !=null&&!type.equals("")){
logBean.setType(Integer.valueOf(type));
}
if (operator !=null&&!operator.equals("")){
logBean.setOperator(operator);
}
List list = logService.getLogList(logBean);
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
String fileName = simpleDateFormat.format(date).toString()+".xls";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.addHeader("Content-Disposition", "attachment");
response.addHeader("filename",fileName);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
sheet.setColumnWidth(0, 4000);
//设置列宽
sheet.setColumnWidth(1,4000);
sheet.setColumnWidth(2,4000);
sheet.setColumnWidth(3,4000);
sheet.setColumnWidth(4,4000);
sheet.setColumnWidth(5,8000);
sheet.setDefaultRowHeight((short) (2 * 256));
//设置行高
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("时间");
cell = row.createCell(1);
cell.setCellValue("操作人");
cell = row.createCell(2);
cell.setCellValue("业务类型");
cell = row.createCell(3);
cell.setCellValue("序列号");
cell = row.createCell(4);
cell.setCellValue("IP");
cell = row.createCell(5);
cell.setCellValue("操作内容");
HSSFRow rows;
HSSFCell cell1s;
LogTypeEnum[] values = LogTypeEnum.values();
Map resultMap = new HashMap();
for (LogTypeEnum logTypeEnum : values) {
resultMap.put(logTypeEnum.getCode(), logTypeEnum.getMsg());
}
for (int i=0;
i
【excel下载|excel下载 前端:vue 后台:java】前台代码实现
async exportExcel() {
let self = this;
this.queryParam.startLong = this.timeRender(
this.queryParam.startTimeTemp
);
this.queryParam.endLong = this.timeRender(this.queryParam.endTimeTemp);
let downUrl = "exportLogs?" + $.param(self.queryParam);
axios({
method: "get",
url: downUrl,
responseType: "blob"
}).then(data => {
if (data && data.headers && data.headers.filename) {
let url = window.URL.createObjectURL(data.data);
let link = document.createElement("a");
link.style.display = "none";
link.href = https://www.it610.com/article/url;
link.setAttribute("download", decodeURI(data.headers.filename));
document.body.appendChild(link);
link.click();
} else {
(this.$message || {}).warning('error');
}
});
},
推荐阅读
- Jsr303做前端数据校验
- 《机器学习实战》高清中文版PDF英文版PDF+源代码下载
- 百度云极速下载,来体验飞的感觉,还可以看最新动漫的百度云视频哦
- 7、前端--jQuery简介、基本选择器、基本筛选器、属性选择器、表单选择器、筛选器方法、节点操作、绑定事件
- ExcelPackage读取写入
- 前端代码|前端代码 返回顶部 backToTop
- Excel|Excel 2013 新增功能之瞬间填充整列数据!
- 狗狗定点大小便视频教程下载地址
- Spring|Spring Cloud Feign实现文件上传下载的示例代码
- MagicaVoxel-0.99.6-macos-10.7|MagicaVoxel-0.99.6-macos-10.7 网盘下载