创建三个执行导出的方法
在本节中,将创建三个执行导出的私有帮助器方法。
- ExportSetup()
- ExportSelection()
- ExportCompletion()
在本教程稍后的部分中,将从按钮单击事件方法调用这些方法。首先,要创建 ExportSetup() 帮助器方法。
创建 ExportSetup() 方法
打开 Web 或 Windows 窗体。
从“视图”菜单中,单击“代码”。
在类的顶部,添加三个类声明。
``` vb
Private exportPath As String
Private myDiskFileDestinationOptions As DiskFileDestinationOptions
Private myExportOptions As ExportOptions
```
``` csharp
private string exportPath;
private DiskFileDestinationOptions diskFileDestinationOptions;
private ExportOptions exportOptions;
```
稍后将在 ExportSetup() 方法中实例化这些帮助器类。
在该类的底部,创建不带返回值的私有帮助器方法(名为 ExportSetup())。
Public Sub ExportSetup() End Sub
private void ExportSetup() { }
在该方法中,把 exportPath 字符串变量设置为硬盘的根目录。
``` vb
exportPath = "C:\Exported\"
```
``` csharp
exportPath = "C:\\Exported\\";
```
<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>如果要把 Exported 文件夹放在 Web 服务器的 Web 目录中,请用 Request.PhysicalApplicationPath 属性给文件夹名加上前缀。</p></td>
</tr>
</tbody>
</table>
创建一个条件块,测试 exportPath 字符串中的目录是否已经存在。
If Not System.IO.Directory.Exists(exportPath) Then End If
if (!System.IO.Directory.Exists(exportPath)) { }
在该条件块中,调用 System.IO.Directory 的 CreateDirectory() 方法以创建 exportPath 字符串中的目录。
``` vb
System.IO.Directory.CreateDirectory(exportPath)
```
``` csharp
System.IO.Directory.CreateDirectory(exportPath);
```
在条件块外面,实例化 DiskFileDesintationOptions 类。
myDiskFileDestinationOptions = New DiskFileDestinationOptions()
diskFileDestinationOptions = new DiskFileDestinationOptions();
用 hierarchicalGroupingReport 实例的 ExportOptions 属性填充 ExportOptions 实例。
``` vb
myExportOptions = hierarchicalGroupingReport.ExportOptions
```
``` csharp
exportOptions = hierarchicalGroupingReport.ExportOptions;
```
<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>在<a href="ms227453(v=vs.90).md">“项目设置”</a>的<a href="ms227530(v=vs.90).md">“附加设置要求”</a>中,您已实例化 hierarchicalGroupingReport 实例并将其绑定到 CrystalReportViewer 控件。</p></td>
</tr>
</tbody>
</table>
把 ExportOptions 实例的 ExportDestinationType 属性设置为枚举选项 ExportDestinationType.DiskFile。
myExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
exportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
对于 Windows 项目,清除 ExportOptions 实例的 ExportFormatOptions 属性中的值。(网站不需要这一行代码,因为在每次单击事件时都会自动清除变量。)
myExportOptions.ExportFormatOptions = Nothing
exportOptions.ExportFormatOptions = null;
现在已创建了 ExportSelection() 帮助器方法。
创建 ExportSelection() 方法
打开 Web 或 Windows 窗体。
从“视图”菜单中,单击“代码”。
在类的顶部,添加布尔声明,用于测试是否没有选择导出格式。
Private selectedNoFormat As Boolean = False
private bool selectedNoFormat = false;
在该类的底部,创建不带返回值的私有帮助器方法(名为 ExportSelection())。
``` vb
Public Sub ExportSelection()
End Sub
```
``` csharp
private void ExportSelection()
{
}
```
- 在该方法中,创建 "Select Case" [Visual Basic] 或 "switch" [C#] 语句,引用 ExportFormatType 枚举的成员。该枚举基于在前一过程中创建的 exportTypesList DropDownList 控件的 SelectedIndex。
``` vb
Select Case exportTypesList.SelectedIndex
Case ExportFormatType.NoFormat
Case ExportFormatType.CrystalReport
Case ExportFormatType.RichText
Case ExportFormatType.WordForWindows
Case ExportFormatType.Excel
Case ExportFormatType.PortableDocFormat
Case ExportFormatType.HTML32
Case ExportFormatType.HTML40
End Select
```
``` csharp
switch ((ExportFormatType)exportTypesList.SelectedIndex)
{
case ExportFormatType.NoFormat:
break;
case ExportFormatType.CrystalReport:
break;
case ExportFormatType.RichText:
break;
case ExportFormatType.WordForWindows:
break;
case ExportFormatType.Excel:
break;
case ExportFormatType.PortableDocFormat:
break;
case ExportFormatType.HTML32:
break;
case ExportFormatType.HTML40:
break;
}
```
现在已创建了 ExportCompletion() 帮助器方法。
创建 ExportCompletion() 方法
打开 Web 或 Windows 窗体。
从“视图”菜单中,单击“代码”。
在该类的底部,创建不带返回值的私有帮助器方法(名为 ExportCompletion())。
``` vb
Public Sub ExportCompletion()
End Sub
```
``` csharp
private void ExportCompletion()
{
}
```
在该方法中,创建带有 Exception 类的 try/catch 块,该类作为名为“ex”的变量被引用。
Try Catch ex As Exception End Try
try { } catch (Exception ex) { }
在 try 块中,创建一个条件块来测试布尔变量 selectedNoFormat。
If selectedNoFormat Then Else End If
if (selectedNoFormat) { } else { }
在 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>在<a href="ms227453(v=vs.90).md">“项目设置”</a>的<a href="ms227530(v=vs.90).md">“附加设置要求”</a>中,已创建了用于本教程的 MessageConstants 类。</p></td>
</tr>
</tbody>
</table>
``` vb
message.Text = MessageConstants.FORMAT_NOT_SUPPORTED
```
``` csharp
message.Text = MessageConstants.FORMAT_NOT_SUPPORTED;
```
- 在 Else 块中,调用 hierarchicalGroupingReport 实例的 Export() 方法。
``` vb
hierarchicalGroupingReport.Export()
```
``` csharp
hierarchicalGroupingReport.Export();
```
还是在 Else 块中,将 message Label 控件的 Text 属性设置为 MessageConstants 类的 SUCCESS 常量。
message.Text = MessageConstants.SUCCESS
message.Text = MessageConstants.SUCCESS;
在 catch 块中,把 message Label 控件的 Text 属性设置为 MessagesConstants 类的 FAILURE 常量,然后对其追加 Exception 参数的 Message 属性。
message.Text = MessageConstants.FAILURE & ex.Message
message.Text = MessageConstants.FAILURE + ex.Message;
在 try/catch 块外面,把 message Label 控件的 Visible 属性设置为“True”。
message.Visible = True
message.Visible = true;
对于 Windows 项目,请将 selectedNoFormat 布尔值变量重置为 false。(网站不需要这一行代码,因为在每次单击事件时该变量都会被自动重置为 False。)
selectedNoFormat = False
selectedNoFormat = false;
您已创建完三个执行导出的私有帮助器方法。