下面的示例演示了当报表被 导出为 HTML 时,如何使用 XRControl.HtmlItemCreated 事件来创建自定义 HTML 单元格。 下面的处理方法创建了两个 DevExpress.XtraPrinting.HtmlExport.Controls.DXHtmlTableCell 对象,用于在网页上显示一个复选框和一个超链接。

为了使此示例能正确工作,需执行下列操作。

  1. 启动 MS Visual Studio (2005、2008 或 2010),并且新建一个或者打开一个现有的 Windows 窗体应用程序

  2. 添加新报表 (命名为 XtraReport1) 到应用程序中。

  3. XRLabel 控件拖放到报表中,并以下列方式接管它的 HtmlItemCreated 事件:

    C#CopyCode image复制代码
    using DevExpress.XtraReports.UI;
    using DevExpress.XtraPrinting.HtmlExport.Controls;
    using DevExpress.XtraPrinting.HtmlExport.Native;
    // ...
    
    private void xrLabel1_HtmlItemCreated(object sender, HtmlEventArgs e) {
        // Clear the contents of the parent html row.
        DXHtmlTableRow parentRow = (DXHtmlTableRow)e.ContentCell.Parent;
        parentRow.Controls.Clear();
    
        // Create a check box cell.
        DXHtmlTableCell checkCell = new DXHtmlTableCell();
        DXHtmlGenericControl checkBox1 = new DXHtmlGenericControl(DXHtmlTextWriterTag.Input);
        checkBox1.Attributes["checked"] = "true";
        checkBox1.Attributes["type"] = "CheckBox";
        checkCell.Controls.Add(checkBox1);
    
        // Create a hyperlink cell.
        DXHtmlTableCell linkCell = new DXHtmlTableCell();
        DXHtmlAnchor hypLink = new DXHtmlAnchor();
        hypLink.InnerText = "XtraReports Web Page";
        hypLink.HRef = "http://www.devexpress.com/Products/NET/Reporting/index.xml";
        linkCell.Controls.Add(hypLink);
    
        // Add the created cells to the parent row.
        parentRow.Cells.Add(checkCell);
        parentRow.Cells.Add(linkCell);
    }
    
    Visual BasicCopyCode image复制代码
    Imports DevExpress.XtraReports.UI
    Imports DevExpress.XtraPrinting.HtmlExport.Controls
    Imports DevExpress.XtraPrinting.HtmlExport.Native
    ' ...
    
    Private Sub xrLabel1_HtmlItemCreated(ByVal sender As Object, ByVal e As HtmlEventArgs) _
    Handles xrLabel1.HtmlItemCreated
        ' Clear the contents of the parent html row.
        Dim parentRow As DXHtmlTableRow = CType(e.ContentCell.Parent, DXHtmlTableRow)
        parentRow.Controls.Clear()
    
        ' Create a check box cell.
        Dim checkCell As New DXHtmlTableCell()
        Dim checkBox1 As New DXHtmlGenericControl(DXHtmlTextWriterTag.Input)
        checkBox1.Attributes("checked") = "true"
        checkBox1.Attributes("type") = "CheckBox"
        checkCell.Controls.Add(checkBox1)
    
        ' Create a hyperlink cell.
        Dim linkCell As New DXHtmlTableCell()
        Dim hypLink As New DXHtmlAnchor()
        hypLink.InnerText = "XtraReports Web Page"
        hypLink.HRef = "http://www.devexpress.com/Products/NET/Reporting/index.xml"
        linkCell.Controls.Add(hypLink)
    
        ' Add the created cells to the parent row.
        parentRow.Cells.Add(checkCell)
        parentRow.Cells.Add(linkCell)
    End Sub
    

然后,可以使用下列代码或类似于 把报表导出为 HTML

C#CopyCode image复制代码
using System;
using System.Windows.Forms;
using System.Diagnostics;
// ...

private void button1_Click(object sender, EventArgs e) {
    XtraReport1 report = new XtraReport1();
    report.ExportToHtml("Test.html");
    StartProcess("Test.html");
}

public void StartProcess(string path) {
    Process process = new Process();
    try {
        process.StartInfo.FileName = path;
        process.Start();
        process.WaitForInputIdle();
    }
    catch { }
}
Visual BasicCopyCode image复制代码
Imports System
Imports System.Windows.Forms
Imports System.Diagnostics
' ...

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles button1.Click
    Dim report As New XtraReport1()
    report.ExportToHtml("Test.html")
    StartProcess("Test.html")
End Sub

Public Sub StartProcess(ByVal path As String)
    Dim process As New Process()
    Try
        process.StartInfo.FileName = path
        process.Start()
        process.WaitForInputIdle()
    Catch
    End Try
End Sub

CodeCentralShow Me

在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E89。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。

Expand image参阅