导出数据到Excel文件

第一种方式:

      [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 = "支付单号	订单号	客户	厂商	商品名称	是否开发票	发票号	发票类型	订单金额	支付单金额	支付方式";
                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}	{1}	{2}	{3}	{4}	{5}	{6}	{7}	{8}	{9}	{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 = "支付单号	订单号	客户	厂商	商品名称	是否开发票	发票号	发票类型	订单金额	支付单金额	支付方式";
                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}	{1}	{2}	{3}	{4}	{5}	{6}	{7}	{8}	{9}	{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"));
            }
        }
原文地址:https://www.cnblogs.com/wangchengshen/p/4478397.html