Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
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:

C#
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="406" Width="345">
    <Grid>
        <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>
    </Grid>
</Window>


Below is my Window1.xaml.cs code:

C#
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()
        {
            InitializeComponent();
        }

        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;
            sheetRange.GetType().InvokeMember("Value",BindingFlags.SetProperty,null,sheetRange,obj);

        }
    }
}


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.

Regards
Anurag
Posted

1 solution

There are at least three approaches:

  1. Don't write
    C#
    using Systems.Windows.Window;

    and
    C#
    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
    C#
    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:
    C#
    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.

—SA
 
Share this answer
 
v6
Comments
Maciej Los 26-Mar-13 14:33pm    
Nice explained, +5!
Sergey Alexandrovich Kryukov 26-Mar-13 14:49pm    
Thank you, Maciej.
—SA
Anurag Sinha V 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..

Regards
Anurag
Sergey Alexandrovich Kryukov 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.
—SA
Anurag Sinha V 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... :)

-Anurag

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900