ASP. NET2.0 export Word documents (C # export DOC)

Source: Internet
Author: User

I see a lot about ASP on the network. NET export DOC file examples, some simply export html pages directly as DOC files without any processing, but there will be many errors, such as displaying some controls as images. I have also seen a XX system created by Microsoft for a large Chinese company. The exported DOC file is actually XML in some special format, but I am not very familiar with this technology. So I collected data on the network, found many implementation methods, tested them one by one, and finally summed up the following experience.

1. First, configure the system environment:

1. In the command line (work sdk command line prompt), enter dcomcnfg to display the "component service" Manager.
2. Choose component service> Computer> my computer> DCOM configuration, find Microsoft Word documents, right-click, and select Properties"
3. In the "properties" dialog box, click the "Security" tab, select "Custom" in "Start and activate Permissions", and click "edit" on the right. in the displayed dialog box, add "ASPNET
"(Netword service in IIS6) user, give" Local start "and" Local activation "permissions, click" OK ", close" component SERVICE "Manager.
In this way, you can access the Word object on the Asp.net page.

2. modify WEB. CONFIG and add the following content to the <system. web> section: <identity impersonate = "true"/>

3. Add reference: website --> Add reference --> COM -- Microsoft Word 11.0 Object Library

4. Add a namespace reference,
Using Microsoft. Office. Interop. Word;

V. Code
Object Nothing = System. Reflection. Missing. Value;
// Obtain the path for saving the Word file
Object filename = "c: aa.doc ";
// Create a component object named WordApp
Microsoft. Office. Interop. Word. Application WordApp = new ApplicationClass ();
// You can directly use Word. Application WordApp = new ApplicationClass () in code writing on the network. However, I always encounter errors in actual writing.
// Create a document object named WordDoc
Microsoft. Office. Interop. Word. Document WordDoc = WordApp. Documents. Add (ref Nothing, ref Nothing );
// Add a table
Microsoft. Office. Interop. Word. Table table = WordDoc. Tables. Add (WordApp. Selection. Range, 3, 3, ref Nothing, ref Nothing );
// Add custom text content to the first cell of the table
Table. Cell (1, 1). Range. Text = "lllll ";
// Add text content in the blank area of the document
WordDoc. Paragraphs. Last. Range. Text = "Wellcome To Aspxcn. Com ";
// Save the content of the WordDoc object as a DOC object
WordDoc. SaveAs (ref filename, ref Nothing,
Ref Nothing, ref Nothing );
// Close the WordDoc Document Object
WordDoc. Close (ref Nothing, ref Nothing, ref Nothing );
// Close the WordApp Component Object
WordApp. Quit (ref Nothing, ref Nothing, ref Nothing );
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.