簡體   English   中英

從網格導出數據到Excel時替換char

[英]Replace char while exporting data from grid to excel

我使用以下代碼片段從radGrid導出數據:

protected void ExpExcel_Click(object sender, EventArgs e)
{
    foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
    {
        if (col.UniqueName.Contains("notes") || (col.UniqueName.Contains("EditCommandColumn")) ||
            (col.UniqueName.Contains("column1")))
        {
            col.Display = false;
        }
        else
        {
            col.Display = true;
        }
    }
    foreach (GridFilteringItem item in RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem))
        item.Visible = false;
    RadGrid1.ExportSettings.ExportOnlyData = true;
    RadGrid1.MasterTableView.ExportToExcel();        
}

而且工作正常。 我的問題是我需要替換列中的一個字符,我有點迷路了。 導出以優化數據時,如何替換特定列中的特定字符?

如果您確切知道要刪除的字符類型,則可以使用Replace方法:

string a = "Example1";

a.Replace("1",string.Empty);

您的例子是:

col.UniqueName.Replace("[what you want to remove]", "[what you want it to replace with]");

我重寫了您的方法,並為“注釋”列中的所有單元格添加了下划線(_)的所有空格('')的替換:

protected void ExpExcel_Click(object sender, EventArgs e)
{
    foreach (GridColumn col in rgTest.MasterTableView.Columns)
    {
        col.Display = !(col.UniqueName.Contains("notes") || col.UniqueName.Contains("EditCommandColumn") || col.UniqueName.Contains("column1"));
    }

    List<Telerik.Web.UI.GridItem> items = rgTest.MasterTableView.GetItems(Telerik.Web.UI.GridItemType.FilteringItem).ToList();
    foreach (GridFilteringItem item in items)
    {
        item.Visible = false;

        // Replace any space character with an underscore:
        if (item.OwnerTableView.DataKeyNames[0] == "notes")
        {
            var c = item.Controls[0] as System.Web.UI.WebControls.TextBox;
            if (c != null)
            {
                c.Text.Replace(' ', '_');
            }
        }
    }

    rgTest.ExportSettings.ExportOnlyData = true;
    rgTest.MasterTableView.ExportToExcel();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM