0

列の 1 つでシートを並べ替えるために必要なプログラムを C# で作成しています。現時点では、Office Interop オブジェクトを使用してファイルを制御することができないため、出力としてファイルを読み込むために、Excel Data Reader (http://exceldatareader.codeplex.com/) を使用して読み込んでいます。常に csv、tsv、または SQL テーブルにあります。DataTable を Excel として保存する方法、または相互運用なしで並べ替えるコマンドが存在するかどうかについてのアイデアはありますか?

4

1 に答える 1

1

Web グリッドを使用して、Interop を使用せずにデータテーブルを Excel に保存する方法があります。アプリケーションについて話していると思うので、System.Webライブラリへの参照を作成する必要があります。

コード:

// Create the DataGrid and perform the databinding
var myDataGrid = new System.Web.UI.WebControls.DataGrid();
myDataGrid.HeaderStyle.Font.Bold = true;
myDataGrid.DataSource = myDataTable;
myDataGrid.DataBind();

string myFile = "put the name here with full path.xls"
var myFileStream = new FileStream( myFile, FileMode.Create, FileAccess.ReadWrite );

// Render the DataGrid control to a file
using ( var myStreamWriter = new StreamWriter( myFileStream ) )
{
    using (var myHtmlTextWriter = new System.Web.UI.HtmlTextWriter(myStreamWriter ))
    {
        myDataGrid .RenderControl( myHtmlTextWriter );
    }
}
于 2011-02-10T21:58:13.113 に答える