C # manipulating Docx documents

Source: Internet
Author: User

One, you need to refer to the Docx class library file, which can be found directly in NuGet.

Second, create the file and add a table

public static string fileName = AppDomain.CurrentDomain.BaseDirectory + "Youziku-5.doc"; public static bool Existsfile () {try {if (file.exists (FileName)) return True                ; Create the file file.create (filename). Close ();
using (var document = Docx.create (FileName)) {
Create a table with 1 rows and 3 columns var table = document. AddTable (1, 3); Table. Design = tabledesign.lightlist;//Border table. Alignment = Alignment.center; Table. Rows[0]. Cells[0]. Paragraphs[0]. Append ("font name"); Table. Rows[0]. CELLS[1]. Paragraphs[0]. Append ("Font effect"); Table. Rows[0]. CELLS[2]. Paragraphs[0]. Append ("font manufacturer"); var p1 = document. InsertParagraph (); P1. Inserttableafterself (table); Saves the current document. Save (); } return true; } catch (Exception ex) {Console.WriteLine (ex. Message); return false; } }

Third, append data to the table

  public static string Set (String fontname, String fontcompany) {try {//If not saved                In the file, create var result = Existsfile ();                if (!result) return tajaxcallback.error; using (var document = Docx.load (FileName)) {var Imgpath = AppDomain.CurrentDomain.BaseD                    Irectory + "123.png";                        Add an image to the document. var image = document.                    AddImage (Imgpath); var picture = image.                    Createpicture (); var table = document.                    Tables[0]; var count = table.                    RowCount; Table.                    InsertRow (); Table. Rows[count]. Cells[0]. Paragraphs[0].                    Append (FontName); Table. Rows[count]. CELLS[1]. Paragraphs[0].                    Appendpicture (picture); Table. Rows[count]. CELLS[2]. Paragraphs[0].                    Append (Fontcompany); Document.                Save (); } return TajaxcaLlback.ok;                } catch (Exception e) {Console.WriteLine (e);            return tajaxcallback.error; }        }

  

C # manipulating Docx documents

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.