ComponentArt:导出网格数据

发布于 2024-07-30 12:31:16 字数 759 浏览 4 评论 0原文

我需要将 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" />

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

预先感谢您的帮助。

I have a need to export the contents in a ComponentArt Grid into a file, preferably excel in csv format.

I was wondering if anyone had any ideas how to best approach this task. At the moment we have the grid being populated with data, and using client templating some minor manipulation is being performed before it is displayed to the user.

An example of a template that is applied is:

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

So when the grid is exported I would like the file to contain what is in the grid at the moment of export including any templated changes that may be visible if possible.

Thank you in advance for your assistance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮坠入山谷 2024-08-06 12:31:16

您无法使用 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 复制。

You cannot export the Web.UI Grid directly using RenderControl into a text writer. Instead, you should convert its data into another form (such as the ASP DataGrid) and export that instead.

The reason behind this is that the Web.UI Grid's structure is build dynamically on the client -- it's passed from server to client as a bunch of XML and array data, which is converted into the tables and divs that you see in the window.

As a result you'll want to perform the export on the Grid's datasource, and then analyze the client templates to replicate what they would change when built dynamically on the client.

The following is a short example of how to export the CA grid it into 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();
}

So before binding the datasource of the GridView you will want to perform the ClientTemplate replication.

寂寞美少年 2024-08-06 12:31:16

我要做的是在用户控件 .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 格式的网格内容。 从那里开始,假设组件艺术具有良好的格式良好的标记,您可以考虑将 html 解析为 XML,并将单元格值拉出以转换为 CSV。

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

What I'd do is that I would define the component art control (along with templates) in a user control .ascx file. You can then render this control to a string using this little code snippet:

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

string gridContent = sb.ToString();

Now, you have the contents of the grid in HTML format. From there, assuming component art has good well formed markup, you could consider parsing the html as XML, and pulling the cell values out to turn into your CSV.

Assuming you want to allow the user to save the CSV file via a browser, you can just implement a custom http handler that renders the control, creates the CSV, then send that to the browser as "text/csv"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文