开发者

Reordering Excel Table Without Interop Objects

I am writing a program in C# that is required to reorder a sheet by one of the columns. At the moment I am unable to use Office Interop objects to control the file, so I have been reading it in using Excel Data Reader (http://exceldataread开发者_运维知识库er.codeplex.com/) in order to read in the files, as my output is always in csv, tsv, or a SQL table. Any ideas as to how I would be able to save the DataTable as excel or if a command exists to reorder it without Interop?


There is a way to save a datatable to Excel, without using Interop, by means of a web-grid. I suppose you're talking about an application, so you have to make a reference to the System.Web library.

The code:

// 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 );
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜