Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
using SoftArtisans.OfficeWriter.ExcelWriter;
...
//Declare globals
 MemoryStream stream;
 ExcelApplication XLA;
 ExcelTemplate XLT;

protected void RunReport()
{
          //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 void BindTemplateData()
{
	XLT = new ExcelTemplate();

	//Open the template file
	XLT.Open(stream);

	//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();
}

protected void JoinReports()
{
	//Instantiate ExcelApplication
	XLA = new ExcelApplication();

	//Open the first template
	Workbook wbTo = XLA.Open(Page.MapPath("//templates//Part1_Financial_Template.xlsx"));

	//Open the second template
	Workbook wbFrom = XLA.Open(Page.MapPath("//templates//Part2_Financial_Template.xlsx"));

	//Open the first worksheet, with the table
	Worksheet wsFrom = wbFrom[0];
	//Open the second worksheet, with the data
	Worksheet wsFromData = wbFrom[1];

	//Call CopySheet, making sure to copy the data sheet first
	wbTo.Worksheets.CopySheet(wsFromData, 1, "Data");
	wbTo.Worksheets.CopySheet(wsFrom, 1, "Percent by Quarter");

	//Instantiate the 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 XLT
         Workbook wb = XLA.Open(XLT);

         //Get the first worksheet
         Worksheet ws = wb.Worksheets[0];

         //Set autofit for the width and height
         //This sets all cells to the 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);
}


Downloads

TBA

You can download the code for the Financial Report here.