Vectorlight News

  • Chat App Converted to HTML and JQuery
    Sep 08, 2011

    Converted from Silverlight to HTML and Javascript/JQuery is the Vectorlight Chat App. Login using your Vectorlight password to chat using your username and avatar.

  • HTML5 iPhone,Android Big Guns Tower Defense
    Jul 02, 2011

    Big Guns has made the leap from Windows Phone 7 (XNA) to HTML5 so you can now play it on your iPhone, Android and other HTML5 compatible devices.

  • HTML5 Games - Word Poppers and Batty
    Jun 04, 2011

    As the take-up of HTML5 quickens (74% of users currently have a browser capable of HTML5 Canvas) we present two more games for both your browser and mobile.

  • Big Guns Tower Defense on Windows Phone 7
    May 06, 2011

    Coming soon to Windows Phone 7 is an XNA port of the popular Vectorlight tower defense game Super Tower Defense. Whilst retaining many of the graphical and gameplay features of the original Silverlight game.

  • Wakacube WP7 Update
    Apr 26, 2011

    Released to the Windows Phone 7 marketplace today is Version 1.1 of Wakacube the 3D physics game of skill. Included in the update are more levels (30 in total) and new mode Wakatime which generates random crate structures to keep players entertained long after the levels have been completed.

  • Home Page News

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.

Your Comments

Aladin posted

http://ebayvietnam.net


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


Chinmay posted

I am getting this error while calling to


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


Please help.


rhysjo posted

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 posted

Hi,



How data is entered in another Worksheet?



Thanks!


dan posted

Hi Jerry,



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



Thanks!


jerry posted

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


 

Post your Comments

Rate this: 

1 Star 2 Star 3 Star 4 Star 5 Star
16 Ratings / 3.6 Average

Tweets

Silverlight Controls

  • Rich TextBox

    Create and edit rich content with this slick and expandable Rich TextBox...

  • TreeView

    This easy to use TreeView comes with drag and drop, sorting, searching and much more...

  • Context Menu

    You too can have cool popup context menus in your Silverlight applications...

  • Resizable Dialog

    Draggable and resizable popup dialogs are what serious Silverlight developers need...

  • Spell Checker

    Real-time spell checking in Silverlight? We did it first here...