...
2. Globally declare the ExcelTemplate objects. There are two instances - one for the Workbook being copied to and one for the Workbook being copied from
Code Block |
---|
ExcelTemplate XLTto;
ExcelTemplate XLTfrom;
|
3. and ExcelApplication objects. We are also using a MemoryStream to pass the application object to the template object.
Code Block |
---|
MemoryStream stream;
ExcelApplication XLA;
ExcelTemplate XLT;
|
The next step uses CopySheet to create a JoinReports method.
Joining Reports
1. Instantiate ExcelApplication
Code Block |
---|
XLA = new ExcelApplication();
|
2. Open the workbook to copy into.
Code Block |
---|
Workbook wbTo = XLA.Open(Page.MapPath("//templates//Part1_Financial_Template.xlsx"));
|
3. Open the workbook to copy from.
Code Block |
---|
Workbook wbFrom = XLA.Open(Page.MapPath("//templates//Part2_Financial_Template.xlsx"));
|
4. Open the worksheets to copy from. These are the first sheet with a table, and the second sheet containing the data.
Code Block |
---|
Worksheet wsFrom = wbFrom[0];
Worksheet wsFromData = wbFrom[1];
|
5. Call CopySheet for each worksheet. The data sheet needs to be copied first so that the references to it remain valid.
Code Block |
---|
wbTo.Worksheets.CopySheet(wsFromData, 1, "Data");
wbTo.Worksheets.CopySheet(wsFrom, 1, "Percent by Quarter");
|
6. Instantiate the MemoryStream. ExcelTemplate opens ExcelApplication objects as streams
Code Block |
---|
stream = new MemoryStream();
|
7. Finally save the joined file to a stream.
Code Block |
---|
XLA.Save(wbTo, stream);
|
Data Binding
1. Create a method to bind and process the two templates. This method takes the filename and returns the processed template object
Code Block |
---|
private ExcelTemplatevoid BindTemplateData(string filename) { } |
4. In the BindTemplateData method, instantiate a new the ExcelTemplate object to return.
Code Block |
---|
ExcelTemplate XLT = new ExcelTemplate(); |
5. Open the template files with the ExcelTemplate.Open
method. This opens the stream created in the JoinReports method
Code Block |
---|
XLT.Open(Page.MapPath(filenamestream)); |
6. Create a a DataBindingProperties
object object. None of the binding properties will be changed for this tutorial, but but DataBindingProperties
is is a required parameter in in ExcelTemplate
data data binding methods.
Code Block |
---|
DataBindingProperties dataProps = XLT.CreateDataBindingProperties(); |
Data Binding
1. In the BindTemplateData method, get the 7. Get the data for Assets, Losses, and Other.
...
Code Block |
---|
DataTable dtAssets = GetCSVData("//data//Assets.csv"); DataTable dtLosses = GetCSVData("//data//Losses.csv"); DataTable dtOther = GetCSVData("//data//Other.csv"); |
28. Create the datasets for the header row in template 1. This will simply be ignored by template 2.
Code Block |
---|
//Create the array of header values. This example only binds a single item string[] headerValues = { "2011" }; //Create the array of header names. string[] headerNames = { "FiscalYear" }; |
39. Use ExcelTemplate.BindData
to bind the data for the Top and Details Sales data sets.
Code Block |
---|
XLT.BindData(dtAssets, "Assets", bindingProps); XLT.BindData(dtLosses, "Losses", bindingProps); XLT.BindData(dtOther, "Other", bindingProps); |
410. Use the ExcelTemplate.BindRowData
method to bind the header data to the data markers in the template file (i.e. %%=Header.FiscalYear
).
Code Block |
---|
XLT.BindRowData(headerValues, headerNames, "Header", bindingProps); |
511. Call ExcelTemplate.Process() to import all data into the file.
...
Do not save the file. In the next step the XLT object will be post processed by ExcelApplication
Post-Processing
1. The first template file has to be post-processed with autofit. This is also the workbook that is being copied to. Instantiate the ExcelApplication object:
Code Block |
---|
ExcelApplication XLA = new ExcelApplication();
|
2. Open the populated template file with ExcelApplication. The file will open as a Workbook object.
Code Block |
---|
Workbook wbTowb = XLA.Open(XLTToXLT); |
3. Access the first Worksheet.
Code Block |
---|
Worksheet wsTows = wbTowb.Worksheets[0]; |
4. Call Area.AutoFitHeight() and Area.AutoFitWidth() to set the column and row height. AutoFitWidth sets the column width to fit the widest populated cell in the column. AutoFitHeight sets the row height to highest populated cell in the row. In this snippet, the area is Worksheet.PopulatedCells, which returns an area containing all populated cells.
Code Block |
---|
wsTows.PopulatedCells.AutoFitWidth(); wsTows.PopulatedCells.AutoFitWidth(); |
5. Open the populated template file with ExcelApplication. One instance of ExcelApplication can be used to open multiple Workbooks
Code Block |
---|
Workbook wbFrom = XLA.Open(XLTFrom);
|
6. Open both the table sheet and the data sheet.
Code Block |
---|
Worksheet wsFrom = wbFrom[0];
Worksheet wsFromData = wbFrom[1];
Worksheet wsFrom = wbFrom[0]; Worksheet wsFromData = wbFrom[1]
|
7. Call CopySheet with wbTo, and pass the worksheets. Make sure to copy the data sheet first
Note | ||
---|---|---|
| ||
Always copy the data sheet first. The table sheet references the data sheet. If you copy the data sheet second, the references break and an error is thrown. |
Code Block |
---|
wbTo.Worksheets.CopySheet(wsFromData, 1, "Data");
wbTo.Worksheets.CopySheet(wsFrom, 1, "Percent by Quarter");
|
8. Finally, call ExcelApplication.Save to save the final file. This example streams the file using the page response.
Code Block |
---|
XLA.Save(wbTowb, Page.Response, "temp.xlsx", false); |
...
Code Block |
---|
using SoftArtisans.OfficeWriter.ExcelWriter; ... //Declare Templateglobals objects globalyMemoryStream ExcelTemplate XLTTostream; ExcelApplication XLA; ExcelTemplate XLTFromXLT; protected void RunReport() { //GetJoin the populatedtwo filesreports fromusing BindTemplateDataCopySheet XLTTo = BindTemplateData("//templates//Part1_Financial_Template.xlsx" JoinReports(); XLTFrom = BindTemplateData("//templates//Part2_Financial_Template.xlsx"); //Join the two reports using CopySheet JoinReports //bind the data for the newly modified file BindTemplateData(); //Finally postprocess to set the auto fit values PostprocessTemplate(); } protected ExcelTemplatevoid BindTemplateData(string filename) { ExcelTemplate XLT = new ExcelTemplate(); //Open the template file XLT.Open(Page.MapPath(filenamestream)); //Create data binding properties for ExcelTemplate DataBindingProperties bindingProps = XLT.CreateDataBindingProperties(); //Get the datatables to be bound into the template. //This example uses CSV data, but the data can come from //anywhere DataTable dtAssets = GetCSVData("//data//Assets.csv"); DataTable dtLosses = GetCSVData("//data//Losses.csv"); DataTable dtOther = GetCSVData("//data//Other.csv"); //Bind the datatables XLT.BindData(dtAssets, "Assets", bindingProps); XLT.BindData(dtLosses, "Losses", bindingProps); XLT.BindData(dtOther, "Other", bindingProps); /*This section only applies to the first template. It will be ignored by the second template, since there are no markers to bind to.*/ //Create the array of header values. This example only binds a single item string[] headerValues = { "2011" }; //Create the array of header names. string[] headerNames = { "FiscalYear" }; //Bind the header row data XLT.BindRowData(headerValues, headerNames, "Header", bindingProps); //end //Process to import the data to the file XLT.Process(); //Return the populated file return XLT; } protected void JoinReports() { //Instantiate ExcelApplication for XLA the= workbook being copied to ExcelApplication XLATo = new ExcelApplication()new ExcelApplication(); //Open the first template Workbook wbTo = XLA.Open(Page.MapPath("//templates//Part1_Financial_Template.xlsx")); //Open the populatedsecond template Workbook wbTowbFrom = XLAToXLA.Open(XLTTo)Page.MapPath("//templates//Part2_Financial_Template.xlsx")); //Open the first worksheet, with the table Worksheet wsTowsFrom = wbTo.WorksheetswbFrom[0]; //Open the second worksheet, with the data Worksheet wsFromData = wbFrom[1]; //Call AutoFit CopySheet, making sure to setcopy the heightdata andsheet widthfirst wsTowbTo.PopulatedCellsWorksheets.AutoFitWidth(CopySheet(wsFromData, 1, "Data"); wsTowbTo.PopulatedCellsWorksheets.AutoFitHeight(CopySheet(wsFrom, 1, "Percent by Quarter"); //Instantiate ExcelApplicationthe for the workbook being copied from ExcelApplication XLAFrom stream to save to stream = new MemoryStream(); //Save the stream XLA.Save(wbTo, stream); } protected void PostprocessTemplate() { //Instantiate ExcelApplication XLA = new ExcelApplication(); //Open the newly populated template XLT Workbook wbFromwb = XLAFromXLA.Open(XLTFromXLT); //OpenGet the first worksheet, with the table Worksheet wsFromws = wbFromwb.Worksheets[0]; //OpenSet autofit for the secondwidth worksheet,and withheight the data Worksheet wsFromData = wbFrom[1]; //CallThis CopySheet,sets makingall surecells to copy the data sheet first wbTo.Worksheets.CopySheet(wsFromData, 1, "Data"); wbTo.Worksheets.CopySheet(wsFrom, 1, "Percent by Quarter"); //Save the file XLATo.Save(wbTothe highest or widest //value present in the area ws.PopulatedCells.AutoFitWidth(); ws.PopulatedCells.AutoFitHeight(); //Open the processed file in your browser XLA.Save(wb, Page.Response, "Part3_Output.xlsx", false); } |
...