2011-01-19 52 views
4

從Web應用程序中,是否有一種將LINQ對象列表導出到Excel文件的簡單方法?有沒有好的圖書館可以做到這一點?將LINQ對象列表導出到Excel文件

+1

可能重複[如何使用LINQ to實體的數據導出到Excel?](http://stackoverflow.com/questions/2202606/how-to-export-data-to-excel-using-linq -to-entity) – 2011-01-19 15:50:13

+0

我會想象你將不得不使用ado.net,看看一些類似的問題http://stackoverflow.com/questions/3486278/c-export-to-excel,http:// stackoverflow .COM /問題/ 151005 /創建-Excel的XLS-和XLSX文件,從-C。乾杯 – msarchet 2011-01-19 15:52:17

回答

0

因此,在指定Excel時,您要提交一個行/列範例(而不是xml)。所以你必須指定屬性如何映射到列。

除此之外,您使用Ole DB provider寫入Excel文件。循環遍歷你的對象,使用一個stringbuilder爲每個對象生成一個INSERT語句,然後在你的工作表上執行它。像蛋糕一樣容易。

4

退房VB團隊的視頻從ASP.Net鏈接,貝絲·馬西實際上做了一個非常類似的演示,可能做你想要什麼:

http://www.asp.net/linq/videos/how-do-i-create-excel-spreadsheets-using-linq-to-xml

您可能會發現其他人在同一系列有用,他們在這裏大約4/5上下頁:

http://www.asp.net/web-forms/data

還有一個叫做LINQ到Excel的項目,該項目是在這裏 - http://code.google.com/p/linqtoexcel/

或者你可以使用的OpenXML的圖書館做這樣的事情,在這裏就是這樣一個例子 - http://msdn.microsoft.com/en-us/library/bb508943(v=office.12).aspx

的Excel也可以打開XML文件直接,因此你可以使用XML序列化或任何其他的只是創建一個XML輸出方法,並在Excel中打開它。

13

這是Excel導出我基於上面的VB視頻鏈接結束了。它接受任何對象列表(它不包括實體框架對象上的導航屬性和集合)並將它們導出到Excel。它在約4秒內輸出約35K記錄。

public void ExportToExcel<T>(List<T> list) 
    { 
     int columnCount = 0; 

     DateTime StartTime = DateTime.Now; 

     StringBuilder rowData = new StringBuilder(); 

     PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); 

     rowData.Append("<Row ss:StyleID=\"s62\">"); 
     foreach (PropertyInfo p in properties) 
     { 
      if (p.PropertyType.Name != "EntityCollection`1" && p.PropertyType.Name != "EntityReference`1" && p.PropertyType.Name != p.Name) 
      { 
       columnCount++; 
       rowData.Append("<Cell><Data ss:Type=\"String\">" + p.Name + "</Data></Cell>"); 
      } 
      else 
       break; 

     } 
     rowData.Append("</Row>"); 

     foreach (T item in list) 
     {     
      rowData.Append("<Row>"); 
      for (int x = 0; x < columnCount; x++) //each (PropertyInfo p in properties) 
      {      
       object o = properties[x].GetValue(item, null); 
       string value = o == null ? "" : o.ToString();      
       rowData.Append("<Cell><Data ss:Type=\"String\">" + value + "</Data></Cell>"); 

      } 
      rowData.Append("</Row>"); 
     } 

     var sheet = @"<?xml version=""1.0""?> 
        <?mso-application progid=""Excel.Sheet""?> 
        <Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet"" 
         xmlns:o=""urn:schemas-microsoft-com:office:office"" 
         xmlns:x=""urn:schemas-microsoft-com:office:excel"" 
         xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"" 
         xmlns:html=""http://www.w3.org/TR/REC-html40""> 
         <DocumentProperties xmlns=""urn:schemas-microsoft-com:office:office""> 
          <Author>MSADMIN</Author> 
          <LastAuthor>MSADMIN</LastAuthor> 
          <Created>2011-07-12T23:40:11Z</Created> 
          <Company>Microsoft</Company> 
          <Version>12.00</Version> 
         </DocumentProperties> 
         <ExcelWorkbook xmlns=""urn:schemas-microsoft-com:office:excel""> 
          <WindowHeight>6600</WindowHeight> 
          <WindowWidth>12255</WindowWidth> 
          <WindowTopX>0</WindowTopX> 
          <WindowTopY>60</WindowTopY> 
          <ProtectStructure>False</ProtectStructure> 
          <ProtectWindows>False</ProtectWindows> 
         </ExcelWorkbook> 
         <Styles> 
          <Style ss:ID=""Default"" ss:Name=""Normal""> 
           <Alignment ss:Vertical=""Bottom""/> 
           <Borders/> 
           <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000""/> 
           <Interior/> 
           <NumberFormat/> 
           <Protection/> 
          </Style> 
          <Style ss:ID=""s62""> 
           <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000"" 
            ss:Bold=""1""/> 
          </Style> 
         </Styles> 
         <Worksheet ss:Name=""Sheet1""> 
          <Table ss:ExpandedColumnCount=""" + (properties.Count() + 1) + @""" ss:ExpandedRowCount=""" + (list.Count() + 1) + @""" x:FullColumns=""1"" 
           x:FullRows=""1"" ss:DefaultRowHeight=""15""> 
           " + rowData.ToString() [email protected]" 
          </Table> 
          <WorksheetOptions xmlns=""urn:schemas-microsoft-com:office:excel""> 
           <PageSetup> 
            <Header x:Margin=""0.3""/> 
            <Footer x:Margin=""0.3""/> 
            <PageMargins x:Bottom=""0.75"" x:Left=""0.7"" x:Right=""0.7"" x:Top=""0.75""/> 
           </PageSetup> 
           <Print> 
            <ValidPrinterInfo/> 
            <HorizontalResolution>300</HorizontalResolution> 
            <VerticalResolution>300</VerticalResolution> 
           </Print> 
           <Selected/> 
           <Panes> 
            <Pane> 
             <Number>3</Number> 
             <ActiveCol>2</ActiveCol> 
            </Pane> 
           </Panes> 
           <ProtectObjects>False</ProtectObjects> 
           <ProtectScenarios>False</ProtectScenarios> 
          </WorksheetOptions> 
         </Worksheet> 
         <Worksheet ss:Name=""Sheet2""> 
          <Table ss:ExpandedColumnCount=""1"" ss:ExpandedRowCount=""1"" x:FullColumns=""1"" 
           x:FullRows=""1"" ss:DefaultRowHeight=""15""> 
          </Table> 
          <WorksheetOptions xmlns=""urn:schemas-microsoft-com:office:excel""> 
           <PageSetup> 
            <Header x:Margin=""0.3""/> 
            <Footer x:Margin=""0.3""/> 
            <PageMargins x:Bottom=""0.75"" x:Left=""0.7"" x:Right=""0.7"" x:Top=""0.75""/> 
           </PageSetup> 
           <ProtectObjects>False</ProtectObjects> 
           <ProtectScenarios>False</ProtectScenarios> 
          </WorksheetOptions> 
         </Worksheet> 
         <Worksheet ss:Name=""Sheet3""> 
          <Table ss:ExpandedColumnCount=""1"" ss:ExpandedRowCount=""1"" x:FullColumns=""1"" 
           x:FullRows=""1"" ss:DefaultRowHeight=""15""> 
          </Table> 
          <WorksheetOptions xmlns=""urn:schemas-microsoft-com:office:excel""> 
           <PageSetup> 
            <Header x:Margin=""0.3""/> 
            <Footer x:Margin=""0.3""/> 
            <PageMargins x:Bottom=""0.75"" x:Left=""0.7"" x:Right=""0.7"" x:Top=""0.75""/> 
           </PageSetup> 
           <ProtectObjects>False</ProtectObjects> 
           <ProtectScenarios>False</ProtectScenarios> 
          </WorksheetOptions> 
         </Worksheet> 
        </Workbook>"; 

     System.Diagnostics.Debug.Print(StartTime.ToString() + " - " + DateTime.Now); 
     System.Diagnostics.Debug.Print((DateTime.Now - StartTime).ToString()); 

     string attachment = "attachment; filename=Report.xml"; 
     HttpContext.Current.Response.ClearContent(); 
     HttpContext.Current.Response.AddHeader("content-disposition", attachment); 
     HttpContext.Current.Response.Write(sheet); 
     HttpContext.Current.Response.ContentType = "application/ms-excel"; 
     HttpContext.Current.Response.End(); 

    }