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