zoukankan      html  css  js  c++  java
  • 导出数据到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"));
                }
            }
  • 相关阅读:
    分享一个自己写的vue多语言插件smart-vue-i18n
    利用vw+rem实现移动web适配布局
    你说前端不了解业务?
    小程序开发总结一:mpvue框架及与小程序原生的混搭开发
    小码农的职场人生一:由张小平离职引发的一些吐槽
    javascript本地缓存方案-- 存储对象和设置过期时间
    手淘移动适配方案flexible.js兼容bug处理
    微信小程序入坑之自定义组件
    vuejs开发组件分享之H5图片上传、压缩及拍照旋转的问题处理
    非域环境下使用证书部署数据库(SqlServer2008R2)镜像
  • 原文地址:https://www.cnblogs.com/wangchengshen/p/4478397.html
Copyright © 2011-2022 走看看