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

Silver Light Training at Softronic

Saturday, June 12, 2010



Training At Softronic (Software House) . Arranged By Microsoft Pakistan for Give Idea of Next Generation Application. Trained : Usama Wahab Khan on 8-jun-2010


Agenda
•What is .NET Framework 3.5 , 4.0
•Visual Studio 2008 & 2010 Silverliht Tools
•Current Software Development and Design Model
•Introduction to Silverlight
•XAML
•WPF
•Silverlight Architecture
•Silverlight Development Model

Send Email in Silverlight

Monday, June 7, 2010



add asp.net Generic Handler in Web.Project
Add Static Class and Crate Send Email Function
Add This Code in Asp.net Generic handler
Add This Code At SilverLight Project(MainPage.xaml.cs) on send button click
Add this SMPT Configuration in Web.config File
Download SourceCode
By Usama Wahab Khan
Learn Free help Others

Silverlight Simple Deployment and Out of browser mode

Friday, June 4, 2010

Silverlight 3.0, 4 Provide Support Application Out of browser Mode it will behave just like desktop application it install on you desktop and you programs as well all you to remove
you can create border less as well just like window media player .










you install you application Programticlly as well

   1: private void Application_Startup(object sender, StartupEventArgs e)
   2: {
   3:     if ((App.Current.InstallState == InstallState.Installed) && (!App.Current.IsRunningOutOfBrowser))
   4:     {
   5:         this.RootVisual = new Installed();
   6:     }
   7:     else if (!App.Current.IsRunningOutOfBrowser)
   8:     {
   9:         this.RootVisual = new Installer();
  10:     }
  11:     else
  12:     {
  13:         this.RootVisual = new MainPage();
  14:     }
  15: }

For update you OOB mode applicaiton 
Installer Setting

Silver Light aquarium (Water effect)

Tuesday, June 1, 2010




Click on fishes

Download SourceCode
By Usama Wahab Khan