Exporter une list d'objects LINQ vers le file Excel

À partir d'une application Web, existe-t-il un moyen simple d'exporter une list d'objects LINQ vers un file Excel? Existe-t-il de bonnes bibliothèques qui peuvent faire cela?

    Donc, en spécifiant Excel, vous vous engagez dans un paradigme ligne / colonne (par opposition à xml, par exemple). Vous devrez donc spécifier comment les propriétés correspondent aux colonnes.

    Au-delà, vous écrivez dans un file Excel à l'aide du fournisseur Ole DB . Faites une boucle dans vos objects, générant une instruction INSERT pour chacun en utilisant un créateur de strings et exécutez-le contre votre feuille. Facile comme un gâteau.

    C'est l'export Excel que j'ai terminé en fonction du lien vers la video VB ci-dessus. Il prend toute list d'objects (il exclut les propriétés de navigation et les collections sur les objects Entity Framework) et les exporte vers Excel. Il exporte environ 35K loggings en ~ 4 secondes.

    public void ExportToExcel<T>(List<T> list) { int columnCount = 0; DateTime StartTime = DateTime.Now; SsortingngBuilder rowData = new SsortingngBuilder(); 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=\"Ssortingng\">" + 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); ssortingng value = o == null ? "" : o.ToSsortingng(); rowData.Append("<Cell><Data ss:Type=\"Ssortingng\">" + 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.ToSsortingng() +@" </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.ToSsortingng() + " - " + DateTime.Now); System.Diagnostics.Debug.Print((DateTime.Now - StartTime).ToSsortingng()); ssortingng 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(); } 

    Découvrez les videos de l'équipe VB liées à ASP.Net, Beth Massi fait en fait une démo très similaire qui pourrait faire ce que vous voulez:

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

    Vous pourriez find d'autres personnes dans la même série utiles, elles sont ici vers 4/5 en bas de la page:

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

    Il existe également un projet appelé Linq-To-Excel qui est ici – http://code.google.com/p/linqtoexcel/

    Ou vous pouvez utiliser les bibliothèques OpenXML pour faire ce genre de chose, voici un tel exemple – http://msdn.microsoft.com/en-us/library/bb508943(v=office.12).aspx

    Excel peut également ouvrir des files XML directement, afin que vous puissiez simplement créer une sortie XML à l'aide de la serialization XML ou autre méthode et l'ouvrir dans Excel.