ajax 上传参数 下载excel文件

这篇博客详细介绍了如何利用Ajax技术配合jsp和javascript处理上传参数,并通过控制器Java文件实现从服务器下载Excel文件的功能。文章中提到了依赖的下载工具J-Excel,可在GitHub上获取。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值