使用easyui的扩展进行页面上表格的导出Excel

可以导出为excel、pdf,可以进行打印

参考位置:http://www.jeasyui.net/extension/204.html
可在上述位置 下载文件 进行查看,其中引用的easyui的插件自行准备吧。
参考位置的导出为导出当前页面数据,所以进行行了修改,可以导出按照条件查找的数据,就是修改了数据源而已。

页面中添加上述下载的<Mydatagrid-export.js的引用:

<script src="~/Content/easyui1.7.0/Mydatagrid-export.js"></script>

 


 

一、导出excel

1、html:

<input id='btnExport' type='button' class='tools_btn' value='➲ 导 出' onclick="btn_export()" />

2、导出的方法:其中,controller和action、参数、导出文件名称可自行修改;

//导出
    function btn_export() {
        var myDate = new Date();
        var rows = GetAllDataByWhere("/ControllerName/GetGoodListByWhere", { param1: escape($('#XXX').val()), param2: escape($('#YYY').val()) });
        $('#MyGrid').datagrid('toExcel', { filename: '导出文件名称-' + myDate.getFullYear() + myDate.getMonth() + myDate.getDate() + '.xls', rows: rows })
    }

3、GetAllDataByWhere方法:使用的ajax请求获取数据,返回查询的结果。

//根据条件查询所有数据
function GetAllDataByWhere(url, param) {
    $.ajax({
        url: url,
        type: "post",
        data: param,
        dataType: "json",
        async: false,
        cache: false,
        success: function (data) {
            rows = data.rows;
        },
        error: function () {
            layer.msg("数据查询错误!");
            return;
        }
    });
    return rows;
}

 4、后台数据查询方法:有些内容可自行忽略,查询根据自己的条件进行查询

public ActionResult GetGoodListByWhere(string goodName, string classid)
        {
            Expression<Func<GoodDesctiption, bool>> where = c => true && c.deleteMark == 0;
            string goodname = Server.UrlDecode(goodName);
            if (!string.IsNullOrEmpty(goodname))
            {
                where = where.And(c => c.goodName.Contains(goodname));
            }
            if (!string.IsNullOrEmpty(classid))
            {
                //查询本产品分类以及本产品分类的子分类下的产品
                var list = GetGoodTypeListRecursion(classid);
                where = where.And(ExpressionExtensions.GetConditionExpression<GoodDesctiption>(list.ToArray(), "classid"));

                //where = where.And(c => c.classid == classid);
            }

            var temp = OperateContext.Current.BllContext.IGoodDesctiptionBll.GetList(where).ToList();
            var result = temp.Select(c => new
            {
                id = c.id,
                goodCode = c.goodCode,
                goodName = c.goodName,
                classid = c.classid,
                className = GetGoodClassName(c.classid),
                unit = c.unit,
                specification = c.specification,
                offer = c.offer,
                description = c.description
            });

            return Json(new DataGridModel() { rows = result});
        }

根据以上可以进行按照查询条件查询结果的导出。 


 

二、打印

直接根据上面的方法就可以了:

1、html:

<input id='btnPrint' type='button' class='tools_btn' value='📖 打 印' onclick="btn_print()" />

2、打印的方法:其中,controller和action、参数、导出文件名称可自行修改;

//打印
    function btn_print() {
        var rows = GetAllDataByWhere("/ControllerName/GetGoodListByWhere", { param1: escape($('#XXX').val()), param2: escape($('#YYY').val()) });
        $('#MyGrid').datagrid('print', { filename: 'DataGrid', rows: rows });
    }

3、4与上述一样


三、导出为PDF

未进行尝试,有意者可自行测试。具体参考方法在http://www.jeasyui.net/extension/204.html

 

posted @ 2019-05-24 14:19  1sa2sa  阅读(2404)  评论(0编辑  收藏  举报