1, Overview
Recently, the project has the following requirement: to export a Word document, the format of this document is not fixed. You can adjust it as needed. The data tables in the exported content are dynamic, for example, if you want to export the name and gender, You need to export the data in these two columns. This document is not exported and then adjusted, but exported and changed. Now, you may want to export data using a template! In addition,. Net comes with this component: Microsoft. Office. InterOP. Word, which can meet your needs for the moment. However, this component also has limitations. For example, the client must install the office component and the encoding complexity is high. The most troublesome requirement is the following ---- dynamic table column header! The following describes how to useAspose. Words for. netTo dynamically generate data tables in Word documents.
2,Body
Aspose. WordsIs the aspose "Family Class Library. In addition to this, there are also some serious library examples, such as aspose.pdf (class library for operating PDF files), aspose. Flash (class library for operating Flash files), and aspose. Report (class library for operating reports). If you are interested, you can go to the official website for research. These class libraries contain two languages:Java. Net. The other one is. net.Aspose. Words for. net. Aspose. Words for. NET is very powerful. You can use this to generate WORD Documents without installing the office component.Aspose. WordsSupports doc, docx, ooxml, RTF, HTML, opendocument, PDF, and other formats.. As shown in the following figure, you can learn more about the architecture of aspose. Words. I forgot to mention that this class library is charged. I will provide you with a cracked version later in this article.
Before introducing how to dynamically generate a table, let's take a look at how this class Library generates values based on the template:
First, let's createWordTemplate:Template.doc. Create a bookmark for the data to be generated in the document ,:
As you can see here, we mainly use bookmark as the source of dynamic data.
Let's take a look at how it is implemented:
String Tmppath = Server. mappath ( " ~ /Template.doc " );
Document Doc = New Document (tmppath ); // Load Template
If (Doc. range. bookmarks [ " Name " ] ! = Null )
{
Bookmark = Doc. range. bookmarks [ " Name " ];
Mark. Text = " James company " ;
}
Doc. Save ( " Demo.doc " , Saveformat. Doc, savetype. openinword, response ); // Save as Doc and open
Is it easy? Okay. Let's take a look.ArticleHow to dynamically generate a Word Table.
Aspose. Words: operations on Word document objects. The generated table is similar to a two-dimensional array. Remember to say "is the number of table columns controlled by the user", so we need to define a table with a header in the template. here we need to draw all the column headers that can be displayed in your data table, in this way, you can reduce the number of column headers that do not need to be displayed. Note that the order of bookmarks and column headers cannot be reversed.CodeYou will know. For example:
The gray part is bookmark. The principle of generating a table using aspose. Words is like a two-dimensional array, that is, generating cells one by one. Let's take a look at how the code is implemented:
Code
Documentbuilder Builder = New Documentbuilder (DOC );
Datatable Products = This . Getdata (); // Data Source
Int Count = 0 ;
// Number of columns to be displayed
For (VAR I = 0 ; I < Products. Columns. Count; I ++ )
{
If (Doc. range. bookmarks [products. Columns [I]. columnname. Trim ()] ! = Null )
{
Bookmark = Doc. range. bookmarks [products. Columns [I]. columnname. Trim ()];
Mark. Text = "" ;
Count ++ ;
}
}
System. Collections. Generic. List < String > Listcolumn = New System. Collections. Generic. List < String > (Count );
For (VAR I = 0 ; I < Count; I ++ )
{
Builder. movetocell ( 0 , 0 , I, 0 ); // Move Cells
If (Builder. currentnode. nodetype = Nodetype. bookmarkstart)
{
Listcolumn. Add (builder. currentnode As Bookmarkstart). Name );
}
}
Double Width = Builder. cellformat. width; // Get cell width
Builder. movetobookmark ( " Table " ); // Start adding value
For (VAR m = 0 ; M < Products. Rows. Count; m ++ )
{
For (VAR I = 0 ; I < Listcolumn. Count; I ++ )
{
Builder. insertcell (); // Add a cell Builder. cellformat. Borders. linestyle = linestyle. Single;
Builder. cellformat. Borders. Color = System. Drawing. color. Black;
Builder. cellformat. Width = Width;
Builder. cellformat. verticalmerge = Aspose. Words. Tables. cellmerge. None;
Builder. Write (products. Rows [m] [listcolumn [I]. tostring ());
}
Builder. endrow ();
}
Doc. range. bookmarks [ " Table " ]. Text = "" ;//Clear mark Doc. Save ("baojiadan.doc", saveformat. Doc, savetype. openinword, page. Response );
Let's take a look at the final result:
All of our functions have been completed.
Maybe this method is not the best. If you are interested, you may wish to study it and share it with us.
Appendix:Aspose. Words for net6.5Cracked version