. Net use AsposeWord to export the word table, asposewordtable
This article is original. For more information, see the source.
1. Preface
. You can also use Microsoft. net to export word files. office. interop and NPOI, but both have disadvantages, Microsoft Office. the Interop component needs to install the office on the host where the program runs. As for NPOI, due to long-term maintenance and numerous bugs, the naming of various objects and attribute names is also very confusing and hard to understand, it is difficult to use. Therefore, to avoid the disadvantages of the two, the best choice is to use the AsposeWord component to export word files.
2. Description
The following is an example of using AsposeWord to export a word Table (source code at the end of the article). AsposeWordHelper is a simple encapsulation of the use of AsposeWord, readers who are familiar with their usage can encapsulate them according to their own habits.
3. Final Results
4. Usage
(1) create a Document Object and obtain DocumentBuilder. add content to the Document to use it.
1 // create a file object 2 Document doc = new Document (); 3 DocumentBuilder builder = new DocumentBuilder (doc );
(2) set the paragraph format: Get ParagraphFormat through the DocumentBuilder object, and use ParagraphFormat to set the paragraph format (Note: The Document object can also be used to set the paragraph and table format, for detailed usage, refer to the example on the Aspose official website)
1 // obtain the ParagraphFormat object 2 var ph = builder. paragraphFormat; 3 // text alignment Mode 4 ph. alignment = ParagraphAlignment. center; 5 // single-factor line spacing = 12, 1.5 times = 186 ph. lineSpacing = 12;
The Font object is used to set the Font:
// Obtain the Font object Font font = builder. font; // font size. size = 11; // whether to bold font. bold = false; // underline style. None is a non-underline font. underline = Underline. none;
(3) Add a body: Use DocumentBuilder to add a body,
// Add a text builder. write ("this is text"); // Add text (with carriage return) builder. writeln ("this is text with ln"); // Add the carriage return builder. writeln ();
(4) Add a table:Add a table in DocumentBuilder,
// Start adding Table builder. startTable (); // start to add the first row and set the table row's high RowFormat rowf = builder. rowFormat; rowf. height = 40; // insert a cell builder. insertCell (); // sets whether the cells are horizontally merged. None indicates that the cells are not merged. cellFormat. horizontalMerge = CellMerge. none; // sets whether the cells are vertically merged. None indicates that the cells are not merged. cellFormat. verticalMerge = CellMerge. none; // set the cell width builder. cellFormat. width = 80; // cell vertical alignment direction builder. cellFormat. verticalAlignment = cellverticalignment. center; // cell horizontal alignment builder. paragraphFormat. alignment = ParagraphAlignment. center; builder. cellFormat. fitText = true; // set the text in the cell to multiple rows (single row by default, which affects the cell width) // Add the text builder in the cell. write ("this is a cell of row 1 line 1"); // end the first line builder. endRow (); // continue adding the second row of the table... // end table builder. endTable ();
(5) Finally, use the Document object to save it as a word file.
// Add a file name and Save it as the doc file string fileName = DateTime. Now. ToString ("yyyy-MM-dd") + ". doc"; doc. Save (fileName );
Source Code address: github, package and download