Delen via


How to: Insert Data in Documents Without Writing to Disk

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

Microsoft Office version

  • 2007 Microsoft Office system

  • Microsoft Office 2003

For more information, see Features Available by Application and Project Type.

You can insert data into a Visual Studio Tools for Office solution document in memory, so that data is not written to the hard disk. If you need to send a document to a user as a byte array using the HTTP protocol, you can use this feature to modify data directly in the byte array, instead of creating a temporary file in which to modify the data.

To insert data in a document

  1. Load the document into memory as a byte array.

    Dim name As String = "C:\Documents\WordApplication3.doc" 
    Dim fileStream As System.IO.FileStream = Nothing 
    Dim bytes() As Byte = Nothing 
    
    Try
        fileStream = New System.IO.FileStream( _
            name, System.IO.FileMode.Open, System.IO.FileAccess.Read)
        ReDim bytes(fileStream.Length)
        fileStream.Read(bytes, 0, fileStream.Length)
    
    Finally 
        If Not fileStream Is Nothing Then
            fileStream.Close()
        End If 
    End Try
    
    string name = @"C:\Documents\WordApplication3.doc";
    System.IO.FileStream fileStream = null;
    byte[] bytes = null;
    
    try
    {
        fileStream = new System.IO.FileStream(
            name, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        bytes = new byte[(int)fileStream.Length];
        fileStream.Read(bytes, 0, (int)fileStream.Length);
    }
    finally
    {
        if (fileStream != null)
        {
            fileStream.Close();
        }
    }
    
  2. Pass the byte array to the server-side object model instead of a file name, and then perform your data manipulation.

    Dim sd1 As ServerDocument = Nothing 
    Try
        sd1 = New ServerDocument(bytes, name)
    
        ' Your data manipulation code goes here. 
    
        sd1.Save()
    
    ServerDocument sd1 = null;
    try
    {
        sd1 = new ServerDocument(bytes, name);
    
        // Your data manipulation code goes here. 
    
        sd1.Save();
    
  3. Send the document to the end user and close the ServerDocument.

        ' If you have a Word document, use the MIME string:
        Response.ContentType = "application/msword" 
    
        ' If you have an Excel workbook, use the MIME string: 
        'Response.ContentType = "application/vnd.ms-excel"
    
        Response.AddHeader("Content-disposition", "filename=" + name)
        Response.BinaryWrite(sd1.Document)
    
    Finally 
        If Not sd1 Is Nothing Then
            sd1.Close()
        End If 
    End Try
    
    // If you have a Word document, use the MIME string:
        Response.ContentType = "application/msword";
    
        // If you have an Excel workbook, use the MIME string: 
        //Response.ContentType = "application/vnd.ms-excel";
    
        Response.AddHeader("Content-disposition", "filename=" + name);
        Response.BinaryWrite(sd1.Document);
    }
    finally
    {
        if (sd1 != null)
        {
            sd1.Close();
        }
    }
    

Compiling the Code

This example requires:

  • An ASP.NET project that contains the example code.

  • A Microsoft Office Word document named WordApplication3.doc that has a data cache, and is located in the folder C:\Documents.

Setting up the ASP.NET Project

The ASP.NET project must have a reference to one of the following assemblies:

  • For Word 2007, add a reference to Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll.

  • For Word 2003, add a reference to Microsoft.VisualStudio.Tools.Applications.Runtime.dll.

The code file into which you copy the code example must have an Imports (in Visual Basic) or using (in C#) statement for one of the following namespaces:

See Also

Tasks

How to: Insert Data into a Workbook on a Server

How to: Retrieve Cached Data from a Workbook on a Server

How to: Change Cached Data in a Workbook on a Server

Concepts

Accessing Data in Documents on the Server