Page tree

Versions Compared

Key

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

...

2. Select the "Summary" worksheet as the worksheet that will be active when the workbook opens with WorksheetsWorksheet.Select().

Code Block
ws.Select();

...

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. Set the offsets to 0.

Code Block
Anchor anc = wb.Worksheets[0].CreateAnchor(0, 0, 0, 0);

14. We then want to create a 4. Then insert the 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@imagePath), anc);

...

Writing and Formatting Text

1. Set the Cell.Value of cell A15 to the string "Date Executed :". In cell A16, set Cell.Value to "Report For:". In cell A18, set the Cell.Value to "Table of Contents".

Code Block

ws.Cells["A15"].Value = "Date Executed :";
ws.Cells["A16"].Value = "Report For :";
ws.Cells["A18"].Value = "Table of Contents";

2. Insert the data markers for the "Date Executed" and "Report For" data, which will be imported using ExcelTemplate.

Code Block

ws.Cells["C15"].Value = "%%=WebFormData.Date";
ws.Cells["C16"].Value = "%%=WebFormData.Name";

3. 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.For more about using styles in ExcelWriter, see Effective Use of Styles.

newcode}}GlobalStyle underlined = wb.CreateStyle();{{newcode

54. Set the underlined GlobalStyle to Font.UnderlineStyle of underlined to{{UnderlineStyle.Single. ExcelApplication allows for various underline types.}}.

Code Block
underlined.Font.Underline = Font.UnderlineStyle.Single;

65. Create another GlobalStyle using the Workbook.CreateStyle() method called 'boldAndFont12'.

Code Block
GlobalStyle boldAndFont12 = wb.CreateStyle();

7. Use the Style.Font to set the style font to 'Bold' and '6. Set Font.Bold to true and Font.Size to 12.

Code Block
boldAndFont12.Font.Bold = true;
boldAndFont12.Font.Size = 12;

8. Use the Cell.Value method to set the value of cell A15 to the string "Date Executed :". Cell.Value must be a string.

Use the Cell.Value method again to set the value of cell A16 to the string "Report For :" and value of cell A18 to "Table of Contents".

Code Block

ws.Cells["A15"].Value = "Date Executed :";
ws.Cells["A16"].Value = "Report For :";
ws.Cells["A18"].Value = "Table of Contents";

9. 7. Create another GlobalStyle called "dateForm" and use the NumberFormat.DateFormat method to create specify the desired date format NumberFormat.DateFormat.

Code Block
GlobalStyle dateForm = wb.CreateStyle();
dateForm.NumberFormat = wb.NumberFormat.DateFormat.MonthDayYear;

108. Apply the styles to the various cells. underlined style to A15 and A16. Apply boldAndFont12 to A18 and apply dateForm to B15.

Code Block
ws.Cells["A15"].Style = underlined;
ws.Cells["A16"].Style = underlined;
ws.Cells["A18"].Style = boldAndFont12;
ws.Cells["B15"].Style = dateForm;

11. Insert the data markers for the WebFormData (Textbox inputted name, and today's date) as strings. Data markers must have the format %%=DataSource.ColumnName

Code Block

ws.Cells["C16"].Value = "%%=WebFormData.Name";
ws.Cells["C15"].Value = "%%=WebFormData.Date";

12. 9. Next we will add hyperlinks to each of the worksheets in the workbook. To do this, we will use Excel's native HYPERLINK formula to point to the other worksheets. The format for the HYPERLINK formula will be:

=HYPERLINK(\"#DestinationSheet!DesitinationCell\", \"Text\")

This will be set to the Cell.Formula property.

Iterate through the country sheet names using a for loop, excluding the "Summary" and hidden "SimpleTemplate" sheets.Cells below the "Table of Contents" cell are given a Hyperlink formula to another sheet in the workbook. When hyperlinking to another page in a workbook, the formula must follow "=HYPERLINK(\"#DestinationSheet!DesitinationCell\", \"Text\")" and use the Cell.Formula method., which are the first two worksheets in 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+"\")";
}

...