2012-01-31 97 views
0

我可以將我的radgrid導出到excel文件,但我想在工作表中添加更多信息。使用RadGrid導出自定義Excel文件

如果可能的話,我將不勝感激的教程/示例代碼做一個自定義的Excel文件生成。

    <tel:radgrid runat="server" id="mygrid" skinid="RadGrid_Search_Standard"> 
        <ExportSettings HideStructureColumns="true" /> 

        </tel:radgrid> 

電網正在數據綁定一些數據表,我需要補充一些數據 添加上述

mygrid.MasterTableView.ExportToWord()一些字符串

+3

你能提供你有什麼樣的代碼和您要添加什麼額外??請更具體..謝謝 – MethodMan 2012-01-31 17:59:32

+0

爲什麼有人會降級這個問題? – Kubi 2012-03-07 11:30:19

回答

1

下面是一些代碼,我與Telerik Grid一起使用,而不是使用他們提供的ExportToExcel函數,我創建了自己的按鈕來觸發它自己的導出事件。

我有一個功能(不含稅)叫了getDataSource,我用它來填充網格,你可以覆蓋這個或創建自己獲取數據到一個DataTable和你認爲合適的添加任何行/列/數據。

 //export button calls this 
     private void ExportReport() 
     { 
      SetPublicVariables(); 

      System.Data.DataTable dt = GetDataSource(false); 

      string exportData = buildCSVExportString(dt); 

      string filename = string.Format("{0} - {1}.csv", 
       (Master as MasterPages.Drilldown).Titlelbl.Text, CampaignTitle); 
      if (filename.Length > 255) filename = filename.Substring(0, 255); 

      ExportCSV(exportData, filename); 
     } 

//build a string CSV 
public static string buildCSVExportString(DataTable exportDT) 
     { 
      StringBuilder exportData = new StringBuilder(); 
      // get headers. 

      int iColCount = exportDT.Columns.Count; 
      for (int i = 0; i < iColCount; i++) 
      { 
       exportData.Append(exportDT.Columns[i].ToString()); 
       if (i < iColCount - 1) 
       { 
        exportData.Append(","); 
       } 
      } 
      exportData.Append(System.Environment.NewLine);    

      // get rows. 
      foreach (DataRow dr in exportDT.Rows) 
      { 
       for (int i = 0; i < iColCount; i++) 
       { 
        if (!Convert.IsDBNull(dr[i])) 
        { 
         //If the variable is a string it potentially has charaters that can't be parsed properly. 
         //this fixes the comma issue(which adds aditional columns). Replace and escape " with "". 
         if (dr[i] is string)   
          exportData.Append(String.Format(@"""{0}""", ((string)dr[i]).Replace("\"", @""""""))); 
         else 
          exportData.Append(dr[i].ToString()); 
        } 
        if (i < iColCount - 1) 
        { 
         exportData.Append(","); 
        } 
       } 
       exportData.Append(System.Environment.NewLine); 
      } 
      return exportData.ToString(); 
     } 



public void ExportCSV(string content, string filename) 
     { 
      filename = RemoveIllegalPathChars(filename); 
      HttpResponse Response = HttpContext.Current.Response; 
      string ext = System.IO.Path.GetExtension(filename); 
      Response.ClearHeaders(); 
      Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", filename)); 
      Response.ContentType = "text/csv; charset-UTF-8;"; 
      Response.Clear(); 
      Response.Write(content); 
      Response.End(); 
     } 
1

一種可能的方法是隻在出口前修改HTML代碼。這是如何。

protected override void OnInit(EventArgs e) 
{ 
    base.OnInit(e); 
    RadGridName.GridExporting += (s, a) => 
    { 
     string myHtmlCode = "<span>My HTML code goes here</span>"; 
     a.ExportOutput = a.ExportOutput.Replace("<body>", "<body>" + myHtmlCode); 
    }; 
} 

這應該適用於Excel(而不是ExcelML)和Word。

好運

1

你需要做的就是添加額外的網頁資訊提供給您arg的ExportOutput唯一

void yourRadGridID_GridExporting(object sender, GridExportingArgs e) 
{ 

string additionalPageInfo= "your html code for the additional page info goes here"; 

e.ExportOutput = e.ExportOutput.Replace("`<body>`", "`<body>`" + additionalPageInfo); 

}