...
Code Block |
---|
protected void GenerateTemplate() { xla = new ExcelApplication(); wb = xla.Open(Page.MapPath(@"templates\template.xlsx")); for (int i = 0; i < selectedCountries.Count; i++) { wb.Worksheets.CopySheet(wb.Worksheets[0], wb.Worksheets.Count, selectedCountries[i]); } wb.Worksheets["SimpleTemplate"].Visibility = Worksheet.SheetVisibility.Hidden; wb.Worksheets[1].Select(); /*********Part 2*************/ AddCoverSheet(); /**************************/ } |
START HERE FOR REVIEW <<
Executing the coversheet
...
Adding the cover sheet
1. Create a new worksheet called "Summary" at the beginning of the workbook with Worksheets.CreateWorksheet
:
Code Block |
---|
Worksheet ws = wb.Worksheets.CreateWorksheet("Summary", 0); |
2. Create a Worksheet object of the worksheet that was just created.Select the "Summary" worksheet as the worksheet that will be active when the workbook opens with Worksheets.Select
.
Code Block |
---|
Worksheet ws = wb.Worksheets["Summary"]; |
3. Hide the gridlines in the summary worksheet using Worksheet.ShowGridlines
. By default, this property is set to true
.
Code Block |
---|
ws.ShowGridlines = false; |
Inserting an image
1. The image will be inserted in cell A1
. Adjust the height of row 1 to accommodate the image by retrieving the RowProperties object of row 1:
Code Block |
---|
ws.GetRowProperties(0).Height = 90; |
Note: The row height is set in units of 1/72 of an inch.
2. In the example, the user can select an image to insert into the file. The path of that image is dynamically retrieved and stored in variable imagePath
.
Code Block |
---|
string imagePath = RBImage.SelectedItem.Value; |
3. Create an Anchor
in cell A1
with 0 offset
>>>REVIEW HERE
13. Inserting a picture into a worksheet requires creating an Anchor object using the Worksheet.CreateAnchor() method.
Code Block |
---|
Anchor anc = wb.Worksheets[0].CreateAnchor(0, 0, 0, 0); |
14. We then want to create a Picture object using Pictures.CreatePicture method where the string describing the name of the image is pulled from the utility method RBImage.SelectedItem.Value
.
Code Block |
---|
Picture logo = ws.Pictures.CreatePicture(Page.MapPath(@image), anc);
|
4. Create a GlobalStyle using the Workbook.CreateStyle() method. ExcelWriter uses three Styles: CellStyle. GlobalStyle
, and NamedStyle. The GlobalStyles
type is a Style
that can be applied to cells and areas throughout the workbook.
...
Code Block |
---|
for (int i = 2; i < wb.Worksheets.Count; i++) { string sheetName = wb.Worksheets[i].Name.ToString(); ws.Cells[16 + i, 1].Formula = "=HYPERLINK(\"#"+sheetName+"!A1\", \""+sheetName+"\")"; } |
13. Inserting a picture into a worksheet requires creating an Anchor object using the Worksheet.CreateAnchor() method.
Code Block |
---|
Anchor anc = wb.Worksheets[0].CreateAnchor(0, 0, 0, 0); |
14. We then want to create a Picture object using Pictures.CreatePicture method where the string describing the name of the image is pulled from the utility method RBImage.SelectedItem.Value
.
Code Block |
---|
string image = RBImage.SelectedItem.Value;
Picture logo = ws.Pictures.CreatePicture(Page.MapPath(@image), anc);
|
15. We want to set the dimensions of the Picture
object using Picture.Height and Picture.Width.
Code Block |
---|
logo.Height = 195;
logo.Width = 290;
|
16. Finally, we want select the first sheet in the workbook (the "Summary" sheet) to be first viewed when the document is opened.
Code Block |
---|
wb.Worksheets[0].Select(); |
Binding the coversheet data with ExcelTemplate
...