SILVERLIGHT COM AUTOMATION iterative Excel

Tuesday, June 15, 2010

SilverLight Support COM AUTOMATION in out of the browser mode. its a wonderful capability by using COM AUTOMATION on web. by using COM AUTOMATION in silver light interact with other application as well like Microsoft Office (Excel,Word,other..) excel is great to generate Reports in Silver light. so there demo with source code i get help form windows application to create this excel interaction for write function in c#sharep because we have to use dynamic DataType which has been provided in . net-framework 4 dynamic datatype initialize on runtime thats why it do not provide intelligence.

Xaml Code

<UserControl x:Class="LoadExcelObjects.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

mc:Ignorable="d"

d:DesignHeight="600" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

<Grid x:Name="LayoutRoot" Background="White">

<Image Height="215" HorizontalAlignment="Left" Margin="0,44,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="400" />

<Button Content="Create" Height="23" HorizontalAlignment="Left" Margin="28,15,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />

<Button Content="Load" Height="23" HorizontalAlignment="Left" Margin="313,49,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />

<sdk:DataGrid AutoGenerateColumns="True" Height="196" HorizontalAlignment="Left" Margin="-8,262,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="408" />

<Button Height="28" HorizontalAlignment="Left" Margin="109,15,0,0" Name="button23" VerticalAlignment="Top" Width="279" Content="Kindly install this Applicaiton then Run" Click="label1_Click" />

Grid>

UserControl>


Csharep Code


public partial class MainPage : UserControl

{

dynamic excel;

dynamic sheetShapes;

dynamic chart;

dynamic sheet;

public MainPage()

{

InitializeComponent();

this.Loaded += new RoutedEventHandler(MainPage_Loaded);

}

void MainPage_Loaded(object sender, RoutedEventArgs e)

{

if (Application.Current.InstallState == InstallState.NotInstalled)

{

button1.IsEnabled = false;

button2.IsEnabled = false;

}

else

{

button1.IsEnabled = true;

button2.IsEnabled = true;

}

}

private void button1_Click(object sender, RoutedEventArgs e)

{

if (AutomationFactory.IsAvailable)

{

excel = AutomationFactory.CreateObject("Excel.Application");

excel.Visible = true;

excel.Workbooks.Add();

dynamic workbook = excel.workbooks;

sheet = excel.ActiveSheet; // get the active sheet

dynamic cell = null;

int i = 1;

List<UsamastudentDatatype> data = new List<UsamastudentDatatype>();

for (int j = 0; j <>

{

data.Add(new UsamastudentDatatype() { Subject = "ASP.NET", Marks = (10 + j).ToString(), Name = "Students" });

}

foreach (var item in data)

{

cell = sheet.Cells[i, 1]; // row, column

cell.Value = item.Name;

cell.ColumnWidth = 25;

cell = sheet.Cells[i, 2];

cell.Value = item.Subject;

cell = sheet.Cells[i, 3];

cell.Value = item.Marks;

i++;

}

// add a chart

sheetShapes = sheet.Shapes;

sheetShapes.AddChart(-4100, 200, 2, 400, 300);

chart = sheet.ChartObjects(1).Chart;

}

}

private void button2_Click(object sender, RoutedEventArgs e)

{

if (AutomationFactory.IsAvailable)

{

string path = string.Format("{0}\\picture.jpg",

Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));

chart.Export(path, "JPG");

try

{

using (FileStream stream = File.OpenRead(path))

{

BitmapImage bitmapImage = new BitmapImage();

bitmapImage.SetSource(stream);

image1.Source = bitmapImage;

stream.Close();

}

}

catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }

loadgrid();

}

}

void loadgrid()

{

List<UsamastudentDatatype> obj = new List<UsamastudentDatatype>();

try

{

for (dynamic i = 1; i <>

{

UsamastudentDatatype rowobject = new UsamastudentDatatype();

rowobject.Marks = excel.ActiveSheet.Cells[i, 2].Value.ToString();

rowobject.Name = excel.ActiveSheet.Cells[i, 1].Value.ToString();

rowobject.Subject = excel.ActiveSheet.Cells[i, 2].Value.ToString();

obj.Add(rowobject);

}

}

catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }

dataGrid1.ItemsSource = obj;

}

private void label1_Click(object sender, RoutedEventArgs e)

{

if (Application.Current.InstallState == InstallState.NotInstalled)

{

Application.Current.Install();

}

else

{

button23.IsEnabled = false;

}

}

}

public class UsamastudentDatatype

{

public string Name { get; set; }

public string Subject { get; set; }

public string Marks { get; set; }

}



Download Source

By Usama Wahab khan

share this post
Share to Facebook Share to Twitter Share to Google+ Share to Stumble Upon Share to Evernote Share to Blogger Share to Email Share to Yahoo Messenger More...

0 comments: