1、绑定事件,参数为模糊查询中使用ng-model绑定的数据
<a class="button_type1 addTabPage" title="导出"
ng-click="exportDatas(searchWhere)"><i class="iconfont"></i>导出</a>
2、使用$scope.参数 = {键:值,键:值,键:值···}
$scope.searchWhere = {
name : “”,
loginName : “”,
passWord : “”,
teamName : “”,
schoolName : “”,
dyfield5 : “”,
loginName : “”,
projectType : “”
};
3、
// 导出excel 全部 exportHrSalaryZZ
$scope.exportDatas = function(searchWhere) {
$http(
{
url : Constants.API.INTERFACE_URL
+ "a/xf4g/xfScore/exportXfScoreData",
method : "POST",
data : {
examName:searchWhere.examName,//绑定参数
projectType:searchWhere.projectType,
teamName:searchWhere.teamName
},
headers : {//定义类型
'Content-type' : 'application/json'
},//响应类型
responseType : 'arraybuffer'
})
.success(
function(data, status, headers,
config) {
var blob = new Blob(
[ data ],
{
type : "application/vnd.ms-excel"
});
var objectUrl = URL
.createObjectURL(blob);
var a = document
.createElement('a');
document.body.appendChild(a);
a.setAttribute('style',
'display:none');
a.setAttribute('href',
objectUrl);
var filename = "5G技术及应用大赛评分汇总表.xlsx";//导出的文件名
a.setAttribute('download',//绑定属性
filename);
a.click();
URL.revokeObjectURL(objectUrl);
}).error(
function(data, status, headers,
config) {
});
}
3.1
@CrossOrigin(origins = “*”, maxAge = 3600)
@RequestMapping(value = “exportXfUserData”, method = RequestMethod.POST)
@ResponseBody
public String exportXfUserData(@RequestBody XfUser xfUser, HttpServletRequest request, HttpServletResponse response,
RedirectAttributes redirectAttributes) throws IOException {
Page page = new Page();
// 设置导出的名字
String fileName = “5G技术及应用大赛用户汇总表” + DateUtils.getDate(“yyyyMMddHHmmss”) + “.xlsx”;
// 合同管理列表信息
List list = xfUserService.exportXfUserData(xfUser);
page.setList(list);
new ExportExcel(“5G技术及应用大赛用户汇总表”, XfUser.class).setDataList(page.getList()).write(response, fileName).dispose();
return “5G技术及应用大赛用户汇总表!”;
}
4、写SQL查询语句
5.在实体类中需要写入excel的字段的get方法上添加注解@ExcelField(title = “字段标题”, align = 2, sort = 列号)
String title():导出字段标题(需要添加批注请用“”分隔,标题批注,仅对导出模板有效)
String value() default “”:默认调用当前字段的“get”方法,如指定导出字段为对象,请填写“对象名.对象属性”,例:“area.name”、“office.name”
int type() default 0:字段类型:0:导出导入;1:仅导出;2:仅导入
int align() default 0;:字段对齐方式:0:自动;1:靠左;2:居中;3:靠右
int sort() default 0:导出字段字段排序(升序)
String dictType() default “”:如果是字典类型,请设置字典的type值
Class<?> fieldType() default Class.class:反射类型
int[] groups() default {}:字段归属组(根据分组导出导入)
List<ZbrouterPortChange> queryList = zbRouterChangeService.exportExcel(zbrouterPortChangeModel);
if(queryList == null){
queryList = new ArrayList<ZbrouterPortChange>();
}
//标题
String[] headers = new String[] {
"主用路由器IP","主用路由器名称","主用端口号","主用物理状态",
"主用协议状态","主用端口索引","主用倒换","备用路由器IP"
,"备用路由器名称","备用端口号", "备用物理状态","备用协议状态","备用端口索引",
"备用倒换","VPN","接口IP","接口掩码","采集时间","倒换状态"};
// 设定每一列列宽
short[] widths = new short[] {25,25,25,25,25, 25,25,25,25,25, 25,25,25,25,25, 25,25,25,25};
// 按自定义顺序导出不再采用反射获取类中声明的field
String[] fields = new String[] {"mainRouterIp","mainRouterName","mainRouterPort","mainPhyState","mainProtocolState",
"mainPortIndex","VPN","interfaceIp","interfaceMask",
"slaveRouterIp","slaveRouterName","slaveRouterPort","slavePhyState","slaveProtocolState",
"slavePortIndex","collectionTime","shiftState", "mainShift", "slaveShift"};
String title = "主备路由器端口倒换";
try {
ExportExcel.export(queryList, headers, fields, widths,
title, "", false);
} catch (IOException e) {
e.printStackTrace();
}