As we know, Word documents or Excel documents, which are usually exported, are basically divided into two categories. One is the way to dynamically generate the content of all documents, the other is the content output based on fixed template. The latter is widely used in many occasions, which is also a reflection of the standardization of enterprise reports.
My blog has covered several articles about the use of Aspose.Word controls and Aspose.Cell controls, as shown below.
"Using Aspose.Cell Control to Realize Excel High-difficulty Report Formation (1)"
"Using Aspose.Cell Control to Realize Excel High-difficulty Report Formation (2)"
"Using Aspose.Cell Control to Realize Excel High-difficulty Report Formation (3)"
Using Aspose.Cell Control to Merge Multiple Excel Files
And about the operation of Word, "Using Aspose.Word Control to Realize the Operation of Word Documents"
These are my own summaries, which are the real experience from the project to the blog. This article mainly introduces the use and operation of Aspose.Word control and Aspose.Cell control in Web template document output.
1. Word Templated Document Export
Templated output, first of all, is to define a fixed template in advance, then bind the data source or replace the relevant text to achieve the template document export operation.
In Aspose.Word's operation object, we can use text substitution to modify the template content. The simple operation is as follows.
Document doc = new Document(MyDir + "Document.doc");
doc.Range.Replace("wuhuacong", "Wu Huacong", "false, true);
It can also be implemented by bookmark reference substitution. First of all, we need to define the corresponding tag reference. The operation is as follows.
Insert a tag reference at a fixed location in a Word document and a bookmark reference at a specified location in Word (2007, 2010), as shown below.
The code to replace the label content is shown below.
Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks["ACCUSER_SEX"];
if (bookmark != null)
{
bookmark.Text = male;
}
In order to better show the effect of the operation, let's fix a template document for Word, as shown below.
To achieve template document export, I combine two ways to achieve content replacement operation, one is using text replacement, the other is using tag reference, the code of the two parts is as follows.
protected void btnGenWord_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.doc");
Aspose.Words.Document doc = new Aspose.Words.Document(templateFile);
//Replacement with text
foreach (string name in dictSource.Keys)
{
doc.Range.Replace(name, dictSource[name], true, true);
}
#region uses bookmark replacement 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.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc));
}
The Asp.NET-based interface is shown below.
The generated template document is shown below. The whole document is based on fixed template output, so the comparison standard and unification are made.
2. Aspose.Cell Templated Document Export
Aspose.Cell's template documents are similar, there are two ways to operate, one is to replace the text, the other is to bind the data source way to achieve, specific can refer to my previous list of essays, for Apsose.Cell, the function of binding the data source is very powerful.
Binding data source is by setting variable object in template, variable object is referenced by &=, attribute or column name of object is referenced by way of &= Customer. City, which is very intuitive and convenient.
Variable objects in this way support simple objects, as shown below.
Complex collection objects are also supported, as shown below.
In order to demonstrate template document export, I designed a fixed Excel template, which can replace data in two ways. First, I defined the Excel template as follows.
The code for 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];
//Using Text Replacement
foreach (string name in dictSource.Keys)
{
worksheet.Replace(name, dictSource[name]);
}
//Replacement with bound data
designer.SetDataSource("ACCUSER_SEX", "male");
designer.SetDataSource("ACCUSER_TEL", "1862029207*");
designer.Process();
designer.Save("testAdvice.xls", SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
}
Word control and Apose. Cell control, to achieve the template export of Word documents and Excel documents related to the operation process and code, I hope to help you, but also as a summary of my future reference, ha ha, welcome to communicate with each other.