8

我的 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
4

7 回答 7

3

您仍然可以使用链接文章中确定的相同技术插入 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();
    }
  } 
于 2010-03-29T01:00:08.967 回答
1

您可以只编写数据的字符串表示形式 - 制表符为每个字段分隔,\r\n 为每一行分隔。然后将其作为 .csv 文件从浏览器中流出,该文件将在 Excel 中自动打开。

于 2010-02-05T15:15:11.623 回答
1

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

如果您想尝试这种方法,我在http://social.msdn.microsoft.com/Forums/en-US/whatforum/thread/69869649-a238-4af9-8059-55499b50dd57有代码示例。IMO 这似乎是最快的(至少,比尝试直接写入 Excel 快得多)、最简单的(至少,比尝试自己将查询转换为某种 XML 格式容易得多)和最可重用的方法(加上在代码示例中我们是后期绑定的,因此您可以针对混合环境进行开发,假设至少 Excel 2003)。

于 2012-10-23T18:29:38.270 回答
0

可以使用开源的NPOI库写出excel、doc、powerpoint等

于 2010-04-05T15:12:07.393 回答
0

最简单的方法是绑定一个网格视图并将其导出,请参阅http://aspalliance.com/771(首先使用 google 查找 esample)

于 2010-04-07T12:00:01.290 回答
0

您可以考虑使用 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>

希望这可以帮助。

于 2010-03-10T08:41:45.150 回答
0

如前所述,将数据导出到 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 中的自动格式化也将无法正常工作。

于 2010-03-22T07:30:22.057 回答