1

ComponentArt グリッドのコンテンツをファイルにエクスポートする必要があります。できれば csv 形式でエクスポートする必要があります。

このタスクに最善のアプローチをする方法を誰かが知っているかどうか疑問に思っていました。現時点では、グリッドにデータが入力されており、クライアント テンプレートを使用して、ユーザーに表示される前にいくつかの小さな操作が実行されています。

適用されるテンプレートの例は次のとおりです。

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

したがって、グリッドがエクスポートされるとき、可能であれば表示される可能性のあるテンプレート化された変更を含む、エクスポートの時点でグリッドにあるものをファイルに含めたいと思います。

よろしくお願いいたします。

4

2 に答える 2

3

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.

于 2009-08-20T17:04:58.023 に答える
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 形式になりました。そこから、コンポーネント アートに適切な形式のマークアップがあると仮定すると、html を XML として解析し、セル値を引き出して CSV に変換することを検討できます。

ユーザーがブラウザー経由で CSV ファイルを保存できるようにしたい場合は、コントロールをレンダリングし、CSV を作成し、それを "text/csv" としてブラウザーに送信するカスタム http ハンドラーを実装するだけです。

于 2009-08-18T16:31:14.307 に答える