Use Aspose.words for. NET dynamic generation of data tables in Word documents

Source: Internet
Author: User

1. Overview

Recently, there is a requirement in the project: Export Word documents, require that the format of the document is not fixed, the user can easily adjust, the data table column in the export content is dynamic, such as the need to export names and gender, you have to export these two columns of data, And this document is not exported after the adjustment but after the guidance has been adjusted. See here, you may immediately think of the template to export! And. NET comes with this component: Microsoft.Office.Interop.Word, you can meet the demand for the moment. However, this component also has limitations, such as the need for the client to install Office components, and a high degree of coding complexity. The most troublesome requirement is the----Dynamic table column header in the back! The following describes how to use aspose.words for. NET to dynamically generate data tables in a Word document.

2. Text

Aspose.words, is one of the Aspose "Family Class library". In addition to this, there are many other powerful class libraries such as Aspose.pdf (operation of the PDF file Class library), Aspose.flash (operation of the Flash File class library), Aspose.report (operation of the report of the class library), etc., we are interested in the official web study. These class libraries contain two languages, one is Java and the other is. NET. This is mainly about aspose.words for. Net. Aspose.words for. NET feature is very powerful, you do not need to install Office components, you can use this to generate Word documents. Aspose.words supports DOC,DOCX,OOXML,RTF format, HTML format, OpenDocument format, PDF format, and other formats. From the figure below, you can learn more about the architecture of the aspose.words. A little forgot to mention, this set of class library is charged, in this article I will provide a cracked version for everyone.

Before describing how to dynamically generate a table, let's look at how this class library generates values based on the template:

First, let's build a Word Template:template.doc. Create a bookmark in the document where you need to generate the data:

As you can see here, we mainly operate through the bookmark, as a source of dynamic data.

Let's take a look at how this is accomplished:

String tmppath = Server.MapPath ("~/template.doc");
Document doc = new document (Tmppath); Loading templates
if (Doc. range.bookmarks["Name"]! = NULL)
{
Bookmark mark = doc. range.bookmarks["Name"];
Mark. Text = "Zhang three companies";
}
Doc.  Save ("Demo.doc", Saveformat.doc, Savetype.openinword, Response); Save As Doc, and open

Isn't it simple? OK, let's take a look at the beginning of the article on how to dynamically generate a Word table.

Aspose.words the action in the Word document object. A bit like a two-dimensional array on the build table. Remember the need to say "table column number is controlled by the user", so we have to define a template on a table containing a header, here to draw your data table can be displayed in all the column header, this is mainly to consider that the user can reduce the need to display the column header. Note here that the order of the Bookmarks and column headers cannot be reversed, so let's see the code below. Such as:

The gray part is the bookmark. Aspose.words the principle of generating a table is like a two-dimensional array, that is, a cell to be generated. Let's take a look at how the code section is implemented:

Code

Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/

-->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 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 values
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 out the signs
Doc. Save ("Baojiadan.doc", Saveformat.doc, Savetype.openinword, page. Response);

Let's take a look at the effect of the final build:

This part of our function has been fully completed.

Perhaps this method is not the best, if friends are interested in research, we exchange together.

Attached: aspose.words for NET 6.5 cracked version

Use Aspose.words for. NET dynamic generation of data tables in Word documents

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.