How to use components of the .NET Framework 3.0 to create and then to stream an Office Word 2007 document and an Office Excel 2007 workbook to a client computer

Article translations Article translations
Article ID: 932921 - View products that this article applies to.
Expand all | Collapse all

On This Page

INTRODUCTION

This article describes how to use components of the Microsoft .NET Framework 3.0 to perform the following actions:
  • Create a Microsoft Office Word 2007 document on a Web server
  • Create a Microsoft Office Excel 2007 workbook on the Web server
  • Stream the Word document and the Excel workbook from the Web server to a client computer
    This procedure uses ASP.NET and the Office Open XML Formats file format.
Note You do not have to install the Microsoft 2007 Office System or its components on the server to follow the steps that are described in the "More Information" section. However, you must have the .NET Framework 3.0 installed on the server. By default, the .NET Framework 3.0 is included with Windows Vista. However, if the server is running a different version of Windows, you may have to download and then install the .NET Framework 3.0. To obtain the .NET Framework 3.0, visit the following Microsoft Web site:
http://www.microsoft.com/downloads/details.aspx?FamilyId=10CC340B-F857-4A14-83F5-25634C3BF043
Additionally, you can use the 2007 Office system on the client computer to follow the steps that are described in the "More Information" section. However, you can also use an earlier version of the Office system. If the client computer has Microsoft Office 2003, Microsoft Office XP, or Microsoft Office 2000 installed, you must install the free Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats to follow the steps that are described in the "More Information" section. To obtain the Office Compatibility Pack, visit the following Microsoft Web site:
http://www.microsoft.com/downloads/details.aspx?FamilyID=941b3470-3ae9-4aee-8f43-c6bb74cd1466
The steps that are described in the "More Information" section use the files in the StreamingOfficeSample.exe sample package. You can download the StreamingOfficeSample.exe sample package from Microsoft. The following file is available for download from the Microsoft Download Center:
Collapse this imageExpand this image
Download
Download the StreamingOfficeSample.exe package now.
For more information about how to download Microsoft support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591 How to obtain Microsoft support files from online services
Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help prevent any unauthorized changes to the file.

MORE INFORMATION

The StreamingOfficeSample.exe sample package

The StreamingOfficeSample.exe sample package contains the following:
  • A Web form
  • The DocumentGenerator class library project
  • The WorkBookGenerator class library project
The Web form and the class library projects were created by using Microsoft Visual C# 2005.

The Web form

The Web form connects to the Northwind sample database that is included with Microsoft Office Access 2003.

Note You do not have to have Office Access 2003 installed to use the sample package.

The Web form uses data from the following tables to display an online order lookup form:
  • Customers
  • Orders
  • Order Details
  • Products
You can use the Web form to select a customer in a list. After you select the customer, the Web form filters the list of orders. The filtered list shows only orders for the selected customer. When you select an order in the list, the details for the order appear in the data grid control on the Web form. Then, you can click the buttons on the Web form to perform the following actions:
  • Create a report that is based on the selected data
    The code creates this report as a Word 2007 document and then streams the document to the client computer.
  • Export the order details data to a new Excel workbook
    The code creates an Excel 2007 workbook and then streams the workbook to the client computer.

The DocumentGenerator class library project

The code in the DocumentGenerator class library project uses the Packaging components in the WindowsBase.dll assembly to create the Word document. The WindowsBase.dll assembly is included with the .NET Framework 3.0.

This class library project contains the NwindTemplate.docx file as an embedded resource. The Packaging components in the WindowsBase.dll assembly read the contents of the NwindTemplate.docx file into a package object. The Packaging components use the methods in the clsCreateDocument class to perform the following actions:
  • Create a Word 2007 document in the temporary folder on the server
    This Word document is based on the NWindTemplate.docx file.
  • Add various parts and various data to the Word document from the Web form
When the Word document is complete, the code in the DocumentGenerator class library project streams the Word document to the client computer.

The WorkBookGenerator class library project

The code in the WorkBookGenerator class library project uses the Packaging components in the WindowsBase.dll assembly to perform the following actions:
  • Export the order details data to a new Excel workbook
    To do this, the Packaging components read the data into an Array list object.
  • Create an Excel workbook in the temporary folder on the server
    The Packaging components, together with the methods in the clsCreateWorkbook class, add the workbook parts and the data to the Excel workbook from the Web form.
When the Excel workbook is complete, the code in the WorkBookGenerator class library project streams the workbook to the client computer.

Test the sample

To test the sample, you will perform the following actions:
  • Add the sample project to the Web server
  • On a client computer, visit the sample project Web site that you created
  • Download the Word document report and the Excel workbook
To do this, follow these steps.

Add the sample project to the Web server

  1. Download the sample package file.
  2. Create the following folder on the Web server:
    C:\Inetpub\wwwroot\StreamingOffice2007Sample
  3. Extract the contents of the sample package file to the folder that you created in step 2.
  4. Use IIS Manager to create a virtual directory. To do this, follow these steps:
    1. Click Start, click Run, type inetmgr, and then click OK.
    2. Expand the server name, and then expand Web Sites.
    3. Right-click Default Web Site, point to New, and then click Virtual Directory. The Virtual Directory Creation Wizard starts.
    4. In the Virtual Directory Creation Wizard, click Next.
    5. In the Alias box, type an alias for the virtual directory. For example, type StreamingOffice2007Sample.
    6. Click Next.
    7. Click Browse, locate the folder that you created in step 2, and then click Next.
    8. Click to select the Read check box and the Run scripts (such as ASP) check box.
    9. Make sure that the other check boxes are cleared, and then click Next.
    10. Click Finish to complete the wizard.
For ASP content, you may want to confirm that an application was created. To do this, follow these steps:
  1. Right-click the virtual directory that you created in the previous steps, and then click Properties.
  2. On the Virtual Directory tab, make sure that the virtual directory alias is listed in the Application name box under Application Settings. If the virtual directory alias is not listed, click Create.

    Note The application name does not have to match the virtual directory alias that you typed in step 4e of the previous steps.
  3. Click the ASP.NET tab. Verify that ASP.NET version is set to 2.0.50727. If it is not, select 2.0.50727 in the ASP.NET version list, and then click Apply.

Visit the sample project Web site, and then download the Word document report and the Excel workbook

Note The client computer that you will use for the following steps must have one of the following installed:
  • Microsoft Office Word 2007 and Microsoft Office Excel 2007
  • Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats
On a client computer, follow these steps:
  1. Start a Web browser such as Microsoft Internet Explorer, and then visit the following Web site:
    http://ServerName/StreamingOffice2007Sample/default.aspx
    Note The placeholder ServerName represents the Web server.

    The Northwind Order Form page appears.
  2. In the Customer Name list, select a customer.
  3. Select an order ID. Notice that the order details for the order that you selected appear in the data grid control.
  4. Click Generate Word Report. The code in the DocumentGenerator class library project creates a Word 2007 document and then streams the document to the client computer.
  5. When the File Download dialog box appears, click Open to open the Report.docx file. The file opens in Word. The Word document contains an order summary for the selected order.
  6. Click Export to Excel. The code in the WorkBookGenerator class library project creates an Excel 2007 workbook and then streams the workbook to the client computer.
  7. When the File Download dialog box appears, click Open to open the Report.xlsx file. The file opens in Excel. The workbook contains a summary of the order detail information. The summary includes the total cost of the order.

Properties

Article ID: 932921 - Last Review: March 2, 2007 - Revision: 1.2
APPLIES TO
  • Microsoft .NET Framework 3.0
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Express Edition
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Office Word 2007
  • Microsoft Office Excel 2007
Keywords: 
kbexpertiseinter kbhowto KB932921

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com