How to Use aspose. Words for. Net to dynamically generate data tables in Word documents under Asp.net

Source: Internet
Author: User

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. words , is one of the aspose "family class libraries . 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. this section mainly introduces aspose. words. net . Aspose. Words for. NET is very powerful. You can use this to generate WORD Documents without installing the office component. aspose. words supports doc, docx, ooxml, RTF, HTML, opendocument, and 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 see how the Class Library generates values based on the template:

First, we will create a Word template: 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:CopyCodeThe Code is as follows: String tmppath = server. mappath ("~ /Template.doc ");
Document Doc = new document (tmppath); // load the Template
If (Doc. range. bookmarks ["name"]! = NULL)
{
Bookmark mark = Doc. range. bookmarks ["name"];
Mark. Text = "Michael Zhang ";
}
Doc. Save ("demo.doc", saveformat. Doc, savetype. openinword, response); // save it 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. You can see the following code. 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:Copy code The Code is as follows: documentbuilder builder = new documentbuilder (DOC );
Datatable products = This. getdata (); // Data Source
Int COUNT = 0;
// Record how many columns to display
For (VAR I = 0; I <products. Columns. Count; I ++)
{
If (Doc. range. bookmarks [products. Columns [I]. columnname. Trim ()]! = NULL)
{
Bookmark mark = 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 the cell
If (builder. currentnode. nodetype = nodetype. bookmarkstart)
{
Listcolumn. Add (builder. currentnode as bookmarkstart). Name );
}
}
Double width = builder. cellformat. width; // obtain the cell width.
Builder. movetobookmark ("table"); // start adding a 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 the 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 net 6.5 cracked version

Related Article

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.