We know that generally exported Word documents or Excel documents, basically divided into two categories, one is the dynamic generation of all the content of the document, a fixed template based on the content output, the latter in many occasions used more, which is also a manifestation of Enterprise report normalization.
My blog describes several uses of the Aspose.word control and the Aspose.cell control, as shown below.
The use of Aspose.cell control to achieve Excel difficult report generation (i)
The use of Aspose.cell control to achieve Excel difficult report generation (ii)
The use of Aspose.cell controls to generate Excel's difficult report (III)
Merging multiple Excel files using the Aspose.cell control
And the operations of using Aspose.word controls to implement Word documents in Word.
These are my own summary, is actually from the project, to the blog to go to the experience summary, this article mainly introduces in the Web templated document output, the Aspose.word control and the Aspose.cell control use operation.
1. Word Templating Document Export
The templated output begins with a predefined, fixed template, and then binds the data source or replaces the associated text to implement the exported operation of the templated document.
In the Aspose.word object, we can use the text substitution method to implement the template content modification, the simple operation is as follows.
Document doc = new document (MyDir + "Document.doc");d OC. Range.replace ("Wuhuacong", "Wu Huacong", false, true);
It can also be implemented as a bookmark reference substitution, first of all to define the appropriate label reference, the operation is as follows.
Inserts a label reference at a fixed location in a Word document, inserting a bookmark reference at the specified location in Word (2007, 2010) in insert-bookmark, as shown below.
The code to replace the contents of the label is shown below.
Aspose.Words.Bookmark Bookmark = doc. range.bookmarks["Accuser_sex"]; if (bookmark! = null) { bookmark. Text = "male"; }
To better demonstrate the effect of the operation, we will first fix a Word template document as follows.
To implement a templated document export, I am here to implement the replacement of the content in two ways, one is to use the text substitution, one is to use the label reference, two parts of the code as shown below.
protected void Btngenword_click (object sender, EventArgs e) {dictionary<string, string> D Ictsource = new dictionary<string, string> (); Dictsource.add ("Tis_handle_no", "T0001"); Dictsource.add ("Accuse_industry", "Taxi"); Dictsource.add ("Accuser_name", "Zhang San"); String templatefile = Server.MapPath ("./templates/advice.doc"); Aspose.Words.Document doc = new Aspose.Words.Document (templatefile); Replace the foreach (string name in Dictsource.keys) {doc) with text. Range.replace (name, Dictsource[name], true, true); } #region Bookmark substitution mode Aspose.Words.Bookmark bookmark = doc. range.bookmarks["Accuser_sex"]; if (bookmark! = null) {bookmark. Text = "male"; } bookmark = doc. range.bookmarks["Accuser_tel"]; if (bookmark! = null) {Bookmark. Text = "1862029207*"; } #endregion Doc. Save (Response, "Testadvice.doc", Aspose.Words.ContentDisposition.Attachment, Aspose.Words.Saving.SaveOption S.createsaveoptions (ASPOSE.WORDS.SAVEFORMAT.DOC)); }
The ASP. NET-based interface is shown below.
The resulting templated document looks like this, and the entire document is based on the fixed template output, so it is more standard and uniform.
2, Aspose.cell the template document export
Aspose.cell template document is similar, there are two ways to operate, one is to replace the text, one is to bind the data source implementation, specifically, I can refer to the previous list of essays, for Apsose.cell, binding the data source is very powerful.
Binding the data source is by setting the variable object in the template way, the variable object is referenced by &=, the object's property or column name, through such as &=customer.city way reference, very intuitive and convenient.
Variable objects in this way support simple objects, as shown below.
Complex collection objects are also supported, as shown below.
To demonstrate a templated document export, I've designed a fixed Excel template here to replace the data in two ways, first defining the Excel template as shown below.
The code for the background binding data is shown below.
protected void Btngenexcel_click (object sender, EventArgs e) {dictionary<string, string> Dictsource = new dictionary<string, string> (); Dictsource.add ("Tis_handle_no", "T0001"); Dictsource.add ("Accuse_industry", "Taxi"); Dictsource.add ("Accuser_name", "Zhang San"); String templatefile = Server.MapPath ("./templates/advice.xls"); Workbookdesigner designer = new Workbookdesigner (); Designer. Open (TemplateFile); Aspose.Cells.Worksheet Worksheet = Designer. Workbook.worksheets[0]; Replace foreach with text (string name in Dictsource.keys) {worksheet. Replace (name, Dictsource[name]); }//Replace designer with bound data. Setdatasource ("Accuser_sex", "male"); Designer. Setdatasource ("Accuser_tel", "1862029207*"); Designer. Process (); Designer. Save ("Testadvice.xls", Savetype.openinexcel, Fileformattype.excel2003, Response); }
The above is the use of Aspose.word control and Aspose.cell control, the implementation of the Word document and the template of the Excel document export operation process and code, I hope to help you, also as a summary of my future reference, hehe, welcome to communicate with each other research.