【问题标题】:Reordering Excel Table Without Interop Objects重新排序没有互操作对象的 Excel 表
【发布时间】:2011-06-25 03:56:03
【问题描述】:

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

【问题讨论】:

    标签: c# excel export export-to-excel web-based


    【解决方案1】:

    有一种方法可以通过网络网格将数据表保存到 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 );
        }
    }
    

    【讨论】:

    • 谢谢——这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2016-08-14
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多