前端代码
/**
*导出excel 用过layui的都看的懂
*/
function exports() {
layer.confirm('确定导出所有策略的信息?', {
btn: ['确定', '取消']
}, function(index){
//按钮【按钮一】的回调
location.href=prefix + '/exports';
layer.close(index);
}, function(index){
//按钮【按钮二】的回调
});
}
后端代码
@ExcelProperty是代表导出后这个属性对应的列名是什么 以及位置
package com.bootdo.cpm.domain;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import java.io.Serializable;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import lombok.Data;
/**
* cpm策略表
*
* @author hw
* @email [email protected]
* @date 2019-03-26 11:26:41
*/
@Data
public class CpmStrategyDO extends BaseRowModel implements Serializable {
private static final long serialVersionUID = 15353L;
//标识id
@Id
@GeneratedValue(generator = "JDBC")
@ExcelProperty(value = "策略id",index = 0)
private Integer cpmId;
//名称标题
@ExcelProperty(value = "名称标题",index = 1)
private String cpmTitle;
//广告链接
@ExcelProperty(value = "广告链接",index = 2)
private String cpmUrl;
//持续范围时间开始日
@ExcelProperty(value = "持续范围时间开始日",index = 3)
private Object cpmBegindate;
//持续范围时间结束日
@ExcelProperty(value = "持续范围时间结束日",index =4)
private Object cpmEnddate;
//单个用户接触的数量
@ExcelProperty(value = "单个用户接触的数量",index =5)
private Integer userNum;
//投放数量(千)
@ExcelProperty(value = "投放数量",index =6)
private Object cpmNum;
//当前总次数
@ExcelProperty(value = "当前总次数",index =7)
private Integer currentNum;
//cpm描述
@ExcelProperty(value = "描述",index =8)
private String cpmDetailed;
//cpm运行开始时间段(比如两点到四点的两点)
@ExcelProperty(value = "运行开始时间段",index =9)
private Object runBegindate;
//cpm运行结束时间段(比如两点到四点的四点)
@ExcelProperty(value = "运行结束时间段",index =10)
private Object runEnddate;
//拼接数据(用于返回给apk)
private String jsondata;
//优先级
private Integer priority;
//创建时间
private Object createDate;
//状态 0没运行,1运行
private Integer status;
//倒计时策略id
private Integer timeId;
}
Controller代码
@RequestMapping("/exports")
@ResponseBody
@RequiresPermissions("cpm:cpmStrategy:cpmUser")
public void exports(HttpServletRequest request, HttpServletResponse response) {
//需要导出的数据
List<CpmStrategyDO> list = cpmStrategyService.list(null);
try {
ServletOutputStream out = response.getOutputStream();
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
String fileName = new String("CPM策略信息表格"
.getBytes("utf-8"), "iso8859-1");
Sheet sheet1 = new Sheet(1, 0, CpmStrategyDO.class);
sheet1.setSheetName("sheet1");
response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx");
response.setContentType("multipart/form-data");
response.setCharacterEncoding("utf-8");
writer.write(list, sheet1);
writer.finish();
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}