In the current article I proudly present a simple application, which creates a simple Excel file from XAML. In the example I present I am using Excel 2010 as this is my current version. In order to be able to create an excel application from WPF, we should enable the usage of some COM libraries . This is done through the following steps:
1. Go to “References” and click the right button -> Add Reference;
2. In the menu which appears, select “Microsoft Excel 14.0 Object Library” (the library can vary depending on your excel version. Excel 14.0 is for Excel 2010. Here you may find the other excel versions. As you see – Excel 2007 is version 12.0, Excel 2013 is version 15.
3. Simply click “OK”.
After adding the COM library you get access to a few nice features. The XAML code of our application is simple – it is just a button with method. Anyway, here it comes:
1 2 3 4 5 6 7 8 |
<Window x:Class="ExcelInWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="VitoshAcademy" Height="350" Width="525"> <Grid> <Button Width="Auto" Margin="50" Click="GenerateExcelFile">Generate Excel File</Button> </Grid> </Window> |
With the code behind, taking care for the execution of the program, the code looks as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using System; using System.Windows; using System.Windows.Controls; using Microsoft.Office.Interop.Excel; namespace ExcelInWPF { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : System.Windows.Window { public MainWindow() { InitializeComponent(); } private void GenerateExcelFile(object sender, RoutedEventArgs e) { Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); app.Visible = true; app.WindowState = XlWindowState.xlMaximized; Workbook wb = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet ws = wb.Worksheets[1]; DateTime currentDate = DateTime.Now; ws.Range["A1:A3"].Value = "Who is number one? :)"; ws.Range["A4"].Value = "vitoshacademy.com"; ws.Range["A5"].Value = currentDate; ws.Range["B6"].Value = "Tommorow's date is: =>"; ws.Range["C6"].FormulaLocal = "= A5 + 1"; ws.Range["A7"].FormulaLocal = "=SUM(D1:D10)"; for (int i = 1; i <= 10; i++) ws.Range["D" + i].Value = i * 2; wb.SaveAs("C:\\Temp\\vitoshacademy.xlsx"); } } } |
What do we have here? From the beginning, a new namespace:
1 |
using Microsoft.Office.Interop.Excel; |
Then in the method for file generation we have something familiar for those who have code experience with VBA for Excel. With the first three lines we create an Excel application, make it visible and we maximize it. Then we create a workbook and we name it in the same line. The first worksheet of the workbook is also named. Thus, so far we have named the excel application (app), the workbook (wb) and the first worksheet (ws). Then we create a variable of type DateTime, naming it currentDate. Using the “ws.Range[“”] we set values for the different excel boxes. We use formulas and even a loop. At the end we save everything in the path “C\Temp\ and the file is named “vitoshacademy.xlsx”. The usage of double slash (“\\”) is needed, because the single one acts as a comment in C#, and the double one neutralizes it.
In short, that is it. If you want to see this example, you may try it out here – ExcelInWPF.