简体   繁体   English

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

[英]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. 我正在用C#编写一个程序,该程序需要按列之一对工作表进行重新排序。 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://exceldatareader.codeplex.com/) in order to read in the files, as my output is always in csv, tsv, or a SQL table. 目前,我无法使用Office Interop对象来控制文件,因此我一直在使用Excel Data Reader(http://exceldatareader.codeplex.com/)进行读取,以便读取文件作为输出。始终位于csv,tsv或SQL表中。 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? 关于如何将DataTable保存为excel或是否存在不使用Interop对其重新排序的命令的任何想法?

There is a way to save a datatable to Excel, without using Interop, by means of a web-grid. 有一种方法可以通过Web网格将数据表保存到Excel,而无需使用Interop。 I suppose you're talking about an application, so you have to make a reference to the System.Web library. 我想您正在谈论一个应用程序,因此您必须参考System.Web库。

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

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

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