jeecg导出Excel

1. 需要的js

/**
 * 弹出下载Excel窗口
 *
 */
function popResetVoucherCode(param) {
    var url = "***Controller.do?**functionOne";
    url += ("&param=" + param);
    $.dialog.setting.zIndex = getzIndex(true);
    if (typeof(windowapi) == 'undefined') {
        $.dialog({
            content: "url:" + url,
            zIndex: getzIndex(),
            lock: true,
            title: $.i18n.prop('dialog.reset.voucher.code.select'),
             700,
            height: 300,
            ok: function () {
                iframe = this.iframe.contentWindow;
                var saveUrl = "***Controller.do?**functionTwo";
                $("#formobj",iframe.document).attr('action',saveUrl);    //通过jquery为action属性赋值
                saveObj();
                return false;
            },
            okVal: '下载',
            cache: false,
            cancelVal: $.i18n.prop('dialog.close'),
            cancel:true /*为true等价于function(){}*/
        });
    } else {
        $.dialog({//使用W,即为使用顶级页面作为openner,造成打开的次级窗口获取不到关联的主窗口
            content: 'url:' + url,
            lock: true,
             width,
            zIndex: getzIndex(),
            height: height,
            parent: windowapi,
            title: title,
            opacity: 0.3,
            ok: function () {
                iframe = this.iframe.contentWindow;
                var saveUrl = "***Controller.do?**function";
                $("#formobj",iframe.document).attr('action',saveUrl);    //通过jquery为action属性赋值
                saveObj();
                return false;
            },
            okVal: '下载',
            cache: false,
            cancelVal: $.i18n.prop('dialog.close'),
            cancel: true /*为true等价于function(){}*/
        });
    }
}
/**
 * Jeecg Excel 导出
 * 代入查询条件
 *  url:        后台处理地址
 *   atagridId   需要刷新的datagrid的标识位
*/
function JeecgExcelExport(url,datagridId){  
    var queryParams = $('#'+datagridId).datagrid('options').queryParams;
    $('#'+datagridId+'tb').find('*').each(function() {
        queryParams[$(this).attr('name')] = $(this).val();
    });
    var params = '&';
    $.each(queryParams, function(key, val){
        params+='&'+key+'='+val;
    }); 
    var fields = '&field=';
    $.each($('#'+ datagridId).datagrid('options').columns[0], function(i, val){
        if(val.field != 'opt'){
            fields+=val.field+',';
        }
    });

    var id='&id=';
    $.each($('#'+ datagridId).datagrid('getSelections'), function(i, val){
        id+=val.id+",";
    });
    window.location.href = url+ encodeURI(fields+params+id);
}

// 表单提交,触发
$(document).ready(function() {
    //表单提交
    $("#formobj").Validform({
        btnSubmit : "#btn_sub",
        btnReset : "#btn_reset"
    });
});

  

2.jsp 操作下载Excel 的窗口

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/context/mytags.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <title>下载Excel</title>
    <link href="${webRoot}/plug-in/tools/css/metrole/common.css" rel="stylesheet" type="text/css">
    <t:base type="jquery,easyui,tools,DatePicker,validform"></t:base>
    <script src="${webRoot}/plug-in/lhgDialog/lhgdialog.min.js"></script>
    <script src="${webRoot}/plug-in/jquery-plugs/i18n/jquery.i18n.properties.js"></script>
    <script src="${webRoot}/plug-in/tools/curdtools.js"></script>
    <script src="${webRoot}/plug-in/easyui/extends/datagrid-groupview.js"></script>
    <link href="${webRoot}/plug-in/tools/css/metrole/common.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="${webRoot}/plug-in/bootstrap-select-1.13.2/dist/css/bootstrap-select.min.css">
    <script src="${webRoot}/plug-in/bootstrap-select-1.13.2/js/bootstrap-select.js"></script>
    <%@ include file="/webpage/common/select2.jsp" %>
    <script type="text/javascript">
        $(function () {});
    </script>
</head>
<body style="overflow-y: hidden" scroll="no">
<t:formvalid formid="formobj" refresh="true" dialog="true" action="***Controller.do?**functionTwo" layout="table">
    <br>
    <table cellpadding="0" cellspacing="1" class="formtable">
        <tbody>
        <tr>
            <td align="right" style=" 25%">
                <label class="Validform_label">主体:</label>
            </td>
            <td class="value" style=" 75%">
                <input type="text" style=" 90%" class="text conditionValue" value="${**Name}">
                <input name="id" type="hidden" value="${**Id}">
            </td>
        </tr>
        <tr>
            <td align="right">
                <label class="Validform_label">条件:</label>
            </td>
            <td class="value">
                <select name="entityName" id="entityName" style=" 90%;margin-right: 20px;padding-top: 0px;" class="input-sm useWith">
                    <c:forEach items="${**Map}" var="entry" varStatus="vs">
                        <option value="${entry.key}" > ${entry.value}</option>
                    </c:forEach>
                </select>
            </td>
        </tr>
        </tbody>
    </table>
</t:formvalid>
</body>
</html>

3.1 后台处理方式一:

@RequestMapping(params = "voucherDownBill")
    public String **FunctionTwo(HttpServletRequest request, HttpServletResponse response, ModelMap map) {
        AjaxJson resultJson = new AjaxJson();
        Class clazz = null;
        List list = new ArrayList();
        list.add(entity);
        String fileName = "导出文件标题";
        try {
            ExportParams params = new ExportParams(fileName + "列表", fileName, ExcelType.HSSF);
            Workbook workbook = ExcelExportUtil.exportExcel(params, clazz, list);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName + "列表" + dateFormat.format(new Date()) + "" + ".xls", "UTF-8"));// 组装附件名称和格式
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        }catch (IOException e){
            e.printStackTrace();
            logger.error("导出失败");
        }
        return NormalExcelConstants.JEECG_EXCEL_VIEW;
    }    

3.2 后台处理方式二:

@RequestMapping(params = "voucherDownBill")
    public String **FunctionTwo(HttpServletRequest request, HttpServletResponse response, ModelMap map) {
        AjaxJson resultJson = new AjaxJson();
        Class clazz = null;
        List list = new ArrayList();
        Entity entity = new Entity();
        entity.set(***);
        clazz = entity.getClass();
        list.add(entity);
        String fileName = "导出文件标题";
        map.put(NormalExcelConstants.FILE_NAME, fileName);
        map.put(NormalExcelConstants.CLASS, clazz);
        map.put(NormalExcelConstants.PARAMS, new ExportParams(fileName + "列表", "导出人:"+ ResourceUtil.getSessionUser().getRealName(), "导出信息"));
        map.put(NormalExcelConstants.DATA_LIST, list);

        return NormalExcelConstants.JEECG_EXCEL_VIEW;
    }        
原文地址:https://www.cnblogs.com/bridgestone29-08/p/11017617.html