Use Microsoft Word documentation in ASP.NET

xiaoxiao2021-03-06  111

Using Microsoft Word Document in ASP.NET [Source] http://www.codeproject.com/UserItems/WordApplication.asp [Author] Michela [Download this article] Project Size: 309 KB [Introduction] This article It should be written in the ASP.NET to create a Microsoft Word document. This article demonstrates how to create and modify Microsoft Word documents in ASP.NET. [Background] Automation is a (e.g., a Visual Basic.NET or C #) application written in a variety of languages ​​to control other applications at the program level. For Word's automation allows you to perform such a new document, add text, mail merge, and formatted documents to your document. In Word and other Microsoft Office programs, those visual operations performed through the user interface can also be implemented by automation of the program level. Word provides an interface with an object-operable functionality through an object model. The object model is a set of sets and methods, these classes and methods correspond to the logical components of Word. For example, he might be an application object, a document object, a paragraph object, and each object contains the functionality of the Word component. [Establishment Project] The first step in the .NET is to add COM reference to your project, click REFERENCE, ADD REFERENCE. Select the COM tab and find Microsoft Word 10.0 Object Library. Click to select, OK. This will automatically add an assembly package with Word's COM to the application directory. Now you can build a Word instance:

Word.ApplicationClass OwordApp = New Word.ApplicationClass (); You can call Word to provide you with methods and properties to manipulate the Word document. Learn how to use Word, Excel, PowerPoint's object model is the best way to use Macro Recorder in these Office applications: 1. Select Record New Macro in the macro option of the Tools menu, and perform tasks you are interested. 2. Select Stop Recording in the Macro option of the Tools menu. 3. If you have a record, select the macros in the macro option in the Tools menu, find the macro you record, you can edit it. The above operation produces VBA code to complete the task you record. It should be noted that macros is not the best code in most cases, but it provides a convenient and available approach. The following example opens and adds a writing text:

object fileName = "c: //database//test.doc"; object readOnly = false; object isVisible = true; object missing = System.Reflection.Missing.Value; Word.ApplicationClass oWordApp = new Word.ApplicationClass (); Word .Document oWordDoc = oWordApp.Documents.Open (ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref Missing, ref missiVate (); owordapp.selection.typetext ("this is the text"); OWORDAPP.SELECTION.TYPEPARAGRAGRAPH () ;OWORDDOC.SAVE (); OWORDAPP.Application.quit (Ref missing, ref Missing, Ref missing; if you create a new document and save this: word.applicationclass owordapp = new word.applicationclass (); word.document =OWordapp.Documents.add (Ref missing, ref missings.add (Ref missing, ref missing, ref missing, Ref missing); OWordDoc.actiVate (); OWORDAPP.SELECTION.TYPETEXT ("this is the text"); OWORDDDOC.SAVEAS ("o sddd C: //myfile.doc "); OWORDAPP.Application.quit (Ref missing, ref missing, ref missing); In C #, the Word document class open method is defined: Open (Ref Object, Ref Object, REF Object, Ref Object, Ref Object, Ref Object, Ref Object, Ref Object, Ref Object, Ref Object, Ref Object, Ref Object, Ref Object, REF Object, Ref Object. 15 parameters are required in the open method in the C #, and each parameter must be described by the REF keyword, and is the object type. The first parameter is the file, name, usually a string in Visual Basic.NET, but in C #, it must be an object containing String, the code is like this:

Object filename = "c: //database/test.doc"; although we only need to use the first parameter of the Open method, C # does not allow the default parameters to be used, so we give it 14 Object types: System. Reflection.Missing.Value [Use "If you need to automatically build a document with universal format, you can use a pre-formatted touch version to create a new document, which can be more convenient. Using the touch version in Word instead of establishing an empty document has two obvious advantages: 1. You can format the document and the object in the documentation in a greater extent. 2. Can establish a document with less code. By using the touch version, you can adjust the table, paragraphs, and some other locations in the document, while including formatting these objects. By using an automation process, you can create a touch-based document, the code is as follows: Word.ApplicationClass OwordApp = new word.applicationclass (); object odemplate = "c: //mytemplate.dot"; OWORDDOC = Owordapp.documents.add (Ref Otemplate, Ref Missing, Ref missing, ref missing); In your tap, you can define some marks, automation processing will populate the text to these locations, as follows:

Object Obookmark = "MyBookmark"; OWORDDOC.BOOKMARKS.ITEM (Ref Obookmark) .Range.text = "Some text Here"; another advantage of using the touch is that you can create and save those formats you want during the running process. The style is as follows:

Object ostylename = "MyStyle"; OWORDDOC.BOOKMARKS.ITEM (REF OBOOKMARK) .Range.Set_Style (Ref OSTYLENAME); [Use ccWordApp class] In the project, I contain ccWordApp.cs file, I don't want to always insert text Open the code such as a document. So, I decided to package some of the most important features into the ccWordApp class. The following code briefly describes this class and his features:

public class CCWordApp {// it's a reference to the COM object of Microsoft Word Application private Word.ApplicationClass oWordApplic; // it's a reference to the document in use private Word.Document oWordDoc; // Activate the interface with the COM object of Microsoft Word public CCWordApp (); // Open an existing file or open a new file based on a template public void Open (string strFileName); // Open a new document public void Open (); // Deactivate the interface with the COM object of Microsoft Word public void Quit (); // Save the document public void Save (); // Save the document with a new name as HTML document public void SaveAs (string strFileName); // Save the document in HTML format public void SaveAsHtml (string strFileName); // Insert Text public void InsertText (string strText); // Insert Line Break public void InsertLineBreak (); // Insert multiple Line Break public void InsertLineBreak (int NLINE); // set the paragraph alignment // Possible Values ​​of Strtype: "Centre", "Right", "Left", "Justify" public void setAlignment (String start "; // set the font style // Possible Values ​​of Strtype: "Bold", "Italic" "underlined" public void setfont (string strading); // disable all the style public void setfont (); // set the font name public void setfontname (string strading); // set the Font Dimension Public Void SetFontSIZE (int nsize); // INSERT A page break public void insertpagebreak (); // Go to a predefined Bookmark Public Void gotobookmark (String StrBookmarkname);

// go to the end of document public void gotothend (); // Go to the beginning of document public void gotothebeginning (); Open an existing file will be like this: ccWordApp test; test = new ccWordApp (); Test.Open ("C: //Database/test.doc"); Test.insertText ("this is the text"); test.insertlinebreak; test.save (); test.quit (); [details] demonstration The project includes: ccWordapp.cs - The class CREATEDOCMODEL.ASPX used above - establishes an example of a new document based on the bookmark. CreateNewDoc.aspx - Create a new document and add a write text. ModifyDocument.aspx - Open an existing document and add some text at the end. Template / Template1.dot - An example of the touch version (Used in CreatedOCModel.aspx) Note that you can save your document, it should be rewritable. You can modify this path in Web.config.

转载请注明原文地址:https://www.9cbs.com/read-126301.html

New Post(0)