Konstruktor ServerDocument — (array<Byte , String)

Inicjuje nowe wystąpienie ServerDocument klasy przy użyciu tablicy bajtowej, która reprezentuje dokument do załadowania i rozszerzenie nazwy pliku dokumentu.

Przestrzeń nazw:  Microsoft.VisualStudio.Tools.Applications
Zestaw:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (w Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

Składnia

'Deklaracja
Public Sub New ( _
    bytes As Byte(), _
    fileType As String _
)
public ServerDocument(
    byte[] bytes,
    string fileType
)

Parametry

  • bytes
    Typ: array<System.Byte[]
    Tablica bajtów reprezentujący dokumentu do załadowania.
  • fileType
    Typ: System.String
    Rozszerzenie nazwy pliku dokumentu, który jest przechowywany w bytes parametr, poprzedzoną kropką (.)— na przykład "xlsx" lub ".docx".

Wyjątki

Wyjątek Warunek
ArgumentNullException

bytes Jest parametr nullodwołanie o wartości null (Nothing w języku Visual Basic) lub jest pusty.

-lub-

fileType Jest parametr nullodwołanie o wartości null (Nothing w języku Visual Basic) lub pusty lub zawiera tylko znaki odstępu.

UnknownCustomizationFileException

fileType Parametr określa rozszerzenie nazwy pliku, który nie jest obsługiwany przez Visual Studio 2010 Tools for Office Runtime.

DocumentCustomizedWithPreviousRuntimeException

W pliku określonym przez documentPath ma dostosowania, który nie został utworzony z programu Visual Studio 2010 Tools dla środowiska Office Runtime lub Visual Studio Tools dla pakietu Microsoft Office system (wersja 3.0 Runtime).

Uwagi

Ten konstruktor umożliwia dostęp do pamięci podręcznej danych lub wdrożenia manifestu informacji w dokumencie, który jest już w pamięci.Korzystając z tego konstruktora, dokument zostanie otwarty z dostępem do odczytu i zapisu.

fileType Parametr jest używany tylko do określenia typu dokumentu, który jest przechowywany w tablicy bajtów.Wartość fileType jest mapowany do jednego z typów plików, które są obsługiwane dla dostosowania poziomu dokumentu.Nie jest podejmowana próba otwarcia pliku.Opcjonalnie można przekazywać w pełnej nazwy pliku (na przykład "Workbook1.xlsx"), ale jeśli to zrobisz, rozszerzenie nazwy pliku jest używany.Aby uzyskać więcej informacji na temat obsługiwanych typów plików, zobacz Architektura dostosowywania na poziomie dokumentu.

Aby uzyskać dostęp do tablicy bajtowej dla dokumentu, po wywołaniu ten konstruktor, użyj Document właściwość.

Przykłady

Następujący kod w przykładzie wykorzystano ServerDocument(array<Byte[], String) Konstruktor, aby utworzyć nową ServerDocument z tablicy bajtowej, który zawiera skoroszyt programu Excel z rozszerzeniem nazwy pliku xlsx.Następnie w przykładzie Document właściwość, aby wyświetlić liczbę bajtów w dokumencie.

W tym przykładzie wymaga:

  • Projekt aplikacji konsoli lub innych projektów nienależących do pakietu Office.

  • Odwołania do następujących zestawów:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll i Microsoft.VisualStudio.Tools.Applications.Runtime.dll (jeśli cele projektu .NET Framework 4 lub .NET Framework 4.5).

      lub

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll i Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (Jeśli projekt jest przeznaczony dla.NET Framework 3.5).

  • Imports(dla Visual Basic) lub using (w przypadku C#) oświadczenia dla Microsoft.VisualStudio.Tools.Applications i Microsoft.VisualStudio.Tools.Applications.Runtime obszarów nazw u góry pliku kodu.

Private Sub CreateServerDocumentFromByteArray(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing
    Dim stream As System.IO.FileStream = Nothing

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion = 3 Then
            ' Read the file into a byte array.
            stream = New System.IO.FileStream(documentPath, System.IO.FileMode.Open, _
                System.IO.FileAccess.Read)
            Dim buffer(Fix(stream.Length)) As Byte
            stream.Read(buffer, 0, Fix(buffer.Length))

            ' Display the number of bytes in the document.
            serverDocument1 = New ServerDocument(buffer, "*.xlsx")
            MessageBox.Show("The Document property contains " & _
                serverDocument1.Document.Length.ToString() & " bytes.")
        End If

    Catch ex As System.IO.FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As UnknownCustomizationFileException
        System.Windows.Forms.MessageBox.Show("The specified document has a file " & _
            "extension that is not supported by Visual Studio Tools for Office.")
    Finally
        If Not (serverDocument1 Is Nothing) Then
            serverDocument1.Close()
        End If
        If Not (stream Is Nothing) Then
            stream.Close()
        End If
    End Try
End Sub
private void CreateServerDocumentFromByteArray(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;
    System.IO.FileStream stream = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
        if (runtimeVersion == 3)
        {
            // Read the file into a byte array.
            stream = new System.IO.FileStream(
                documentPath, System.IO.FileMode.Open,
                System.IO.FileAccess.Read);
            byte[] buffer = new byte[(int)stream.Length];
            stream.Read(buffer, 0, (int)buffer.Length);

            // Display the number of bytes in the document.
            serverDocument1 = new ServerDocument(buffer,
                "*.xlsx");
            MessageBox.Show("The Document property contains " +
                serverDocument1.Document.Length.ToString() +
                " bytes.");
        }
    }
    catch (System.IO.FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (UnknownCustomizationFileException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document has a file " +
            "extension that is not supported by Visual Studio Tools for Office.");
    }
    finally
    {
        if (serverDocument1 != null)
            serverDocument1.Close();
        if (stream != null)
            stream.Close();
    }
}

Zabezpieczenia programu .NET Framework

Zobacz też

Informacje

ServerDocument Klasa

Przeciążenie ServerDocument

Przestrzeń nazw Microsoft.VisualStudio.Tools.Applications