Page tree

Versions Compared

Key

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

...

5. Call ExcelTemplate.Process() to import all data into the file.

Code Block
XLT.Process();

Do not save the file. In the next step the XLT object will be processed by ExcelApplication.

Without the post processing, the populated file will persist the column width and heights. It should look something like this: Image Removed

Warning

Move to part 3 with the rest of ExcelApplication. Mixing and matching too much doesn't make sense.

Post-Processing

1. In the post-processing method, instantiate the ExcelApplication object:

Code Block

ExcelApplication XLA = new ExcelApplication();

2. Open the populated file with ExcelApplication. The file will open as a Workbook object

Code Block

Workbook wb = XLA.Open(XLT);

3. Access the first Worksheet.

Code Block

Worksheet ws = wb.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

ws.PopulatedCells.AutoFitWidth();
ws.PopulatedCells.AutoFitWidth();

5. Finally, call ExcelApplication.Save to save the final file. This example streams the file using the page response.

...

6. Call  ExcelTemplate.Save() to save the final output

Code Block
XLT.Save(Page.Response, "temp.xlsx", false);

The final output should look something like this:

...

Code Block
using SoftArtisans.OfficeWriter.ExcelWriter;
...


//Instantiate the template object
ExcelTemplate XLT = new ExcelTemplate();
//Open the file
XLT.Open(Page.MapPath("//templates//Part1_Financial_Template.xlsx"));


//Create data binding properties
DataBindingProperties bindingProps = XLT.CreateDataBindingProperties();


//Get the data from the CSVs. More info about the generic parser is available
//in the project and in the tutorial above.
DataTable dtAssets = GetCSVData("//data//Assets.csv");
DataTable dtLosses = GetCSVData("//data//Losses.csv");
DataTable dtOther = GetCSVData("//data//Other.csv");


//Declare the row data. This tutorial uses a single item array to demonstrate the
//optional modifier
string\[\] headerValues = { "2011" };
string\[\] headerNames = { "FiscalYear" };


//Bind each datatable
XLT.BindData(dtAssets, "Assets", bindingProps);
XLT.BindData(dtLosses, "Losses", bindingProps);
XLT.BindData(dtOther, "Other", bindingProps);

//Bind the single row data
XLT.BindRowData(headerValues, headerNames, "Header", bindingProps);


//Call process to import data to file
XLT.Process();

/*This
next section handles the post-processing*/


//Instantiate ExcelApplication
ExcelApplication XLA = new ExcelApplication();


//Open the XLT object as a new workbook
Workbook wb = XLA.Open(XLT);


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


//Set the autofit width and height
ws.PopulatedCells.AutoFitWidth();
ws.PopulatedCells.AutoFitHeight();


//Save the file
XLAXLT.Save(wb, Page.Response, "temp.xlsx", false);

...