Erstellen von drei Methoden zur Ausführung des Exports

In diesem Abschnitt erstellen Sie die drei privaten Hilfsmethoden zur Ausführung des Exportvorgangs.

  • ExportSetup()
  • ExportSelection()
  • ExportCompletion()

Später in diesem Lernprogramm werden diese Methoden von der Click-Ereignismethode eines Button-Steuerelements aufgerufen. Zunächst erstellen Sie die ExportSetup()-Hilfsmethode.

So erstellen Sie die ExportSetup()-Methode

  1. Öffnen Sie das Web Form oder Windows Form.

  2. Klicken Sie im Menü Ansicht auf Code.

  3. Fügen Sie am Anfang der Klasse drei Klassendeklarationen hinzu.

``` vb
Private exportPath As String
Private myDiskFileDestinationOptions As DiskFileDestinationOptions
Private myExportOptions As ExportOptions
```

``` csharp
private string exportPath;
private DiskFileDestinationOptions diskFileDestinationOptions;
private ExportOptions exportOptions;
```

Später instantiieren Sie diese Hilfsklassen in der ExportSetup()-Methode.
  1. Erstellen Sie am Ende der Klasse die private Hilfsmethode ExportSetup() ohne Rückgabewert.

    Public Sub ExportSetup()
    
    End Sub
    
    private void ExportSetup()
    {
    }
    
  2. Setzen Sie die Zeichenfolgenvariable exportPath innerhalb der Methode auf das Stammverzeichnis der Festplatte.

``` vb
exportPath = "C:\Exported\"
```

``` csharp
exportPath = "C:\\Exported\\";
```

<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images\xkh1wxd8.alert_note(de-de,VS.90).gif" alt="Note" class="note" />Anmerkung</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>Wenn Sie den Ordner für exportierte Berichte innerhalb des Webverzeichnisses Ihres Webservers erstellen möchten, stellen Sie dem Ordnernamen die Request.PhysicalApplicationPath-Eigenschaft voran.</p></td>
</tr>
</tbody>
</table>
  1. Erstellen Sie einen Bedingungsblock, durch den getestet wird, ob das Verzeichnis in der exportPath-Zeichenfolge bereits vorhanden ist.

    If Not System.IO.Directory.Exists(exportPath) Then
    
    End If
    
    if (!System.IO.Directory.Exists(exportPath))
    {
    }
    
  2. Rufen Sie innerhalb des Bedingungsblocks die CreateDirectory()-Methode von System.IO.Directory auf, um das Verzeichnis in der exportPath-Zeichenfolge zu erstellen.

``` vb
System.IO.Directory.CreateDirectory(exportPath)
```

``` csharp
System.IO.Directory.CreateDirectory(exportPath);
```
  1. Instantiieren Sie außerhalb des Bedingungsblocks die DiskFileDesintationOptions-Klasse.

    myDiskFileDestinationOptions = New DiskFileDestinationOptions()
    
    diskFileDestinationOptions = new DiskFileDestinationOptions();
    
  2. Füllen Sie die ExportOptions-Instanz mit der ExportOptions-Eigenschaft der hierarchicalGroupingReport-Instanz.

``` vb
myExportOptions = hierarchicalGroupingReport.ExportOptions
```

``` csharp
exportOptions = hierarchicalGroupingReport.ExportOptions;
```

<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images\xkh1wxd8.alert_note(de-de,VS.90).gif" alt="Note" class="note" />Anmerkung</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>Unter <a href="https://msdn.microsoft.com/de-de/library/vs%7ccrystlmn%7c%7e%5chtml%5ctopic179.htm(v=VS.90)">Zusätzliche Voraussetzungen für das Setup</a> in <a href="https://msdn.microsoft.com/de-de/library/vs%7ccrystlmn%7c%7e%5chtml%5ctopic168.htm(v=VS.90)">Projekt-Setup</a> haben Sie eine Instantiierung ausgeführt und die hierarchicalGroupingReport-Instanz an das CrystalReportViewer-Steuerelement gebunden.</p></td>
</tr>
</tbody>
</table>
  1. Setzen Sie die ExportDestinationType-Eigenschaft der ExportOptions-Instanz auf den Enumerationswert ExportDestinationType.DiskFile.

    myExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
    
    exportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
    
  2. Löschen Sie bei einem Windows-Projekt die Werte in der ExportFormatOptions-Eigenschaft der ExportOptions-Instanz. (Diese Codezeile ist für eine Website nicht erforderlich, da die Variable bei jedem Click-Ereignis automatisch gelöscht wird.)

    myExportOptions.ExportFormatOptions = Nothing
    
    exportOptions.ExportFormatOptions = null;
    

Jetzt erstellen Sie die ExportSelection()-Hilfsmethode.

So erstellen Sie die ExportSelection()-Methode

  1. Öffnen Sie das Web Form oder Windows Form.

  2. Klicken Sie im Menü Ansicht auf Code.

  3. Fügen Sie am Anfang der Klasse eine Boolean-Deklaration hinzu, durch die getestet wird, ob kein Exportformat ausgewählt wurde.

    Private selectedNoFormat As Boolean = False
    
    private bool selectedNoFormat = false;
    
  4. Erstellen Sie am Ende der Klasse die private Hilfsmethode ExportSelection() ohne Rückgabewert.

``` vb
Public Sub ExportSelection()

End Sub
```

``` csharp
private void ExportSelection()
{
}
```
  1. Erstellen Sie innerhalb der Methode die Anweisung "Select Case" [Visual Basic] oder "switch" [C#], die auf die Elemente der ExportFormatType-Enumeration verweist. Die Enumeration basiert auf dem SelectedIndex des in der vorherigen Prozedur erstellten DropDownList-Steuerelements exportTypesList.
``` 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;
}
```

Jetzt erstellen Sie die ExportCompletion()-Hilfsmethode.

So erstellen Sie die ExportCompletion()-Methode

  1. Öffnen Sie das Web Form oder Windows Form.

  2. Klicken Sie im Menü Ansicht auf Code.

  3. Erstellen Sie am Ende der Klasse die private Hilfsmethode ExportCompletion() ohne Rückgabewert.

``` vb
Public Sub ExportCompletion()

End Sub
```

``` csharp
private void ExportCompletion()
{
}
```
  1. Erstellen Sie innerhalb der Methode einen try/catch-Block mit der Exception-Klasse, auf die als Variable "ex" Bezug genommen wird.

    Try
    
    Catch ex As Exception
    
    End Try
    
    try
    {
    }
    catch (Exception ex)
    {
    }
    
  2. Erstellen Sie innerhalb des try-Blocks einen Bedingungsblock, um die boolesche Variable selectedNoFormat zu testen.

    If selectedNoFormat Then
    
    Else
    
    End If
    
    if (selectedNoFormat)
    {
    }
    else
    {
    }
    
  3. Setzen Sie die Text-Eigenschaft des Label-Steuerelements message innerhalb des If-Blocks auf die FORMAT_NOT_SUPPORTED-Konstante der MessageConstants-Klasse.

<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images\xkh1wxd8.alert_note(de-de,VS.90).gif" alt="Note" class="note" />Anmerkung</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>Unter <a href="https://msdn.microsoft.com/de-de/library/vs%7ccrystlmn%7c%7e%5chtml%5ctopic179.htm(v=VS.90)">Zusätzliche Voraussetzungen für das Setup</a> in <a href="https://msdn.microsoft.com/de-de/library/vs%7ccrystlmn%7c%7e%5chtml%5ctopic168.htm(v=VS.90)">Projekt-Setup</a> haben Sie die MessageConstants-Klasse für dieses Lernprogramm erstellt.</p></td>
</tr>
</tbody>
</table>

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

``` csharp
message.Text = MessageConstants.FORMAT_NOT_SUPPORTED;
```
  1. Rufen Sie innerhalb des Else-Blocks die Export()-Methode der hierarchicalGroupingReport-Instanz auf.
``` vb
hierarchicalGroupingReport.Export()
```

``` csharp
hierarchicalGroupingReport.Export();
```
  1. Setzen Sie die Text-Eigenschaft des Label-Steuerelements message, während Sie sich im else-Block befinden, auf die SUCCESS-Konstante der MessageConstants-Klasse.

    message.Text = MessageConstants.SUCCESS
    
    message.Text = MessageConstants.SUCCESS;
    
  2. Setzen Sie die Text-Eigenschaft des Label-Steuerelements message innerhalb des catch-Blocks auf die FAILURE-Konstante der MessagesConstants-Klasse, und fügen Sie dann die Message-Eigenschaft des Exception-Parameters an sie an.

    message.Text = MessageConstants.FAILURE & ex.Message
    
    message.Text = MessageConstants.FAILURE + ex.Message;
    
  3. Setzen Sie die Visible-Eigenschaft des Label-Steuerelements message außerhalb des try/catch-Blocks auf "True".

    message.Visible = True
    
    message.Visible = true;
    
  4. Setzen Sie die boolesche Variable selectedNoFormat bei einem Windows-Projekt auf "false" zurück. (Diese Codezeile ist für eine Website nicht erforderlich, da die Variable bei jedem Click-Ereignis automatisch auf "False" zurückgesetzt wird.)

    selectedNoFormat = False
    
    selectedNoFormat = false;
    

Sie haben jetzt die drei privaten Hilfsmethoden zur Ausführung des Exportvorgangs erstellt.