使用带 ExportOptions 类的 ExportToHttpResponse() 方法

要使用带 ExportOptions 参数的 ExportToHttpResponse() 方法,必须为网站完成在“向 Web 或 Windows 窗体添加控件”中所描述的过程。您不需要 ExportSetup()、ExportSelection() 和 ExportCompletion() 方法。所需代码放在 exportByType 单击事件方法中。

使用带 ExportOptions 参数的 ExportToHttpResponse() 方法

  1. 打开 Web 窗体。

  2. 从“视图”菜单中,单击“设计器”。

  3. 双击 exportByType Button 控件。

将会创建 exportByType\_Click() 事件方法,并且会看到“代码”视图。
  1. 在 exportByType_Click() 事件方法中,创建带有 Exception 类的 try/catch 块,该类作为名为“ex”的变量被引用。

    Try
    
    Catch ex As Exception
    
    End Try
    
    try
    {
    }
    catch (Exception ex)
    {
    }
    
  2. 在 try 块中,创建一个条件块,以测试来自 exportTypesList 的选定项是否等于 ExportFormatType.NoFormat。

    If (exportTypesList.SelectedIndex = ExportFormatType.NoFormat) Then
    
    Else
    
    End If
    
    if ((ExportFormatType)exportTypesList.SelectedIndex ==
    ExportFormatType.NoFormat)
    {
    }
    else
    {
    }
    
  3. 在 If 块中,把 message Label 控件的 Text 属性设置为 MessageConstants 类的 FORMAT_NOT_SUPPORTED 常量。

<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>当从 DropDownList 选中 NoFormat 值时,满足了 If 条件。</p></td>
</tr>
</tbody>
</table>

``` vb
message.Text = MessageConstants.FORMAT_NOT_SUPPORTED
```

``` csharp
message.Text = MessageConstants.FORMAT_NOT_SUPPORTED;
```
  1. 在 Else 块中,调用 hierarchicalGroupingReport 实例的 ExportToHttpResponse() 方法。传递选定的 ExportFormatType、ASP.NET Response 对象、布尔值 True 和文件名字符串作为方法参数。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>附件的文件名字符串不需要扩展名,因为扩展名会自动添加到导出的文件。</p></td>
</tr>
</tbody>
</table>

``` vb
hierarchicalGroupingReport.ExportToHttpResponse(exportTypesList.SelectedIndex,
Response, True, "ExportedReport")
```

``` csharp
hierarchicalGroupingReport.ExportToHttpResponse((ExportFormatType)exportTypesList.SelectedIndex,
Response, True, "ExportedReport");
```
  1. 在 catch 块中,把 message Label 控件的 Text 属性设置为 MessagesConstants 类的 FAILURE 常量,然后对其追加 Exception 参数的 Message 属性。

    message.Text = MessageConstants.FAILURE & ex.Message
    
    message.Text = MessageConstants.FAILURE + ex.Message;
    
  2. 在 try/catch 块外面,把 message Label 控件的 Visible 属性设置为“True”。

    message.Visible = True
    
    message.Visible = true;
    

现在即可生成并运行项目,将 Crystal 报表导出为不同格式。

要使用其它增强 API 方法,请单击相应链接跳到该节: