2009-08-13 84 views
1

我需要將ComponentArt網格中的內容導出到文件中,最好是csv格式的excel。ComponentArt:導出網格數據

我想知道如果有人有任何想法如何最好地接近這項任務。目前,我們已經在網格中填充了數據,並且在向用戶顯示之前使用客戶端模板進行一些小操作。

所應用的模板的一個例子是:

<ComponentArt:ClientTemplate Id="PostTemplate"> 
    ## DataItem.GetMember("LastPostBy").Value ##<br />## DataItem.GetMember("LastPostDate").Value ## 
</ComponentArt:ClientTemplate> 

Where the column definitions are: 

<ComponentArt:GridColumn Width="140" HeadingText="Last Post By " DataCellClientTemplateId="PostTemplate" /> 
<ComponentArt:GridColumn DataField="LastPostBy" Visible="false" /> 
<ComponentArt:GridColumn DataField="LastPostDate" Visible="false" /> 

所以當電網出口,我想該文件包含的內容是在網格在出口的那一刻起,包括任何模板的變化,可能如果可能的話可見。

非常感謝您的幫助。

回答

3

您無法直接使用RenderControl將Web.UI網格導出到文本編寫器中。相反,您應該將其數據轉換爲另一種形式(如ASP DataGrid)並導出。

這背後的原因是Web.UI網格的結構是在客戶端動態構建的 - 它從服務器傳遞到客戶端,形成一堆XML和數組數據,它們被轉換爲您看到的表格和div在窗口中。

因此,您希望在網格的數據源上執行導出,然後分析客戶端模板以複製在客戶端上動態構建時要更改的內容。

以下是如何將CA電網將其導出到Excel中一個簡單的例子:

public void ExportDataSetToExcel() { 
    object theColl = gridEmployee.DataSource; //just set this as your grid's datasource. 
    GridView excelGrid = new GridView(); 
    excelGrid.DataSource = theColl; 
    excelGrid.ID = "Export"; 
    excelGrid.DataBind(); 
    excelGrid.AutoGenerateColumns = true; 

    Response.Clear(); 
    Response.Buffer = true; 
    Response.ContentType = "application/vnd.ms-excel"; 
    Response.AddHeader("content-disposition", "attachment;filename=RegainExport.xls"); 
    Response.Charset = ""; 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
    excelGrid.RenderControl(htmlWrite); 
    Response.Write(stringWrite.ToString()); 
    Response.End(); 
} 

所以結合在GridView的數據源之前,您將要執行的ClientTemplate複製。

+0

從此鏈接複製:http://www.componentart.com/community/forums/t/36556.aspx – 2012-01-21 09:58:43

0

我會做的是我會在用戶控件.ascx文件中定義組件藝術控件(以及模板)。然後,您可以使用這個小的代碼片段呈現此控件的字符串:

StringBuilder sb = new StringBuilder(); 
using (StringWriter sw = new StringWriter(sb)) 
using (HtmlTextWriter textWriter = new HtmlTextWriter(sw)) 
{ 
    theGrid.RenderControl(textWriter); 
} 

string gridContent = sb.ToString(); 

現在,你有HTML格式的網格的內容。從那裏開始,假設組件藝術具有良好的格式標記,您可以考慮將XML解析爲XML,並將單元格值拉到CSV中。

假設您希望允許用戶通過瀏覽器保存CSV文件,您可以實現一個自定義http處理程序,該處理程序呈現控件,創建CSV,然後將其作爲「text/csv」發送到瀏覽器中

+0

不幸的是,這是行不通的。這是我嘗試的第一件事,因爲那將是我如何導出一個常規電網控制。 – 2009-08-19 15:20:47

+0

您是否在說CA網格不能呈現正確的HTML?那麼也許你唯一的選擇是將網格模板中的變換複製到另一個更容易重現的區域 – 2009-08-19 18:22:48