如何使用LINQ to Entity将数据导出到Excel?

我的数据来自我的ASP.NET页面上的实体数据模型表。 现在,我必须将这些数据导出到button单击的Excel中。

如果使用的是OLEDB,则直接显示在这里: http : //csharp.net-informations.com/excel/csharp-excel-oledb-insert.htm

这里是我的function从查询表中读取数据:

var model = from i in myEntity.Inquiries where i.User_Id == 5 orderby i.TX_Id descending select new { RequestID = i.TX_Id, CustomerName = i.CustomerMaster.FirstName, RequestDate = i.RequestDate, Email = i.CustomerMaster.MS_Id, DocDescription = i.Document.Description, ProductName = i.Product.Name 

您仍然可以使用链接文章中标识的相同技术插入到Excel电子表格中。

只需使用下面的伪代码

 try { System.Data.OleDb.OleDbConnection MyConnection ; System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand(); string sql = null; MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;"); MyConnection.Open(); myCommand.Connection = MyConnection; myCommand.CommandText = "Insert into [Sheet1$] (id,name) values('@p1', '@p2')"; myCommand.Parameters.Add("@p1", OleDbType.VarChar, 100); myCommand.Parameters.Add("@p2", OleDbType.VarChar, 100); // define query to entity data model var model = from i in myEntity.Inquiries select i; foreach(var m in model) { cmd.Parameters["@p1"].Value = m.RequestID; cmd.Parameters["@p2"].Value = m.CustomerName; // .. Add other parameters here cmd.ExecuteNonQuery(); } } 

您只需编写数据的string表示forms – 为每个字段分隔的制表符和为每行分隔的\ r \ n。 然后将其作为一个.csv文件在浏览器中进行stream式处理,该文件将在Excel中自动打开。

您可以使用reflection来获取属性列表,然后使用该属性列表(和reflection)将查询结果插入到旧的ADO.Net DataTable中。 DataTable具有WriteXML,可用于将临时XML文件存储在临时文件夹中(使用System.IO)。 然后在Excel应用程序中简单地使用OpenXML。

如果你想尝试这种方法,我有http://social.msdn.microsoft.com/Forums/en-US/whatforum/thread/69869649-a238-4af9-8059-55499b50dd57代码示例。 IMO似乎是最快的(至less比直接写入Excel更快),最简单的方法(至less,试图将查询转换成某种XML格式更容易一些),而最可重用的方法(另外在代码示例中,我们是迟到的绑定,所以您可以针对至less使用Excel 2003的混合环境进行开发)。

如前所述,将数据导出为ex​​cel的最简单方法是生成文本或xml表示。 至于我,我更喜欢使用SpreadSheetML和T4文本模板引擎进行文件生成。 你可以看看这里的示例T4文件: http : //lilium.codeplex.com/SourceControl/changeset/view/40985#803959 。

如果你决定使用T4,请记住T4是MS Visual Studio的一部分,你不能单独分发它。 该问题可以通过在目标机器上安装Visual Studio Express Edition来解决。

或者,您可以使用内置的aspx模板引擎,用于生成aspx视图类。 看看它是如何完成的[哎呀,这是不允许我发布更多的超链接,如果你仍然感兴趣,让我知道](注意,这是一个真实的应用程序,所以代码非常大,肮脏)。 Aspx引擎以自己的方式处理Style标签,因此您必须使用限定的名称才能使其工作,Visual Studio中的自动格式化也将无法正常工作。

您可以考虑使用SpreadSheetML,基本上这是一个在xml文件顶部提到的带有Cocoon过程的XML文件,通过双击xml文件来启动excel应用程序。 SpreadSheetML的示例如下所示。

 <?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" xmlns:dt="urn:schemas-microsoft-com:datatypes" xmlns:ms="urn:schemas-microsoft-com:xslt"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <Author xmlns="urn:schemas-microsoft-com:office:spreadsheet">Author<"/Author> <LastAuthor xmlns="urn:schemas-microsoft-com:office:spreadsheet">Author<"/LastAuthor> <Created xmlns="urn:schemas-microsoft-com:office:spreadsheet"/> <LastSaved xmlns="urn:schemas-microsoft-com:office:spreadsheet"/> <Company xmlns="urn:schemas-microsoft-com:office:spreadsheet">Author<"/Company> <Version xmlns="urn:schemas-microsoft-com:office:spreadsheet">11.8132<"/Version> </DocumentProperties> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <WindowHeight xmlns="urn:schemas-microsoft-com:office:spreadsheet">12660<"/WindowHeight> <WindowWidth xmlns="urn:schemas-microsoft-com:office:spreadsheet">19020<"/WindowWidth> <WindowTopX xmlns="urn:schemas-microsoft-com:office:spreadsheet">120<"/WindowTopX> <WindowTopY xmlns="urn:schemas-microsoft-com:office:spreadsheet">105<"/WindowTopY> <ProtectStructure xmlns="urn:schemas-microsoft-com:office:spreadsheet">False<"/ProtectStructure> <ProtectWindows xmlns="urn:schemas-microsoft-com:office:spreadsheet">False<"/ProtectWindows> </ExcelWorkbook> <Styles> <Style ss:ID="s21"> <NumberFormat ss:Format="Percent"/> </Style> <Style ss:ID="s22"> <NumberFormat ss:Format="[ENG][$-409]d\-mmm\-yyyy;@"/> </Style> <Style ss:ID="s23"> <NumberFormat ss:Format="mm/dd/yyyy;@"/> </Style> <Style ss:ID="s24"> <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/> <Font x:Family="Swiss" ss:Bold="1"/> </Style> <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom"/> <Borders/> <Font/> <Interior/> <NumberFormat/> <Protection/> </Style> </Styles> <Worksheet ss:Name="SomeSheetName"> <Table ss:ExpandedColumnCount="33" ss:ExpandedRowCount="5768" x:FullColumns="1" x:FullRows="1"> <Column ss:Width="111"/> <Row> <Cell ss:StyleID="s24"> <Data ss:Type="String">ABCD<"/Data> </Cell> <Cell ss:StyleID="s24"> <Data ss:Type="String">ABCD<"/Data> </Cell> <Cell ss:StyleID="s24"> <Data ss:Type="String">ABCD<"/Data> </Cell> <Cell ss:StyleID="s24"> <Data ss:Type="String">ABCD<"/Data> </Cell> <Cell ss:StyleID="s24"> <Data ss:Type="String">ABCD<"/Data> </Cell> </Row> </Column> </Table> <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> <Selected/> <ProtectObjects>False<"/ProtectObjects> <ProtectScenarios>False<"/ProtectScenarios> </WorksheetOptions> </Worksheet> </Workbook> 

希望这可以帮助。

你可以使用开源的NPOI库来写出excel,doc,powerpoint等

简单的方法是绑定一个网格视图并将其导出,请参阅http://aspalliance.com/771 (首先用谷歌search示例)