Operation word configuration instructions
Introduction: The object library file "MSWord. olb" of Word (Word 2000 is msword9.olb)
1.run dcomcnfg.exe
2. Component Service-computer-my computer-DCOM configuration-find the Microsoft Word Document
3. Click Properties
4. Select "security"
5. Select "use custom Access Permissions" and "use custom launch permissions"
6. Edit permissions respectively and add everyone (ASPnet, vs developers, debugger user)
7. Select "ID" and select "Interactive User ".
8. Add <identity impersonate = "true"/> to Web. config.
C #:
ASP. NET Word document operations have always been a topic of interest to everyone. In fact, it is not difficult to operate Word documents in ASP. NET. You only need to follow the prompts in this article to easily operate Word documents!
I. Preparations
First, confirm that the server has installed Office Word (Office XP is used as an example below). The operating system is Win2000 or XP and has been configured.. Net Running Environment and installation. net C # After the development environment, we can open. and create a Visual C # project> Asp. net web application at "http: // localhost/word ". (1)
2. Reference word object library files
To operate word, we need the word object library file "MSWord. olb (Word 2000 is msword9.olb). After Office Word is installed, you can find this file under the office 10 folder in the office installation directory, after we introduce this file to the project, we can use various operation functions in the source code to operate the word. The procedure is to open project> Add reference> browse in the menu bar, and find MSWord in the "select component" dialog box. after olb, click OK to introduce this object library file. vs.net will automatically convert the library file to the DLL component, so that we only need to create this component object in the source code to achieve the purpose of word operations!
Iii. webform1.aspx. CS code
MSWord. olb has been converted to the relevant DLL file and placed in the bin directory of the project, so we only need to create this object in the source code, you can use the built-in operation functions of the word library file to easily perform word and webform1.aspx operations. the Cs source code is as follows:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Namespace word
{
/// <Summary>
/// Summary of webform1.
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. textbox saveas;
Protected system. Web. UI. webcontrols. Button button;
Protected system. Web. UI. webcontrols. Label label2;
Protected system. Web. UI. webcontrols. Label label1;
Protected system. Web. UI. webcontrols. Label result;
Protected system. Web. UI. webcontrols. textbox wordtext;
# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Public void button_click (Object sender, system. eventargs E)
{
Object nothing = system. reflection. Missing. value;
// Obtain the path for saving the Word file
Object filename = @ saveas. text;
// Create a component object named wordapp
Word. Application wordapp = new word. applicationclass ();
// Create a document object named worddoc
Word. Document worddoc = wordapp. Documents. Add (ref nothing, ref nothing );
// Add a table
Word. Table = worddoc. Tables. Add (wordapp. selection. Range, 1, 1, ref nothing, ref nothing );
// Add custom text content to the first cell of the table
Table. Cell (1, 1). range. Text = wordtext. text;
// 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 );
// Return results
Result. TEXT = "document path: <a href ="/"+ saveas. text + "'>" + saveas. text + "</a> (click the link to View Details) <br/> result: Success! ";
}
Private void page_load (Object sender, system. eventargs E)
{
}
}
}
4. webform1.aspx code
After the Cs source code is complete, we can design the webform page. The complete code is as follows:
<% @ Page Language = "C #" codebehind = "webform1.aspx. cs" autoeventwireup = "false" inherits = "word. webform1" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> webforms-based operation word </title>
<Meta name = "generator" content = "Microsoft Visual Studio 7.0">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Font face = "">
<Asp: textbox id = "wordtext" style = "Z-INDEX: 101; left: 144px; position: absolute; top: 129px "runat =" server "Height =" pixel PX "width =" 360px "textmode =" multiline "> </ASP: textbox>
<Asp: textbox id = "saveas" style = "Z-INDEX: 102; left: 143px; position: absolute; top: 80px "runat =" server "width =" 360px "> C: \ myword.doc </ASP: textbox>
<Asp: button id = "button" style = "Z-INDEX: 103; left: 237px; position: absolute; top: 340px "runat =" server "width =" 98px "on onclick =" button_click "text =" generate WORD Documents "> </ASP: button>
<Input style = "Z-INDEX: 104; left: 361px; width: 49px; position: absolute; top: 340px; height: 24px "type =" reset "value =" Refill "size =" 20 "> </font>
<Font face = ""> webforms-Based Word (xiaobao. Net) </font>
<Asp: Label id = "label1" style = "Z-INDEX: 105; left: 143px; position: absolute; top: 54px "runat =" server "width =" 187px "Height =" 18px "> Word file storage path: </ASP: Label>
<Asp: Label id = "label2" style = "Z-INDEX: 106; left: 142px; position: absolute; top: export PX "runat =" server "width =" 159px "Height =" 12px "> Word file content: </ASP: Label>
<Asp: Label id = "result" style = "Z-INDEX: 107; left: 148px; position: absolute; top: 387px "runat =" server "width =" 352px "Height =" 18px "forecolor =" red "> </ASP: Label>
</Form>
</Body>
</Html>
5. Web. config settings
Web. in the config file, you also need to add <identity impersonate = "true"/> to enable simulated identity, because by default, the ASPNET user has no permission to access word. applicationclass (), when simulated identity is enabled, all pages will use the anonymous Internet User Account (IUSR_machinename) to execute the permission, so that we can successfully access word. applicationclass () and in ASP. net operations on word!