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 = 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, 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.