jsp代码
<a href="javascript(0)" class="easyui-linkbutton" iconCls="icon-drive_web" plain="true" onclick="downloadCert()">导出</a>
js代码
/**
* 下载
*/
function downloadCert(){
var ids = [];
var rows = $('#certificateGrid').datagrid('getSelections');
url = GLOBAL_CONTEXT_PATH + "/certificates/download";
if(rows.length!=0){
for(var i=0; i<rows.length; i++){
ids.push(rows[i].frameNo);
}
var frameNos = ids.join("-");
var param = {"vNum" : frameNos};
//window.open(url);
// Ajax 文件下载
jQuery.download = function(url, data, method){
// 获取url和data
if( url && data ){
// data 是 string 或者 array/object
data = typeof data == 'string' ? data : jQuery.param(data);
// 把参数组装成 form的 input
var inputs = '';
jQuery.each(data.split('&'), function(){
var pair = this.split('=');
inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />';
});
// request发送请求
jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
.appendTo('body').submit().remove();
};
};
if(checkdate()){
confirm("您确定要下载?", function(data){
$.download(url,param,'get' );
if(rows.length==0){
searchCert();
}
});
}
}else{
if(checkdate()){
confirm("您确定要下载?", function(data){
$("#searchCertificate").submit();
});
}
}
}
controller java 文件
@RequestMapping(value = "/certificates/downloadAll" , method = {RequestMethod.GET,RequestMethod.POST})
public void excelDownloadAll(
HttpServletRequest request, HttpServletResponse response,
@RequestParam(required = false) String frameNo,
@RequestParam(required = false) Date startDate,
@RequestParam(required = false) Date endDate
) throws Exception{
String[] fields = {"frameNo","curbWeight","engineNum","fuelType","innerSize","maximumSpeed","overallSize",
"ratedContainedMass","totalAccuracyWeight","totalMass","carBrand","productionDate"};
List<CertificateVo> certVoList = new ArrayList<CertificateVo>();
List<LocalCertificate> cert = null;
cert = localCertificateService.downAllCert(frameNo, startDate, endDate);
excel(cert,certVoList,cert.size());
ExcelUtil.exportExcel(response, "合格证", certVoList, fields, "合格证");
WebUtil.download(response, "合格证"+DateUtil.getDate(new Date()));
}
注意:此处应用到了下载工具。工具地址是:https://github.com/hyberbin/J-Excel