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
- 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.
The StreamingOfficeSample.exe sample packageThe StreamingOfficeSample.exe sample package contains the following:
- A Web form
- The DocumentGenerator class library project
- The WorkBookGenerator class library project
The Web formThe 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:
- Order Details
- 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 projectThe 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
The WorkBookGenerator class library projectThe 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.
Test the sampleTo 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
Add the sample project to the Web server
- Download the sample package file.
- Create the following folder on the Web server:C:\Inetpub\wwwroot\StreamingOffice2007Sample
- Extract the contents of the sample package file to the folder that you created in step 2.
- Use IIS Manager to create a virtual directory. To do this, follow these steps:
- Click Start, click Run, type inetmgr, and then click OK.
- Expand the server name, and then expand Web Sites.
- Right-click Default Web Site, point to New, and then click Virtual Directory. The Virtual Directory Creation Wizard starts.
- In the Virtual Directory Creation Wizard, click Next.
- In the Alias box, type an alias for the virtual directory. For example, type StreamingOffice2007Sample.
- Click Next.
- Click Browse, locate the folder that you created in step 2, and then click Next.
- Click to select the Read check box and the Run scripts (such as ASP) check box.
- Make sure that the other check boxes are cleared, and then click Next.
- Click Finish to complete the wizard.
- Right-click the virtual directory that you created in the previous steps, and then click Properties.
- 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.
- 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 workbookNote 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
- Start a Web browser such as Microsoft Internet Explorer, and then visit the following Web site:http://ServerName/StreamingOffice2007Sample/default.aspxNote The placeholder ServerName represents the Web server.
The Northwind Order Form page appears.
- In the Customer Name list, select a customer.
- Select an order ID. Notice that the order details for the order that you selected appear in the data grid control.
- 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.
- 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.
- 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.
- 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.
Id. de artículo: 932921 - Última revisión: 03/02/2007 00:07:14 - Revisión: 1.2
- kbexpertiseinter kbhowto KB932921