Click here to Skip to main content
11,802,636 members (70,696 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# WPF Excel Interop
Hi Folks,

Have a disturbing doubt, hope mentors at CP would be able to assist.
I have a simple WPF application, in which there is a button.
The button is supposed to write data to an excel sheet, which ain't happening.

I get an error while building the application that:

'Window' is an ambiguous reference between 'Systems.Windows.Window' and 'Microsoft.Office.Interop.Excel.Window'.

Below is my Window1.xaml code:

<Window x:Class="WpfApplication1.Window1"
    Title="Window1" Height="406" Width="345">
        <Image Margin="12,100,66,12" Name="image1" Stretch="UniformToFill" Source="Images\s3.jpg" />
        <Button Height="23" Margin="0,38,100,0" Name="btnExcel" VerticalAlignment="Top" BorderBrush="Chartreuse" Foreground="DarkMagenta" HorizontalAlignment="Right" Width="75" Click="btnExcel_Click">Excel</Button>

Below is my Window1.xaml.cs code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace WpfApplication1
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
        public Window1()
        private void btnExcel_Click(object sender, RoutedEventArgs e)
            //creating an excel application
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            if (excelApp == null)
                MessageBox.Show("Check whether you've added Excel assembly as reference");
            excelApp.Visible = true;
            //creating an object of the workbook
            Workbook wbobj = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            //adding a worksheet
            Worksheet ws = (Worksheet)wbobj.Worksheets[1];
            //defining the range of the worksheet
            Range sheetRange = ws.get_Range("B1", "B5");
            //creating elements with an array
            int[] array = new int[5];
            for (int i = 0; i < array.Length; i++)
                array[i] = i * 2;
            Object[] obj = new Object[1];
            obj[0] = array;

The above code works perfectly fine when it's done via normal Windows Forms, because the master namespace used is Form. There is no ambiguity here.
Can anybody shed some light on this scenario, as to is it possible to invoke an excel stuff in a WPF application?
Any assistance would be highly appreciated.

Posted 26-Mar-13 7:54am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

There are at least three approaches:

  1. Don't write
    using Systems.Windows.Window;
    using Microsoft.Office.Interop.Excel;
    in the same file. Skip on one of those lines, or both. When a namespace is used, use fully-qualified names, such as
    Microsoft.Office.Interop.Excel.Window myWindow = //...

  2. Excel generation is totally unrelated to WPF. You only export data, never any WPF UI elements. That said, you really should separate Excel generation stuff and WPF code files. Write it all in separate files, and not only because of your problem, but to make your application maintainable.

  3. Use the simple alias syntax:
    using Systems.Windows.Window;
    // using Microsoft.Office.Interop.Excel // remove, don't use it; instead, use:
    using ExcelWindow = Microsoft.Office.Interop.Excel.Window;
    ExcelWindow myWindow = //...

What to prefer? In all your practice, you will need to use all of these techniques. As to your particular case, even if you use #1 and #3, you still need to do your best to isolate WPF and Excel parts of the code, so #2 is a must, even if you use the other techniques related to type naming.

Maciej Los at 26-Mar-13 14:33pm
Nice explained, +5!
Sergey Alexandrovich Kryukov at 26-Mar-13 14:49pm
Thank you, Maciej.
Anurag Sinha V at 26-Mar-13 14:37pm
Elegant solution Mr. Sergey....just class...My application executed successfully..I gladly accept the solution. And yes I totally agree that I should probably segregate the WPF and excel code bits. Thanks a lot..

Sergey Alexandrovich Kryukov at 26-Mar-13 14:51pm
Great! you are very welcome. It's a pleasure to help someone who can actually use help and make things work, especially that quickly.
Good luck, call again.
Anurag Sinha V at 26-Mar-13 15:08pm
Sure will..whenever I fall into some code trap,CP comes to rescue..You guys are doing a great job..I also imagine myself sometime later in your shoes... :)


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 KrunalRohit 366
1 OriginalGriff 365
2 F-ES Sitecore 295
3 Afzaal Ahmad Zeeshan 230
4 CPallini 230
0 OriginalGriff 2,950
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,695
4 Richard MacCutchan 1,157

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100