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 );