【问题标题】:How to Export data to Excel using LINQ to Entity?如何使用 LINQ to Entity 将数据导出到 Excel?
【发布时间】:2012-10-24 02:29:38
【问题描述】:
我的 ASP.NET 页面上的数据来自实体数据模型表。
现在我必须在单击按钮时将此数据导出到 Excel 中。
如果它使用的是 OLEDB,那么直接如下:http://csharp.net-informations.com/excel/csharp-excel-oledb-insert.htm
这是我从查询表中读取数据的函数:
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
【问题讨论】:
标签:
c#
asp.net
excel
linq-to-entities
export-to-excel
【解决方案1】:
您仍然可以使用链接文章中指出的相同技术插入 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();
}
}
【解决方案2】:
您可以只编写数据的字符串表示形式 - 制表符为每个字段分隔,\r\n 为每一行分隔。然后将其作为 .csv 文件从浏览器中流出,该文件将在 Excel 中自动打开。
【解决方案4】:
如前所述,将数据导出到 excel 的最简单方法是生成文本或 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 中的自动格式化也无法正常工作。
【解决方案5】:
您可以考虑使用 SpreadSheetML,它基本上是一个 XML 文件,在 xml 文件顶部提到了一个 Cocoon 进程,以在双击 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>
希望这会有所帮助。
【解决方案6】:
可以使用开源的NPOI库写出excel、doc、powerpoint等