第一种方式:

      [HttpPost]
        public ActionResult ExportPageOrder(FormCollection form)
        {
            try
            {
                Response.Charset = "GB2312";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/vnd.ms-excel";//设置输入类型为Excel文件,指定返回的是一个不能被客户端读取的流,必须被下载
                Response.AddHeader("Content-Disposition", "attachment;filename=订单信息.xls");//添加Http表头,将文件保存为Test.xls

                List<Order> resultOrderList = OrderRepository.List(
               StringHelper.String2Int(form["ShopID"]),
               StringHelper.String2Int(form["OrderID"]),
               StringHelper.String2Int(form["ProductID"]),
               form["ProductName"] ?? string.Empty,
               StringHelper.String2Int(form["OrderStatusID"]),
               StringHelper.String2Int(form["PayingID"]),
               form["CustomerName"] ?? string.Empty,
               form["CustomerMobile"] ?? string.Empty,
               form["CustomerTel"] ?? string.Empty,
               form["ConsigneeName"] ?? string.Empty,
               form["ConsigneeMobileNo"] ?? string.Empty,
               form["ConsigneeTelephone"] ?? string.Empty,
               form["startCreateDate"] ?? "2000-01-01",
               form["endCreateDate"] ?? DateTime.Now.ToString("yyyy-MM-dd"));

                if (resultOrderList.Count() == 0)
                    throw new Exception("未查询到订单信息.");

                string excelstr = "支付单号\t订单号\t客户\t厂商\t商品名称\t是否开发票\t发票号\t发票类型\t订单金额\t支付单金额\t支付方式";
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.AppendLine(excelstr);

                string paytype = string.Empty;
                foreach (var item in resultOrderList)
                {
                    if (item.Paying.PayType != null && item.Paying.PayTypeID == 0)
                    {
                        paytype = "未支付";
                    }
                    else if (item.Paying.PayType != null && item.Paying.PayTypeID != 0)
                    {
                        paytype = item.Paying.PayType.PayTypeName;
                    }
                    sb.AppendLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}",
                                               item.PayingID,
                                               item.OrderID,
                                               item.Customer == null ? "" : item.Customer.LoginName,
                                               item.SellerShopName,
                                               item.Product == null ? "" : item.Product.ProductName,
                                               item.InvoiceID == null ? "" : "",
                                                "",
                                                (item.Invoice != null && item.Invoice.InvoiceType == 0) ? "增值税普通发票" : "增值税专用发票",
                                               item.TotalAmout,
                                               item.Paying == null ? "" : item.Paying.PayingAmount.ToString(),
                                               paytype));
                }

              return Content(sb.ToString());

            }
            catch (Exception ee)
            {
                return Content(string.Format("<script>alert(\"{0}\");location.href=\"{1}\";</script>", ee.Message, "/Order/List"));
            }
        }

 第二种:

 [HttpPost]
        public ActionResult ExportPageOrder(FormCollection form)
        {
            try
            {
               List<Order> resultOrderList = OrderRepository.List(
               StringHelper.String2Int(form["ShopID"]),
               StringHelper.String2Int(form["OrderID"]),
               StringHelper.String2Int(form["ProductID"]),
               form["ProductName"] ?? string.Empty,
               StringHelper.String2Int(form["OrderStatusID"]),
               StringHelper.String2Int(form["PayingID"]),
               form["CustomerName"] ?? string.Empty,
               form["CustomerMobile"] ?? string.Empty,
               form["CustomerTel"] ?? string.Empty,
               form["ConsigneeName"] ?? string.Empty,
               form["ConsigneeMobileNo"] ?? string.Empty,
               form["ConsigneeTelephone"] ?? string.Empty,
               form["startCreateDate"] ?? "2000-01-01",
               form["endCreateDate"] ?? DateTime.Now.ToString("yyyy-MM-dd"));

                if (resultOrderList.Count() == 0)
                    throw new Exception("未查询到订单信息.");

                string excelstr = "支付单号\t订单号\t客户\t厂商\t商品名称\t是否开发票\t发票号\t发票类型\t订单金额\t支付单金额\t支付方式";
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.AppendLine(excelstr);

                string paytype = string.Empty;
                foreach (var item in resultOrderList)
                {
                    if (item.Paying.PayType != null && item.Paying.PayTypeID == 0)
                    {
                        paytype = "未支付";
                    }
                    else if (item.Paying.PayType != null && item.Paying.PayTypeID != 0)
                    {
                        paytype = item.Paying.PayType.PayTypeName;
                    }
                    sb.AppendLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}",
                                               item.PayingID,
                                               item.OrderID,
                                               item.Customer == null ? "" : item.Customer.LoginName,
                                               item.SellerShopName,
                                               item.Product == null ? "" : item.Product.ProductName,
                                               item.InvoiceID == null ? "" : "",
                                                "",
                                                (item.Invoice != null && item.Invoice.InvoiceType == 0) ? "增值税普通发票" : "增值税专用发票",
                                               item.TotalAmout,
                                               item.Paying == null ? "" : item.Paying.PayingAmount.ToString(),
                                               paytype));
                }

               byte[] byteArray = System.Text.Encoding.Default.GetBytes(sb.ToString());
               return File(byteArray, "application/vnd.ms-excel", "订单信息.xls");
                         }
            catch (Exception ee)
            {
                return Content(string.Format("<script>alert(\"{0}\");location.href=\"{1}\";</script>", ee.Message, "/Order/List"));
            }
        }

 

posted on 2015-05-05 10:10  日月草方  阅读(362)  评论(0编辑  收藏  举报