C # create word, operate, read/write

Source: Internet
Author: User

To use C # To operate on word, add reference first:

1. Add reference-> COM-> Microsoft Word 11.0 Object Library

2. Add the. cs File

Using Word;
The following example contains C # operations such as creating, inserting tables, and setting styles for Word documents:

(In this example, some part of the code that involves data information is omitted. It is important to introduce some methods for C # To operate Word documents)

Public string CreateWordFile (string CheckedInfo) {string message = ""; try {Object Nothing = System. reflection. missing. value; Directory. createDirectory ("C:/CNSI"); // create the directory where the file is located string name = "CNSI _" + DateTime. now. tow.string () + ". doc "; object filename =" C: // CNSI // "+ name; // file storage path // create a Word document Word. application WordApp = new Word. applicationClass (); Word. document WordDoc = WordApp. documents. add (re F Nothing, ref Nothing); // Add the header WordApp. activeWindow. view. type = WdViewType. wdOutlineView; WordApp. activeWindow. view. seekView = WdSeekView. wdSeekPrimaryHeader; WordApp. activeWindow. activePane. selection. insertAfter ("[header content]"); WordApp. selection. paragraphFormat. alignment = Word. wdParagraphAlignment. wdAlignParagraphRight; // set the right-aligned WordApp. activeWindow. view. seekView = WdSeekView. WdSeekMainDocument; // jump out of the header and set WordApp. selection. paragraphFormat. lineSpacing = 15f; // set the line spacing of the document // move the focus and wrap the object count = 14; object WdLine = Word. wdUnits. wdLine; // change the line; WordApp. selection. moveDown (ref WdLine, ref count, ref Nothing); // move the focus WordApp. selection. typeParagraph (); // insert a paragraph // create a table Word in the document. table newTable = WordDoc. tables. add (WordApp. selection. range, 12, 3, ref Nothing, ref Nothing); // sets the table style newTab. Le. borders. outsideLineStyle = Word. wdLineStyle. wdLineStyleThickThinLargeGap; newTable. borders. insideLineStyle = Word. wdLineStyle. wdLineStyleSingle; newTable. columns [1]. width = 100f; newTable. columns [2]. width = 220f; newTable. columns [3]. width = 105f; // fill in the table content newTable. cell (1, 1 ). range. text = "product details table"; newTable. cell (1, 1 ). range. bold = 2; // set the font in the cell to Bold. // merge the newTable cells. cell (1, 1 ). merge (newTable. cell (1, 3); WordApp. selection. cells. verticalAlignment = Word. wdCellVerticalAlignment. wdCellAlignVerticalCenter; // vertical center WordApp. selection. paragraphFormat. alignment = Word. wdParagraphAlignment. wdAlignParagraphCenter; // horizontally center // fill in the table content newTable. cell (2, 1 ). range. text = "Basic Product Information"; newTable. cell (2, 1 ). range. font. color = Word. wdColor. wdColorDarkBlue; // set the font color in the cell. // merge the newTable cells. cell (2, 1 ). merge (newTable. cell (2, 3); WordApp. selection. cells. verticalAlignment = Word. wdCellVerticalAlignment. wdCellAlignVerticalCenter; // fill in the table content newTable. cell (3, 1 ). range. text = "brand name:"; newTable. cell (3, 2 ). range. text = BrandName; // vertically merge cells newTable. cell (3, 3 ). select (); // Select a row of object moveUnit = Word. wdUnits. wdLine; object moveCount = 5; object moveExtend = Word. wdMovementType. wdExtend; WordApp. selection. moveDown (ref moveUnit, re F moveCount, ref moveExtend); WordApp. selection. cells. merge (); // Insert the image string FileName = Picture; // path of the image: object LinkToFile = false; object SaveWithDocument = true; object Anchor = WordDoc. application. selection. range; WordDoc. application. activeDocument. inlineShapes. addPicture (FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor); WordDoc. application. activeDocument. inlineShapes [1]. width = 100f; // Image Width WordDoc. application. activeDocument. inlineShapes [1]. height = 100f; // The Image Height. // set the image to a Word with a surrounding area. shape s = WordDoc. application. activeDocument. inlineShapes [1]. convertToShape (); s. wrapFormat. type = Word. wdWrapType. wdWrapSquare; newTable. cell (12, 1 ). range. text = "special product attributes"; newTable. cell (12, 1 ). merge (newTable. cell (12, 3); // Add the WordDoc row to the table. content. tables [1]. rows. add (ref Nothing); WordDoc. paragraphs. Last. range. text = "document creation time:" + DateTime. now. toString (); // "paid" WordDoc. paragraphs. last. alignment = Word. wdParagraphAlignment. wdAlignParagraphRight; // Save the WordDoc file. saveAs (ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing); WordDoc. close (ref Noth Ing, ref Nothing, ref Nothing); WordApp. quit (ref Nothing, ref Nothing, ref Nothing); message = name + "the document is generated successfully and saved to C: CNSI." ;}catch {message = "An error occurred while exporting the file! ";}Return message ;}

 

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.