码迷,mamicode.com
首页 > 其他好文 > 详细

【第十四篇】easyui datagrid导出excel

时间:2015-10-09 15:06:30      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

<a class="btn btn-app" onclick="exportExcel()"><i class="fa fa-edit"></i>导出Excel</a>

贴下面的代码之前,我想说一下

我的数据是主外键关系,有多张表关联,所以在做数据的时候,发现很多问题,读取的时候,它会自动读取所有的数据。

当然这不能满足我导出之后的要求,我只需要导出我要的字段即可,所以我进行了数据处理。

 //导出Excel
    function exportExcel() {
        var rows = $("#saleGrid").datagrid("getRows");

        for (var i = 0; i < rows.length; i++) {    //进行数据处理
            if (isArray(rows[i].OrganizedId)) {
                rows[i].OrganizedId = rows[i].OrganizedId[0];
            }
            if (isArray(rows[i].CustomerId)) {
                rows[i].CustomerId = rows[i].CustomerId[0];
            }
            if (rows[i].AdvanceDate != null) {
                var unix = rows[i].AdvanceDate.replace("/Date(", "").replace(")/", "");
                var un = unix.substring(0, 10);
                var newDate = new Date();
                newDate.setTime(un * 1000);

                rows[i].AdvanceDate = newDate.toLocaleString();
            }
            if (rows[i].OrderDate != null) {
                var unix = rows[i].OrderDate.replace("/Date(", "").replace(")/", "");
                var un = unix.substring(0, 10);
                var newDate = new Date();
                newDate.setTime(un * 1000);

                rows[i].OrderDate = newDate.toLocaleString();
            }
            if (rows[i].RetainageDate != null) {
                var unix = rows[i].RetainageDate.replace("/Date(", "").replace(")/", "");
                var un = unix.substring(0, 10);
                var newDate = new Date();
                newDate.setTime(un * 1000);

                rows[i].RetainageDate = newDate.toLocaleString();
            }

            //移除不要的字段
            delete rows[i].SaleAtts;
            delete rows[i].SaleOrderId;
            delete rows[i].SaleOrderItems;
            delete rows[i].SaleStatus;
            delete rows[i].UserName;
            delete rows[i].Customer;
            delete rows[i].AddDate;

        }
        var bodyData = JSON.stringify(rows);  //转成json字符串

        //替换中文标题
        var a = bodyData.replace(/SaleOrderNo/g, "订单编号").replace(/OrderType/g, "订单类型").replace(/FromWhere/g, "订单来源")
       .replace(/OrganizedId/g, "机构").replace(/SaleUser/g, "销售员").replace(/SaleTc/g, "销售提成").replace(/OrderDate/g, ‘订单日期‘)
       .replace(/ContractNo/g, "合同编号").replace(/Amount/g, "总额").replace(/Advance/g, "首付款").replace(/AdvanceDate/g, "首付款日期")
       .replace(/PayMethod/g, "支付方式").replace(/Retainage/g, "尾款").replace(/RetainageDate/g, "尾款日期").replace(/InlayPrice/g, "镶嵌款")
       .replace(/CustManager/g, "客户经理").replace(/EquityNo/g, "认股书编号").replace(/LogisticsTotal/g, "物流费用")
       .replace(/Remarks/g, "备注").replace(/CompletedStatus/g, "状态").replace(/CustomerId/g, "终端客户");

        var postData = {
            data: a
        };

        $.ajax({
            type: "POST",
            url: "ExportExcel",
            data: postData,
            success: function (data) {
                if (data == "1") {
                    layer.msg("操作成功,文件在桌面!", {
                        icon: 6,
                        time: 2000,
                    });
                } else if (data == "-1") {
                    layer.msg("操作失败!", { icon: 2 });
                }
            }
        });
    }
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public ActionResult ExportExcel()
        {
            string json = Request.Params["data"];
            try
            {
                DataTable dt = ExcelHelper.JsonToDataTable(json);
                string pathDestop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                ExcelHelper.GridToExcelByNPOI(dt, pathDestop + "\\" + "销售订单-" + DateTime.Now.ToString("yyyy-MM-dd") + "导出" + ".xls");
                return Content("1");
            }
            catch (Exception)
            {
                return Content("-1");
            }
        }

效果图

技术分享

导出之后

技术分享

 

 

--------------------------------------------------------------------------------------------------------- 

转载请记得说明作者和出处哦-.-
作者:KingDuDu
原文出处:http://www.cnblogs.com/kingdudu/p/4863980.html

---------------------------------------------------------------------------------------------------------

【第十四篇】easyui datagrid导出excel

标签:

原文地址:http://www.cnblogs.com/kingdudu/p/4863980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!