2015-12-02 40 views
1

我有以下控制器方法将返回模型中的数据。我如何将所有数据导出为ex​​cel?如何在C#中使用Linq将数据导出为ex​​cel

public ActionResult iDeal_Table_Display(Guid? SA = null) 
    { 
     var iDConfig = webservice.Get_iDealConfigs(SA,null,null,null,null,null,null, out retStatus, out errorMsg); 

     var model = iDConfig.Select(ic => new iDealModel2 
     { 
      SaPrefix = ic.PrefixName, 
      CalendarCode = ic.CalendarCodeName, 
      CashnCarry = ic.isCashnCarry, 
      FreeMM = ic.isFreeMM, 
      OnContract = ic.isOnContract, 
      ProductId = ic.ProductName, 
      RequestTypeId = ic.RequestTypeName 
     }).ToList(); 

     return PartialView(model); 
    } 
+1

[从C#创建Excel文件(.xls和.XLSX)文件(可能的重复http://stackoverflow.com/questions/151005/create-excel-xls - 和 - XLSX文件从C - 锐) – Steve

+0

即时消息不使用正常连接即时通讯使用LinQ – xSea

+0

你可以使用intropt,使用office和excel dll。 –

回答

0

很抱歉的第1个答案。我发现没有安装Office更好的方式引用

 public void toExcel() 
    { 
     var grid = new GridView(); 

     var iDConfig = blergo.Get_iDealConfigs(null, null, null, null, null, null, null, out retStatus, out errorMsg); 

      var model = iDConfig.Select(ic => new iDealModel2 
      { 
       SaPrefix = ic.PrefixName, 
       CalendarCode = ic.CalendarCodeName, 
       CashnCarry = ic.isCashnCarry, 
       FreeMM = ic.isFreeMM, 
       OnContract = ic.isOnContract, 
       ProductId = ic.ProductName, 
       RequestTypeId = ic.RequestTypeName 
      }).ToList(); 

      grid.DataSource = from data in model.OrderBy(x => x.SaPrefix) 
           select new 
           { 
            SAPrefix = data.SaPrefix, 
            CalendarCode = data.CalendarCode, 
            isCash = data.CashnCarry, 
            FreeMM = data.FreeMM, 
            onContract = data.OnContract, 
            Product = data.ProductId, 
            RequestType = data.RequestTypeId 
           }; 

      grid.DataBind(); 

      Response.ClearContent(); 
      Response.AddHeader("content-disposition", "attachment; filename=iDealConfig.xls"); 
      Response.ContentType = "application/excel"; 
      StringWriter sw = new StringWriter(); 
      HtmlTextWriter htmlTextWriter = new HtmlTextWriter(sw); 

      grid.RenderControl(htmlTextWriter); 
      Response.Write(sw.ToString()); 
      Response.End(); 

    } 
0

您可以使用数据表,可以做到这一点为:

public ActionResult ExportToExcel() 
{ 
    var products = new System.Data.DataTable(); 
    products.Columns.Add("code", typeof(int)); 
    products.Columns.Add("description", typeof(string)); 

    // you can add your columns here as many you want 

    var a = db.ABC.Where(x => x.S2 == "ASSETS").FirstOrDefault(); 
    var l = db.ABC.Where(x => x.S2 == "LIABILITIES").FirstOrDefault(); 

    // in this way you can get data from database if you are using.. or u may use any other way to seed your file as per your need 

    products.Rows.Add(a.S1, a.S2, a.S39, a.S40); 
    products.Rows.Add(l.S1, l.S2, l.S39, l.S40); 

    // seeding the rows 

    var grid = new GridView(); 
    grid.DataSource = products; 
    grid.DataBind(); 
    Response.ClearContent(); 
    Response.Buffer = true; 

    Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
    Response.AppendHeader("content-disposition", "attachment; filename=filename.xlsx"); 

    Response.Charset = ""; 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter htw = new HtmlTextWriter(sw); 
    grid.RenderControl(htw); 
    Response.Output.Write(sw.ToString()); 
    Response.Flush(); 
    Response.End(); 

    return View("MyView"); 
} 
相关问题