导出数据到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 = "支付单号\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"));
            }
        }
时间: 2024-10-28 22:19:31

导出数据到Excel文件的相关文章

CodeIgniterCodeigniter+PHPExcel导出数据到Excel文件

解压压缩包里的Classes文件夹中的内容到application\libraries\目录下,目录结构如下:--application\libraries\PHPExcel.php--application\libraries\PHPExcel(文件夹)修改application\libraries\PHPExcel\IOFactory.php文件--将其类名从PHPExcel_IOFactory改为IOFactory,遵从CI类命名规则.--将其构造函数改为public $this->loa

【代码实现】PHP导入Excel和导出数据为Excel文件

文章来源:PHP开发学习门户 地址:http://www.phpthinking.com/archives/560 有时需要将Excel表格的数据导入到mysql数据库中,我们使用PHP的一个开源项目PHP-ExcelReader可以轻松实现Excel的导入.另外将mysql数据导出为Excel与本站上篇文章中导出CSV一样,只是将逗号分割符换成制表符,并修改header信息就可以了. 下载源码 本文中,我们沿用本站文章:使用PHP导入和导出CSV文件 中实例的数据表以及html. 1.导入XL

使用PHP导入Excel和导出数据为Excel文件

1.导入XLS PHP-ExcelReader这是一个开源的项目,主要是来解析excel的文件,您可以到http://sourceforge.net/projects/phpexcelreader获取最新版的源码.下载之后解压,主要用到excel文件夹里面的两个文件reader.php和oleread.inc. 导入Xls处理流程:选择xls文件->上传xls文件到服务器->通过PHP-ExcelReader解析excel->批量入库.  include_once("excel

java Servlet导出数据到Excel文件

package com.lbc.excel.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet

导出数据为Excel文件---Controller

@Controller @RequestMapping("/vehmileage") public class VehMileageController extends BaseController{ private static final Logger LOG=Logger.getLogger(VehMileageController.class); private static final String FILE_NAME="里程导出"; private st

导出数据为Excel文件-JS

var params=''; if(vahiclePlate!=''){ params='lpn'+vahiclePlate; } if(startTime!=''){ params=params==''?'startTime='+startTime:(params+'&')+'startTime='+startTime; } .....搜索条件 if(params!=''){ params+='&'+'fileName=车队行程统计导出'; params+='&'+'lpnNam

通过LocalReport将rdl文件导出数据到excel文件

protected void sssss_Click(object sender, EventArgs e) { LocalReport report = new LocalReport(); report.ReportPath = @"D:\pdftest.rdl"; DbProviderFactory dbf = DbProviderFactories.GetFactory(); using (IDbConnection con = dbf.CreateConnection())

导出数据为Excel文件---Client2

public class ExportVehMileageClients extends RestfulWSClient<ServiceResponse>{ private static final Logger LOG=Logger.getLogger(ExportVehMileagesClient.class); private static final String WSURL=GET_EXPORTVEHMILEAGES_URL; private static final String

PHP导出MySQL数据到Excel文件

PHP导出MySQL数据到Excel文件 转载 常会碰到需要从数据库中导出数据到Excel文件,用一些开源的类库,比如PHPExcel,确实比较容易实现,但对大量数据的支持很不好,很容易到达PHP内存使用上限.这里的方法是利用fputcsv写CSV文件的方法,直接向浏览器输出Excel文件. ? 1 <br><!--?php// 输出Excel文件头,可把user.csv换成你要的文件名header('Content-Type: application/vnd.ms-excel');he