Canalblog
Editer l'article Suivre ce blog Administration + Créer mon blog
Publicité
SpireXLS
29 mai 2012

With Silverlight - How to Insert Image in Excel

Although Excel is used to save, display and calculate data, people can add other elements in Excel file, for example, image. Why do people insert image in Excel? Actually, comparing with data information only, image can make Excel appearance more appealed. Also, some images are related to data which is displayed in Excel. For example, people will insert product pictures in Excel if this file shows sales information about this product.

In this post, I want to introduce a method about how to insert an image in Excel with Silverlight. Also, Spire.XLS for Silverlight is used in this method for realizing this function more easily and quickly. So, I have added its dll file as reference in my project at the beginning.

STEPS:

Step 1. Design User Control

Double click InsertImage.xaml and then design UserControl. Firstly, add a label and change content. Set label background and text format (font type and size). Secondly, add a button RUN. Set text as bold type. Thirdly, set background color for whole UserControl.

InsertImage.xaml

<UserControlx:Class="ExcelImage.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="211"d:DesignWidth="549"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"Background="Black"BorderBrush="#FF5AD4AE"Loaded="UserControl_Loaded">

 

    <Gridx:Name="LayoutRoot"Height="211"Width="548"Background="Black">

        <sdk:LabelHeight="71"HorizontalAlignment="Center"Margin="59,48,60,92"Name="label1"VerticalAlignment="Center"Width="429"Content="Insert Image in Excel File with Silverlight"FontSize="24"FontFamily="Times New Roman"Foreground="Black"BorderBrush="#FF051E8A"Background="White" />

        <ButtonContent="RUN"Height="39"HorizontalAlignment="Left"Margin="456,145,0,0"Name="button1"VerticalAlignment="Top"Width="56"FontWeight="Bold"FontFamily="Arial Black"Click="button1_Click" />

    Grid>

UserControl>

Step 2. Load Image

Before inserting image in Excel, I need to add this image as embedded resource in project and then load it. At first, right click project to add existed item (image) then set its Build Action as embedded resource. Then, find loaded event in UserControl Events and double click it to write code.

Get image name from assembly. Judge if the name is the same as image name. If yes, save this image to stream.

        private void UserControl_Loaded(object sender, RoutedEventArgs e)

        {

            Assembly assembly = this.GetType().Assembly;

            foreach (String name in assembly.GetManifestResourceNames())

            {

                if (name.EndsWith("houses.jpg"))

                {

                    stream = assembly.GetManifestResourceStream(name);

                }

            }

        }

Step 3. Declare saveFileDialog

Declare a new Excel workbook, saveFileDialog and stream in MainPage class. Then, create a new workbook and define its Excel version. Next, set saveFileDialog filter for choosing which kind of format (Excel Version) I will save.

        Workbook workbook = null;

        SaveFileDialog saveFileDialog = new SaveFileDialog();

        Stream stream = null

        public MainPage()

        {

            InitializeComponent();

            this.workbook = new Workbook();

            workbook.Version = ExcelVersion.Version2010;

            this.saveFileDialog.Filter = "Excel Document(*.xlsx)|*.xlsx";

        }

Step 4. Insert Image

Double click RUN button and write code to insert image. Create an empty worksheet and initialize this worksheet. Next, use sheet.Pictures.Add() method to insert image. Three parameters are passed to this method, top row, left column and stream which saves image.

            workbook.CreateEmptySheets(1);

            Worksheet sheet = workbook.Worksheets[0];

            sheet.Pictures.Add(1, 1, stream);

Step 5. Save File

Judge if the saveFiledialog which has been declared can pop up. If yes, save the Excel file by using save file dialog box.

            bool? result = this.saveFileDialog.ShowDialog();

            if (result.HasValue && result.Value)

            {

                using (Stream file = this.saveFileDialog.OpenFile())

                {

                    this.workbook.SaveToStream(file);

                }

            }

Full InsertImage.xaml.cs

using System;

using System.Windows;

using System.Windows.Controls;

using System.IO;

using System.Reflection;

using Spire.Xls;

namespace ExcelImage

{

    public partial class MainPage : UserControl

    {

        Workbook workbook = null;

        SaveFileDialog saveFileDialog = new SaveFileDialog();

        Stream stream = null

        public MainPage()

        {

            InitializeComponent();

            this.workbook = new Workbook();

            workbook.Version = ExcelVersion.Version2010;

            this.saveFileDialog.Filter = "Excel Document(*.xlsx)|*.xlsx";

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            workbook.CreateEmptySheets(1);

            Worksheet sheet = workbook.Worksheets[0];

 

            sheet.Pictures.Add(1, 1, stream);

 

            bool? result = this.saveFileDialog.ShowDialog();

            if (result.HasValue && result.Value)

            {

                using (Stream file = this.saveFileDialog.OpenFile())

                {

                    this.workbook.SaveToStream(file);

                }

            }

        }

 

        private void UserControl_Loaded(object sender, RoutedEventArgs e)

        {

            Assembly assembly = this.GetType().Assembly;

            foreach (String name in assembly.GetManifestResourceNames())

            {

                if (name.EndsWith("houses.jpg"))

                {

                    stream = assembly.GetManifestResourceStream(name);

                }

            }

        }

    }

}

RESULT

__________________________________________________________________________

Click Here to LEARN MORE about Spire.XLS for Silverlight

Click Here to DOWNLOAD Spire.XLS for Silverlight

Spire.Office also can be used to realize this function

Publicité
Publicité
Commentaires
SpireXLS
Publicité
Publicité