Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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"
    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:
 
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 26-Mar-13 8:54am

1 solution

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

Solution 1

There are at least three approaches:
 
  1. Don't write
    using Systems.Windows.Window;
    and
    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.  
  3. 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.
  4.  
  5. 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.
 
—SA
  Permalink  
v6
Comments
Maciej Los at 26-Mar-13 14:33pm
   
Nice explained, +5!
Sergey Alexandrovich Kryukov at 26-Mar-13 14:49pm
   
Thank you, Maciej.
—SA
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..
 
Regards
Anurag
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.
—SA
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... :)
 
-Anurag

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 334
2 CPallini 275
3 DamithSL 230
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2014
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