前端Vue+后端java,根据模板导出Excel

java接口输出文件流

String filename = "";    //文件名称
InputStream in = null;		//输入流
XSSFWorkbook workBook = null; //HSSFWorkbook读取97-2003格式 ,XSSFWorkbook读取2007-2013格式		
OutputStream out = null;   //输出流
//获取服务器Excel模板
String templatePath = request.getServletContext().getRealPath("/")+"export/template/指令统计详情模板.xlsx";

//重命名文件名
if(StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
    filename = "指令统计详情" + startTime + "-" + endTime+".xlsx";
}else {
	filename = "指令统计详情" + System.currentTimeMillis() + ".xlsx";
}
		
// 读取Excel文档
File finalXlsxFile = new File(templatePath);
in = new FileInputStream(finalXlsxFile);
workBook = new XSSFWorkbook(in);
//样式1
XSSFCellStyle cellStyle = workBook.createCellStyle();
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 居中
//样式2
XSSFCellStyle cellStyle1 = workBook.createCellStyle();
cellStyle1.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 居中
cellStyle1.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
//样式3
XSSFCellStyle cellStyle2 = workBook.createCellStyle();
cellStyle2.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 居中
cellStyle2.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
cellStyle2.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
XSSFFont font1 = workBook.createFont();
font1.setFontHeightInPoints((short) 10);
font1.setFontName("微软雅黑");
cellStyle2.setFont(font1);//字体	

// sheet 对应一个工作页
Sheet sheet = workBook.getSheetAt(0);
//从第三行开始
Row row = sheet.createRow(2);
//第一列
Cell cell = row.createCell(0);	
//循环数据,datalist为数据list
for (int j = 0; j < dataList.size(); j++) {
    // 创建一行:从第二行开始,跳过属性列
    row = sheet.createRow(2+j);
    //创建第一列,为序号列
    cell = row.createCell(0);
	cell.setCellStyle(cellStyle);
	cell.setCellValue(j+1);	
    //创建第二列
    cell = row.createCell(1);
	cell.setCellStyle(cellStyle);
	cell.setCellValue(map.get("superviseName"));
......
}
String fileName = filename;
fileName = URLEncoder.encode(fileName, "utf-8");
//获取输出流
OutputStream outputStream = response.getOutputStream();
response.reset();
//设置Http响应头告诉浏览器下载这个附件
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.setContentType("application/octet-stream; charset=utf-8");
workBook.write(outputStream);
outputStream.close();   //关闭输出流
outputStream.flush();	//清空输出流				
			

Vue下载文件

/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downExcelFile(url, parameter) {
  return request({
    url: url,
    data: parameter,
    method: 'post',
    responseType: 'blob'
  })
}

downExcelFile(this.url.down, this.queryData)
        .then(res => {
//获取文件名称
          const filename = decodeURI(res.headers['content-disposition'].split(';')[1].split('=')[1]) || '.xls'
          const blob = new Blob([res.data], {
            type: 'application/octet-stream'
          })
          const url = window.URL.createObjectURL(blob)
          const link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', filename)
          document.body.appendChild(link)
          link.click()
        }
        )

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值