Communicating with Excel

When a Silverlight 4 application is running with Elevated Permissions you can access COM objects which can be quite handy.  For example you may want to open Outlook and send an email, or as in the example presented here open Excel and create a new worksheet.

Silverlight and Excel

It is important to include the following namespace when wanting to use these features:

using System.Windows.Interop;

You will also need to add a reference to the Microsoft.CSharp.dll which can be found in:

Program Files/Microsoft SDKs/Silverlight/V4.0/Libraries/Client/

<UserControl x:Class="SilverlightExcel.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="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Create XLS" Width="100" Height="30" Click="Button_Click" />
    </Grid>
</UserControl>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Runtime.InteropServices.Automation;

namespace SilverlightExcel
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            dynamic excel = AutomationFactory.CreateObject("Excel.Application");
            excel.Visible = true;

            dynamic workbook = excel.workbooks;
            workbook.Add();
            dynamic sheet = excel.ActiveSheet;

            dynamic range;

            range = sheet.Range("A1");
            range.Value = "Hello from Silverlight";
            range = sheet.Range("A2");
            range.Value = "100";
            range = sheet.Range("A3");
            range.Value = "50";
            range = sheet.Range("A4");
            range.Formula = "=@Sum(A2..A3)";
            range.Calculate();
        }
    }
}


Here we use the new C# 4.0 dynamic keyword to create and hold the Excel application object.  The following causes Excel to actually load and display:

excel.Visible = true;

Next we add a new Workbook and get a reference to the current Worksheet.  When we have our worksheet we are ready to populate the worksheet cells, for this example we set values in column A.  First we set the column header, then two values and the final cell is a formula to add the two values together.

Raymond wrote:

Hi,


It looks simple so I tried it in vb.net. Didn't work.

I duplicated all the imports (using) but no luck.

Can anyone point me out in the right direction?


I appreciate any help.


Gr,

Raymond.

Aladin wrote:

http://ebayvietnam.net

I need Export SilverLight Chart to Pdf or Excell, any everyone help me?

Chinmay wrote:

I am getting this error while calling to

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

Please help.

rhysjo wrote:

Hi,


This is a great example.  Does anyone have an example of getting a reference to the current selection?  What I'm trying to do is allow the user to select some cells in Excel and have the silverlight app perform an operatoin of the current selection.


Thanks!

Corsariovasco wrote:

Hi,


How data is entered in another Worksheet?


Thanks!

dan wrote:

Hi Jerry,


Yes, to access COM you must be in OOB and with Elevated Permissions.


Thanks!

jerry wrote:

if i want to use the API  for  COM, is it must be in oob?

Post your Comments

Raymond wrote:

Hi,


It looks simple so I tried it in vb.net. Didn't work.

I duplicated all the imports (using) but no luck.

Can anyone point me out in the right direction?


I appreciate any help.


Gr,

Raymond.

Aladin wrote:

http://ebayvietnam.net

I need Export SilverLight Chart to Pdf or Excell, any everyone help me?

Chinmay wrote:

I am getting this error while calling to

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

Please help.

rhysjo wrote:

Hi,


This is a great example.  Does anyone have an example of getting a reference to the current selection?  What I'm trying to do is allow the user to select some cells in Excel and have the silverlight app perform an operatoin of the current selection.


Thanks!

Corsariovasco wrote:

Hi,


How data is entered in another Worksheet?


Thanks!

dan wrote:

Hi Jerry,


Yes, to access COM you must be in OOB and with Elevated Permissions.


Thanks!

jerry wrote:

if i want to use the API  for  COM, is it must be in oob?

Post your Comments

 
Latest Games
Zombie Escape
Apr 19, 2016
Plays: 1,404

Zombie Escape ScreenshotDrive fast before the crazy mutant zombies get you!

6 Ratings/4.1 Average
Car Parking
Jan 16, 2016
Plays: 1,371

Car Parking ScreenshotGuide the car to its parking space in this fun Car Parking game.

1 Rating/5 Average
Trash It
Jan 11, 2016
Plays: 1,257

Trash It ScreenshotAim for the Trash Can and get the various items of Trash in the bin.

4 Ratings/5 Average
Sky Fly
Jan 11, 2016
Plays: 1,377

Sky Fly ScreenshotFly your plane in this colorful vertical scrolling shoot-em-up. Blast the bad guys and collect any bonus's they leave behind.

1 Rating/5 Average
Professor Snappy
Jan 11, 2016
Plays: 1,010

Professor Snappy ScreenshotPop as many bubbles as possible in this fun and colorful bubble popping game. The levels start off easy enough but gradually get harder!

1 Rating/5 Average
Monster Match Saga
Jan 10, 2016
Plays: 1,342

Monster Match Saga ScreenshotHere we have a bunch of monsters that need to be matched up. Look out for the bomb and spinning monsters that will cause special damage!

3 Ratings/4.6 Average
Fly Bird Fly
Jan 10, 2016
Plays: 1,063

Fly Bird Fly ScreenshotGuide your friendly Bird through the maze of pipes and other obstacles collecting the Stars in this cool arcade game inspired by the legendary Flappy Bird.

1 Rating/5 Average
Life In One
Jan 10, 2016
Plays: 1,258

Life In One ScreenshotYou are stranded on an Alien planet. Your goal is to build a space rocket and escape. Start by building units to create power and mine the metal patches. Build defenses to defend your base from the advancing Aliens and Zombies!

2 Ratings/3 Average
X Pool
Jan 02, 2016
Plays: 1,761

X Pool ScreenshotPlay Pool against the computer or battle against your friends in the online mode!

3 Ratings/3 Average
Fruit Slicer
Jan 02, 2016
Plays: 1,022

Fruit Slicer ScreenshotSlice the fruit that is thrown up onto the screen. Slice the fruit into multiple pieces for maximum points!

1 Rating/5 Average