java做excel导出

1、idea (pom.xml)
<dependency>
    <groupId>org.apache.poi</groupId>
     <artifactId>poi-scratchpad</artifactId>
     <version>3.17</version>
 </dependency>
2、应用实例
 @RequestMapping(value = "/exportExcel", method = RequestMethod.POST)
    public final void exportExcel(HttpServletResponse response,String ids) {
        try{
            //创建HSSFWorkbook对象(excel的文档对象)
            HSSFWorkbook wb = new HSSFWorkbook();
            //建立新的sheet对象(excel的表单)
            HSSFSheet sheet=wb.createSheet("授权码");
            //在sheet里创建行
            HSSFRow row1=sheet.createRow(0);
            //创建单元格并设置单元格内容
            row1.createCell(0).setCellValue("授权码");
            row1.createCell(1).setCellValue("专业");
            //ids来自于前台,在这里生成的数据为动态查出来的。
            String[] split = ids.split(",");
            for(int i=0;i<split.length;i++){
                String busName="";
                String authCode = authCodeService.get(split[i]).getAuthCode();
                //获得领域名称
                AuthBusinessQuery query=new AuthBusinessQuery();
                query.setAuthId(split[i]);
                List<AuthBusiness> authBusinesses = authBusinessService.find(query);
                if(authBusinesses.size()>0){
                    for(AuthBusiness abs:authBusinesses){
                        busName+=businessService.get(abs.getBusinessId()).getName()+" ";
                    }
                }else{
                    busName+="无";
                }
                //批量生成行
                HSSFRow row3=sheet.createRow(i+1);
                row3.createCell(0).setCellValue(authCode);
                row3.createCell(1).setCellValue(busName);
                /*row3.createCell(0).setCellValue("hhhhh");
                row3.createCell(1).setCellValue("hshshs");*/

            }
            //输出Excel文件
            OutputStream output=response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition", "attachment; filename=authCode.xls");
            response.setContentType("application/msexcel");
            wb.write(output);//默认生成在本地浏览器下载的地方
            output.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
3、在浏览器上的请求和ajax发送的get请求(不同)

问题:浏览器中请求可以生成xls,但是ajax却不行(所以采用了模拟form表单的方法)

/**
 * form表单提交
 * 参数:
 * url: url
 * target: 打开方式  默认为'_self'  '_self'当前窗口打开  '_blank'新窗口打开
 * method: 提交方式  默认为'post'
 * param: 参数  格式为 {'id':'123','name','xiaowanwan'}
 */
function formAsk(record) {
    //创建form表单
    var temp_form = document.createElement("form");
    temp_form.action = record.url;
    //如需打开新窗口,form的target属性要设置为'_blank'
    temp_form.target = record.target != null ? record.target : "_self";
    temp_form.method = record.method != null ? record.method : "post";
    temp_form.style.display = "none";
    //添加参数
    var param = record.param;
    for (var item in param) {
        var opt = document.createElement("textarea");
        opt.name = item;
        opt.value = param[item];
        temp_form.appendChild(opt);
    }
    document.body.appendChild(temp_form);
    //提交数据
    temp_form.submit();
}

jsp中的应用

 formAsk({
      url: 'exportExcel',
        target: '_blank',
        method: 'post',//url路径的后台采用的是post
        param:{ids:ids}//后台所需要的参数
    });

参考链接:https://www.cnblogs.com/hanfeihanfei/p/7079210.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值