using Word;
The following examples include C#'s operations such as creating Word documents, inserting tables, setting styles, etc.:
(In the example, some of the codes involved in data information are omitted. It is important to introduce some methods of C# operating word documents)
public string CreateWordFile(string CheckedInfo) ...{ string message = ""; try ...{ Object Nothing = ; ("C:/CNSI"); //Create the directory where the file is located string name = "CNSI_" + ()+".doc"; object filename = "C://CNSI//" + name; //File saving path //Create Word document WordApp = new (); WordDoc = (ref Nothing, ref Nothing, ref Nothing, ref Nothing); //Add a header = ; = ; ("[Header Content]"); = ;//Set right alignment = ;//Bounce out of header settings = 15f;//Set the line spacing of the document //Move the focus and wrap the line object count = 14; object WdLine = ;//Change another line; (ref WdLine, ref count, ref Nothing);//Move focus ();//Insert paragraph //Create a table in the document newTable = (, 12, 3, ref Nothing, ref Nothing); //Set the table style = ; = ; [1].Width = 100f; [2].Width = 220f; [3].Width = 105f; //Fill in the form content (1, 1). = "Product Details Table"; (1, 1). = 2;//Set the font in the cell to bold //Merge cells (1, 1).Merge((1, 3)); = ;//Center vertically = ;//Center the level //Fill in the form content (2, 1). = "Basic Product Information"; (2, 1). = ;//Set the font color in the cell //Merge cells (2, 1).Merge((2, 3)); = ; //Fill in the form content (3, 1). = "Brand Name:"; (3, 2). = BrandName; //Merge cells vertically (3, 3).Select();//Select a line object moveUnit = ; object moveCount = 5; object moveExtend = ; (ref moveUnit, ref moveCount, ref moveExtend); (); //Insert the picture string FileName = Picture;//The path of the picture is object LinkToFile = false; object SaveWithDocument = true; object Anchor = ; (FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor); [1].Width = 100f;//Picture width [1].Height = 100f;//Picture height //Set the picture to surrounding type s = [1].ConvertToShape(); = ; (12, 1). = "Product Special Attributes"; (12, 1).Merge((12, 3)); //Add rows in the table [1].(ref Nothing); = "Document creation time:" + ();//"Sign up" = ; //Save file (ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); (ref Nothing, ref Nothing, ref Nothing); (ref Nothing, ref Nothing, ref Nothing); message=name+"The document is generated successfully to save to C:CNSI"; } catch ...{ message = "File export exception!"; } return message; }
The above C# operation examples of creating, inserting tables, setting styles and other operations in Word documents are all the content I share with you. I hope you can give you a reference and I hope you can support me more.