繁体   English   中英

重新排序没有互操作对象的Excel表

[英]Reordering Excel Table Without Interop Objects

我正在用C#编写一个程序,该程序需要按列之一对工作表进行重新排序。 目前,我无法使用Office Interop对象来控制文件,因此我一直在使用Excel Data Reader(http://exceldatareader.codeplex.com/)进行读取,以便读取文件作为输出。始终位于csv,tsv或SQL表中。 关于如何将DataTable保存为excel或是否存在不使用Interop对其重新排序的命令的任何想法?

有一种方法可以通过Web网格将数据表保存到Excel,而无需使用Interop。 我想您正在谈论一个应用程序,因此您必须参考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 );
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM