【问题标题】:ComponentArt: Exporting the Grid DataComponentArt:导出网格数据
【发布时间】:2010-11-19 08:11:30
【问题描述】:

我需要将 ComponentArt Grid 中的内容导出到文件中,最好是 csv 格式的 excel。

我想知道是否有人知道如何最好地完成这项任务。目前,我们在网格中填充了数据,并使用客户端模板在向用户显示之前执行了一些小的操作。

应用的模板示例如下:

<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" />

因此,当导出网格时,我希望文件包含导出时网格中的内容,包括任何可能可见的模板化更改。

提前感谢您的帮助。

【问题讨论】:

    标签: excel templates grid export componentart


    【解决方案1】:

    您不能直接使用 RenderControl 将 Web.UI Grid 导出到文本编写器中。相反,您应该将其数据转换为另一种形式(例如 ASP DataGrid)并导出。

    这背后的原因是 Web.UI Grid 的结构是在客户端上动态构建的——它作为一堆 XML 和数组数据从服务器传递到客户端,然后转换为您在其中看到的表格和 div窗户。

    因此,您需要在 Grid 的数据源上执行导出,然后分析客户端模板以复制它们在客户端上动态构建时会更改的内容。

    以下是如何将 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 复制。

    【讨论】:

    【解决方案2】:

    我要做的是在用户控件 .ascx 文件中定义组件艺术控件(连同模板)。然后,您可以使用以下小代码 sn-p 将此控件呈现为字符串:

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

    现在,您有了 HTML 格式的网格内容。从那里开始,假设组件艺术具有良好的格式良好的标记,您可以考虑将 html 解析为 XML,然后将单元格值提取出来以转换为 CSV。

    假设您希望允许用户通过浏览器保存 CSV 文件,您只需实现一个自定义的 http 处理程序来呈现控件,创建 CSV,然后将其作为“text/csv”发送到浏览器

    【讨论】:

    • 遗憾的是这不起作用。这是我尝试的第一件事,因为这将是我导出常规网格控件的方式。
    • 你是说 CA 网格没有呈现正确的 HTML 吗?那么也许您唯一的选择是将网格模板中的转换复制到另一个更容易重现的区域
    猜你喜欢
    • 2019-03-29
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多