...
2. In the method that will actually run the report, instantiate the ExcelTemplate object.
...
5. Call ExcelTemplate.Process() to import all data into the file.
Code Block |
---|
XLT.Process();
|
We are not saving the file, since we will be using ExcelApplication to access the file and post-process.
Without the post processing, the populated file will persist the column width and heights. It should look something like this:
Post-Processing
1. In the post-processing method, instantiate the ExcelApplication object:
Code Block |
---|
ExcelApplication XLA = new ExcelApplication();
|
2. Open the populated file via the ExcelTemplate object. 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 correctly. 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.
Code Block |
---|
XLA.Save(wb, Page.Response, "temp.xlsx", false);
|
The final output should look something like this