Jake
Jake

Reputation: 1660

WPF excel interop error.'Window' is an ambiguous reference between 'System.Windows.Window' and 'Microsoft.Office.Interop.Excel.Window'

I wrote a small programme to reade data from an excel file in wpf. But i an getting this error: 'Window' is an ambiguous reference between 'System.Windows.Window' and Microsoft.Office.Interop.Excel.Window'.

Why is it happening? My code is given below:

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;

namespace FileReader
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook xlWb;
    Microsoft.Office.Interop.Excel.Worksheet xlWsheet;
    private void excel_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            xlWb = xlApp.Workbooks.Open("C:\\Config.xlsm", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            xlWsheet = (Worksheet)xlWb.Sheets["Startup"];
            Range xlRange = xlWsheet.UsedRange;
            for (int rownum = 0; rownum <= 31; rownum++)
            {
                xlRange = xlWsheet.Cells[rownum + 2, 24];
                d32[rownum] = xlRange.Value.ToString().Substring(2);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("File not found: C:\\Config.xlsm","File not found",MessageBoxButton.OK,MessageBoxImage.Warning);
        }
    }
}

Upvotes: 0

Views: 3317

Answers (2)

Rohit Vats
Rohit Vats

Reputation: 81253

As stated in error - Window class exists in two namespaces you have used. So, you need to use fully qualified type name.

Replace

public partial class MainWindow : Window

with

public partial class MainWindow : System.Windows.Window

Or you can use shorthand syntax where you can declare alias for namespace and use it instead of fully qualified namespace.

using WPFWindow = System.Windows;

public partial class MainWindow : WPFWindow.Window

Upvotes: 3

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Problem : you have imported following two namespaces having Window class

using System.Windows;
using Microsoft.Office.Interop.Excel;

hence there is a ambiguity problem while using following statement

public partial class MainWindow : Window

Solution : you can use fully qualified name of a class Window to avoid the ambiguity problem.

Try This:

public partial class MainWindow : System.Windows.Window

Upvotes: 1

Related Questions