Page tree

Versions Compared

Key

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

...

Info

The sample code template (part2_template.xlsx), page (Part2.aspx), and code behind (Part2.aspx.cs) are included in the SimpleExpenseSummary project available for download as part of the ExcelWriter Basics Tutorials.

.

This part focuses on adding some Excel formulas to the template file from Part 1. Specifically, this covers combining data marker values with other text, adding a 'Total' row after imported data, and including formulas in imported data rows.

...

5. Run the code.
In the output you will see the text combined with the values of the data markers:

Adding a Total Row

Using In-Row Formulas

If a formula references a data marker row, ExcelWriter will update the formula to reflect that multiple rows are being inserted. In this section, we'll briefly cover how ExcelWriter handles formulas that are on the data marker row.

Info

For more information about how ExcelWriter imports data with the ExcelTemplate object, check out How ExcelWriter Inserts Rows: ExcelTemplate.BindData . That article also discusses how importing data with ExcelTemplate affects formulas.

Let's say that we want to generate some short labels for the Top 5 Expenses to use with the descriptions. One possibility is to add the labels to the data source (i.e. through the SQL query or modifying the data structure directly), but another way is to use Excel formulas.

Similar to the last section, we'll use the existing data markers to create a new string for the Top 5 Expenses table.

6. In Column D, create a header for the label. Update the 'Top Expenses' merged cell area to include column D.

Image Added

7. In cell D7, add a formula =UPPER(LEFT(B7, 4)).

Excel's LEFT(text, N) returns the first N characters from the text, starting from the left. In this case, the first 4 characters from cell B7. Then the UPPER function converts the characters to uppercase.

ExcelWriter will update this formula for each row of data that is added, so the formula in row 9 will read =UPPER(LEFT(B9, 4)).

8. Run the report.

You will see that the first 4 characters from the description have been set to uppercase in column D.

Image Added

Adding a Total Row

Downloads

You can download the code for the Basic ExcelWriter Tutorials as a Visual Studio solution, which includes the Simple Expense Summary.

...