vue端:
首先,页面导出按钮
<el-button size="mini" type="primary" :disabled="!buttons.withdraw_btn_uoload" @click="exportExcel" style="float:right;">导出</el-button>
新增接口:
export function exportWithdrawExcelAPI(params) {
return request({
url: '/order/XXXXXXXXXXX',
method: 'GET',
responseType: 'blob',
params
})
}
引入接口:
import { exportWithdrawExcelAPI} from "@/api/order";
点击事件,写在method里面:
// 导出
exportExcel(){
exportWithdrawExcelAPI(this.formInline).then(res => {
if(res.size > 0){
const content = res
const blob = new Blob([content])
const fileName = '提现审核表.xlsx'
if ('download' in document.createElement('a')) { // 非IE下载
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
} else { // IE10+下载
navigator.msSaveBlob(blob, fileName)
}
/* this.$message.success("导出成功")*/
}
}).catch(() => {
});
},
JAVA端:
maven 依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>RELEASE</version>
</dependency>
工具类
public class ExportWithdrawExcelUtil {
//创建表头
public void createTitle(HSSFWorkbook workbook, HSSFSheet sheet){
HSSFRow row = sheet.createRow(0);
//设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
sheet.setColumnWidth(1,12*256);
sheet.setColumnWidth(3,17*256);
//设置为居中加粗
HSSFCellStyle style = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setBold(true);
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setFont(font);
HSSFCell cell;
cell = row.createCell(0);
cell.setCellValue("申请时间");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("吃饭了没");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("吃饱了没");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("真实姓名");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("亚麻得");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("臣妾做不到啊");
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("打豆豆");
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue("睡觉觉");
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue("吃饭饭");
cell.setCellStyle(style);
}
//生成excel文件
public void buildExcelFile(String filename,HSSFWorkbook workbook) throws Exception{
FileOutputStream fos = new FileOutputStream(filename);
workbook.write(fos);
fos.flush();
fos.close();
}
//浏览器下载excel
public void buildExcelDocument(String filename, HSSFWorkbook workbook, HttpServletResponse response) throws Exception{
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(filename, "utf-8"));
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
}
}
controller方法
@ApiOperation(value = "导出提现excel")
@RequestMapping(value = "/exportWithdrawExcel", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
public Object exportWithdrawExcel(HttpServletResponse response,
@RequestParam(value = "memberNo", required = false) String memberNo,
@RequestParam(value = "status",defaultValue = "1",required = true) Integer status,
@RequestParam(value = "startTime",required = false) Long startTime,
@RequestParam(value = "endTime",required = false) Long endTime,
@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) throws Exception{
ExportWithdrawExcelUtil exportexcelUtil = new ExportWithdrawExcelUtil();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("你管我什么婊");
exportexcelUtil.createTitle(workbook,sheet);
if (memberNo!=null&&memberNo.equalsIgnoreCase("")){
memberNo=null;
}
List<CoolMan> list = User.userGetMotherList(status, startTime, endTime, memberNo);
//设置日期格式
HSSFCellStyle style = workbook.createCellStyle();
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
//新增数据行,并且设置单元格数据
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
int rowNum=1;
for(OrderCashListObj orderObj:list){
HSSFRow row = sheet.createRow(rowNum);
if(!StringUtils.isEmpty(orderObj.getXXXX())){
row.createCell(0).setXXXX(sdf.format(orderObj.getXXXX()));
}
if(!StringUtils.isEmpty(orderObj.getXXXX())){
row.createCell(1).setCellValue(orderObj.getXXXX());
}
if(!StringUtils.isEmpty(orderObj.getXXXX())){
row.createCell(2).setCellValue(orderObj.getXXXX());
}
if(!StringUtils.isEmpty(orderObj.getXXXX())){
row.createCell(3).setCellValue(orderObj.getXXXX());
}
if(!StringUtils.isEmpty(orderObj.getXXXX())){
row.createCell(4).setCellValue(orderObj.getXXXX());
}
if(!StringUtils.isEmpty(orderObj.getXXXX())){
row.createCell(5).setCellValue(orderObj.getXXXX().toString());
}
if(!StringUtils.isEmpty(orderObj.getCashXXXX())){
row.createCell(6).setCellValue(orderObj.getXXXX());
}
if(!StringUtils.isEmpty(orderObj.getXXXX())){
row.createCell(7).setCellValue(orderObj.getXXXX().toString());
}
if(!StringUtils.isEmpty(orderObj.getXXXX())){
row.createCell(8).setCellValue(orderObj.getXXXX());
}
rowNum++;
}
String fileName = "你管我什么表.xls";
//浏览器下载excel
exportexcelUtil.buildExcelDocument(fileName,workbook,response);
return new CommonResult().success("导出成功");
}