数据导出Excel表格

    public String exportInfoFr(String path,String name,String startdate,String enddate,SysUser user){
        List<Map<String, Object>> list = this.esEntPermitErrDao.findListObjectBySql("select 字段值1,字段值2,字段值3,字段值4,字段值5 from 表名 where 字段值2 in ('0','2')");
        if (list.size()>0 && list != null ) {
            try {
                List<Map<String,Object>> redBlackMsg = null;
                List<Map<String,Object>> temp = new ArrayList<>();
                WritableWorkbook wwb = null;
                wwb = Workbook.createWorkbook(new java.io.File(path));
                int size = 1;
                WritableSheet wst = wwb.createSheet("sheet1", 0);
                Label label2 = new Label(0, 0, "类别");
                Label label3 = new Label(1, 0, "名称");
                Label label4 = new Label(2, 0, "社会");
                Label label5 = new Label(3, 0, "注册号");
                Label label6 = new Label(4, 0, "组织");
                Label label7 = new Label(5, 0, "登记号");
                Label label8 = new Label(6, 0, "单位");
                
                wst.addCell(label2);
                wst.addCell(label3);
                wst.addCell(label4);
                wst.addCell(label5);
                wst.addCell(label6);
                wst.addCell(label7);
                wst.addCell(label8);
                
                for (int i = 0; i < list.size(); i++) {
                    Map map = (Map) list.get(i);
                    wst.addCell(new Label(0,i+1,(String)map.get("字段值1")));
                    wst.addCell(new Label(1,i+1,(String)map.get("字段值2")));
                    wst.addCell(new Label(2,i+1,(String)map.get("字段值3")));
                    if (map.get("字段值4")!=null && !map.get("字段值4").equals("")) {
                        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        wst.addCell(new Label(3,i+1,sdf.format((Date)map.get("字段值4"))));
                    }
                    wst.addCell(new Label(4,i+1,(String)map.get("字段值5")));
                }
                 wwb.write();
                 wwb.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return name;
        }else {
            return "1";
        }
    }

导入的包

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

action中

    /**创建excle*/
    public String creatExcle(String tooldataid){
        FileUtil fileUtil = new FileUtil();
        String path=this.getRequest().getRealPath("/")+"/upload/downxls/download/"+tooldataid+".xls";
        fileUtil.createFile(path);
        return path;
    }
    /**导出筛查信息*/
    public void exportScreenInfoFr(){
        String name = String.valueOf(System.currentTimeMillis());
        String startdate = this.getParameterToPage("startdate");
        String enddate = this.getParameterToPage("enddate");
        if (!startdate.equals("") && !enddate.equals("")) {
            startdate += " 00:00:00";
            enddate += " 23:59:59";
        }
        SysUser user = this.sysUserManager.getById(this.getCurrentSessionUser().getUserId());
        this.ajaxText(esEntPermitErrManager.exportInfoFr(creatExcle(name),name,startdate,enddate,user));
    }

jsp页面

<a href="#" onclick="exportInfoFr()" class="btn">导出</a>
   //导出信息
    function exportInfoFr(){
        var startdate = $("input[name='startdate']").val();
        var enddate = $("input[name='enddate']").val();
           $.ajax({
            url : '${root}/df/PermErr/exportScreenInfoFr.do',
            cache : false, 
            async : true,
            type : "POST",
            data :{startdate:startdate,enddate:enddate},
            dataType:"text",
            success : function(data) {
                if(data=="1"){
                    errorTip("没有筛查数据!");
                }else{
                    window.location='${root}/servlet/dfile?fileName='+data+'.xls&path=upload/downxls/download';
                }
            }
        });  
    }
原文地址:https://www.cnblogs.com/yanchaohui/p/10736152.html