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
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void btnExcel_Click(object sender, RoutedEventArgs e)
{
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;
Workbook wbobj = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wbobj.Worksheets[1];
Range sheetRange = ws.get_Range("B1", "B5");
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