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

リフレクションを使用してプロパティのリストを取得し、そのプロパティ (およびリフレクション) のリストを使用して、クエリの結果を古い ADO.Net DataTable にプラグインできます。DataTable には、(System.IO を使用して) 一時フォルダーに一時 XML ファイルを格納するために使用できる WriteXML があります。次に、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 に答える
1

データの文字列表現を書くことができます - 各フィールドはタブで区切られ、各行は \r\n で区切られます。次に、それをブラウザーから .csv ファイルとしてストリーミングし、Excel で自動的に開きます。

于 2010-02-05T15:15:11.623 に答える
0

オープン ソースの NPOI ライブラリを使用して、Excel、ドキュメント、パワーポイントなどを書き出すことができます。

于 2010-04-05T15:12:07.393 に答える
0

簡単な方法は、グリッド ビューをバインドしてエクスポートすることです。http: //aspalliance.com/771 を参照してください(最初のサンプルは Google で検索)

于 2010-04-07T12:00:01.290 に答える
0

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>

お役に立てれば。

于 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 に答える