Getting Started
Getting Started

Getting Started (3)

This program guide focuses on helping you start with a simple demo, which shows how to create a new Excel workbook, by using Spire.XLS on Silverlight. At beginning, make sure that Spire.XLS for Silverlight, Visual Studio and Silverlight are installed on your computer.

1. Create Silverlight Project - Click File to Add New Project in Visual Studio. Then, select Visual C# or Visual Basic. Choose Silverlight. Name this project. In this example, I name it CreateWorkbook-xls. After Clicking OK, a dialog box pops up, set if host Silverlight application in a new Web site and select your Silverlight Version. Click OK to finish creating.

2. Add Reference - Right click reference to add reference. Choose appropriate Spire.XLS dll version from your computer. If your Silverlight Version is Silverlight 4, please choose dll file in Silverlight4 folder from Spire.XLS Bin folder.

3. Design MainPage - Double click MainPage.xaml and then design UserControl. Firstly, add a label in UserControl and change its contents. Also, set label contents format, including font style and color. Next, add a button for running demo.

[MainPage.xaml]
<UserControl x:Class="CreateWorkbook_xls.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="233" d:DesignWidth="564" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" Background="#FF8AA874" Loaded="UserControl_Loaded">

    <Grid x:Name="LayoutRoot" Height="228" Width="562">
        <Button Content="RUN" Height="36" HorizontalAlignment="Left" Margin="462,164,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <sdk:Label Height="50" HorizontalAlignment="Center" Margin="10,64,0,114" Name="label1" VerticalAlignment="Center" Width="552" Content="This example shows how to create an Excel Workbook with Spire.XLS for Silverlight." FontFamily="Arial" FontSize="13" Foreground="White" FontWeight="Bold"><"/sdk:Label>
     <Grid.Background>
      <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
        <GradientStop Color="Black" Offset="0" />
        <GradientStop Color="#FF28A2E2" Offset="1" />
      </LinearGradientBrush>
    </Grid.Background>
   </Grid>
</UserControl>

4. Declare SaveFileDialog - Declare a saveFileDialog to save Excel Workbook. In this saveFileDialog, you also need to set a filter for selecting Excel version. In this example, the Excel extension I set includes .xls and .xlsx.

[MainPage.xaml.cs]
        private SaveFileDialog saveFile =null;
        public MainPage()
        {
            InitializeComponent();
            this.saveFile = new SaveFileDialog();
            this.saveFile.Filter = "Excel Document(*.xls)|*.xls|Excel 2007-2010(*.xlsx)|*.xlsx";
        }
[MainPage.xaml.vb]
        Private saveFile As SaveFileDialog = Nothing
        Public Sub New()
            InitializeComponent()
            Me.saveFile = New SaveFileDialog()
            Me.saveFile.Filter = "Excel Document(*.xls)|*.xls|Excel 2007-2010(*.xlsx)|*.xlsx"
        End Sub

5. Create Workbook and Edit - Create a new workbook and then edit worksheet in this workbook. Firstly, merge cells A1 to C1. Then, add text in the merged cell. Finally, set format for this cell, including font style, size and color.

[MainPage.xaml.cs]
            Workbook workbook = new Workbook();
            workbook.Worksheets[0].Range["A1:C1"].Merge();
            workbook.Worksheets[0].Range["A1"].Text = "Hello World";
            workbook.Worksheets[0].Range["A1"].Style.Font.FontName = "Calibri";
            workbook.Worksheets[0].Range["A1"].Style.Font.Size = 14;
            workbook.Worksheets[0].Range["A1"].Style.Font.Color = Colors.Red;
[MainPage.xaml.vb]
            Dim workbook As New Workbook()
            workbook.Worksheets(0).Range("A1:C1").Merge()
            workbook.Worksheets(0).Range("A1").Text = "Hello World"
            workbook.Worksheets(0).Range("A1").Style.Font.FontName = "Calibri"
            workbook.Worksheets(0).Range("A1").Style.Font.Size = 14
            workbook.Worksheets(0).Range("A1").Style.Font.Color = Colors.Red

6. Save Workbook - Judge if the saveFileDialog which I declare in step 4 can pop up after clicking Run button. If yes, save workbook by using save file dialog.

[MainPage.xaml.cs]
            bool? result = this.saveFile.ShowDialog();
            if(result.HasValue&&result.Value)
            {
                using (Stream stream = this.saveFile.OpenFile())
                {
                    workbook.SaveToStream(stream);
                }
            }
[MainPage.xaml.vb]
            Dim result? As Boolean = Me.saveFile.ShowDialog()
            If result.HasValue AndAlso result.Value Then
                Using stream As Stream = Me.saveFile.OpenFile()
                    workbook.SaveToStream(stream)
                End Using
            End If
[Full MainPage.xaml.cs]
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.IO;
using Spire.Xls;

namespace CreateWorkbook_xls
{
    public partial class MainPage : UserControl
    {
        private SaveFileDialog saveFile =null;
        public MainPage()
        {
            InitializeComponent();
            this.saveFile = new SaveFileDialog();
            this.saveFile.Filter = "Excel Document(*.xls)|*.xls|Excel 2007-2010(*.xlsx)|*.xlsx";
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Create and Edit Workbook
            Workbook workbook = new Workbook();
            workbook.Worksheets[0].Range["A1:C1"].Merge();
            workbook.Worksheets[0].Range["A1"].Text = "Hello World";
            workbook.Worksheets[0].Range["A1"].Style.Font.FontName = "Calibri";
            workbook.Worksheets[0].Range["A1"].Style.Font.Size = 14;
            workbook.Worksheets[0].Range["A1"].Style.Font.Color = Colors.Red;

            //Save Workbook
            bool? result = this.saveFile.ShowDialog();
            if(result.HasValue&&result.Value)
            {
                using (Stream stream = this.saveFile.OpenFile())
                {
                    workbook.SaveToStream(stream);
                }
            }
        }
    }
}
[Full MainPage.xaml.vb]
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.IO
Imports Spire.Xls

Namespace CreateWorkbook_xls
    Partial Public Class MainPage
        Inherits UserControl
        Private saveFile As SaveFileDialog = Nothing
        Public Sub New()
            InitializeComponent()
            Me.saveFile = New SaveFileDialog()
            Me.saveFile.Filter = "Excel Document(*.xls)|*.xls|Excel 2007-2010(*.xlsx)|*.xlsx"
        End Sub

        Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            'Create and Edit Workbook
            Dim workbook As New Workbook()
            workbook.Worksheets(0).Range("A1:C1").Merge()
            workbook.Worksheets(0).Range("A1").Text = "Hello World"
            workbook.Worksheets(0).Range("A1").Style.Font.FontName = "Calibri"
            workbook.Worksheets(0).Range("A1").Style.Font.Size = 14
            workbook.Worksheets(0).Range("A1").Style.Font.Color = Colors.Red

            'Save Workbook
            Dim result? As Boolean = Me.saveFile.ShowDialog()
            If result.HasValue AndAlso result.Value Then
                Using stream As Stream = Me.saveFile.OpenFile()
                    workbook.SaveToStream(stream)
                End Using
            End If
        End Sub
    End Class
End Namespace

Result Screenshot

This article is designed to give you a detailed description about how to write a "HelloWorld" demo of Spire.XLS for WPF by Visual Studio. You can follow the below procedure step by step to finish this task. But first of all, please make sure that Spire.XLS for WPF and Visual Studio 2010 are correctly installed on system.

Step 1. Create a new project.

1. Create a new project by choosing WPF Application in Visual Studio and name the project "HelloWorld". If you want to create a C# project, select Visual C#, WPF Application, if you want to build a Visual Basic project, please choose Visual Basic, WPF Application. The detail process is:

Click File → NewProjectWPF ApplicationNameOK

2. Set the Target framework property of the HelloWorld project in Solution Explorer to be .NET Framework 4.The process is:

Click Solution Explorer-> HelloWorld (The project that you already built) -> Properties (right click HelloWorld)->Target framework->.NET Framework 4

3. Add a button in MainWindow. The default button name is "button1". You can set button1 Content property to be "Run" in its properties by right clicking it. Thus, it shows "Run" in the MainWindow. You can perform as below:

Click ViewToolboxButtonProperties (right click button1)Contentset the Content to be "Run"

Step 2. Add reference and project namespaces.

1. Add Spire.XLS. Wpf.dll as reference in Project. The Default location of Spire.Doc for WPF is “C:\Program Files\e-iceblue\Spire.XLS for WPF”. Details are:

Click ProjectAdd ReferenceBrowseChoose the folder contains Spire.XLS for WPFBinWPF 4.0Spire.XLS.Wpf.dll

2. Double click the "Run" button, you can see the following method has been added automatically:

[C#]
namespace HelloWorld
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
        }
    }
}
[VB.NET]
Namespace HelloWorld
	''' 
	''' Interaction logic for MainWindow.xaml
	''' 
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
                           End Sub
	 End Class
End Namespace

3. Add the below namespaces at the top of the method.

[C#]
using Spire.Xls;
[VB.NET]
Imports Spire.Xls

Step 3. Write "Hello, World!" in the method.

Add the following code to the method button1_click

[C#]
           //create a new excel workbook
            Workbook workbook = new Workbook();

            //edit the first worksheet of the workbook
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Range["A1"].Text = "Hello, World!";

            //save the workbook
            workbook.SaveToFile(@"..\..\sample.xls", ExcelVersion.Version97to2003);

            //launch the workbook
            System.Diagnostics.Process.Start(@"..\..\sample.xls");
[VB.NET]
	  'create a new excel workbook
	   Dim workbook As New Workbook()

	  'edit the first worksheet of the workbook
	   Dim sheet As Worksheet = workbook.Worksheets(0)
	   sheet.Range("A1").Text = "Hello, World!"

	   'save the workbook
	    workbook.SaveToFile("..\..\sample.xls", ExcelVersion.Version97to2003)

	   'launch the workbook
	    System.Diagnostics.Process.Start("..\..\sample.xls")

Step 4. Debug the project

Right click the project HelloWorld in Solution Explorer  Debug  Start new instance  Click Run in MainWindow, an Excel Document will be created, edited and opened. The string "Hello, World!" is drawn in the first cell A1 of sheet 1.

Preview

In this document, we will quickly help you finish a simple demo about Spire.XLS using Visual Studio. As usual, it’s a HelloWorld demo. Before you get started, please make sure the Spire.XLS and Visual Studio (2008 or later) are installed on your computer.

1. In Visual Studio, click File, New, and then Project, If you want to create a C# project, select Visual C#, Windows and choose Windows Forms Application and name the project HelloWorld. Click OK. If you want to create a Visual Basic project, select Visual Basic, Windows Forms Application and name the project HelloWorld. Click OK.

2. In Solution Explorer, right-click the project HelloWorld and click Add Reference. In the Browse tab, find the folder in which you installed the Spire.XLS, default is "C:\Program Files\e-iceblue\Spire.XLS", double-click the folder Bin. If the target framework of the project HelloWorld

  • is .NET 2.0, double-click folder NET2.0
  • is .NET 3.5, double-click folder NET3.5
  • is .NET 4.0, double-click folder NET4.0

select assembly Spire.XLS.dll and click OK to add it to the project.

3. In Solution Explorer, double-click the file Form1.cs/Form1.vb to open the form design view, add a button into the form, and change its name to 'btnRun', change its text to 'Run'.

4. Double-click the button 'Run', you will see the code view and the following method has been added automatically:

[C#]
private void btnRun_Click(object sender, EventArgs e)
[VB.NET]
Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click

5. Add the following codes to the top of the file

[C#]
using Spire.Xls;
[VB.NET]
Imports Spire.Xls

6. Add the following codes to the method btnRun_Click

[C#]
//Creates workbook
Workbook workbook = new Workbook();

//Gets first worksheet
Worksheet sheet = workbook.Worksheets[0];

//Writes hello world to A1
sheet.Range["A1"].Text = "Hello,World!";

//Save workbook to disk
workbook.SaveToFile("Sample.xls");

try
{
System.Diagnostics.Process.Start(workbook.FileName);
}
catch { }
[VB.NET]
'Creates workbook
Dim workbook As Workbook = New Workbook()

'Gets first worksheet
Dim sheet As Worksheet = workbook.Worksheets(0)

'Writes hello world to A1
sheet.Range("A1").Text = "Hello,World!"

'Save workbook to disk
workbook.SaveToFile("Sample.xls")

Try
System.Diagnostics.Process.Start(workbook.FileName)
Catch
End Try

7. In Solution Explorer, right-click the project HelloWorld and click Debug, then Start new instance, you will see the opened window Form1, click the button 'Run', an Excel document will be created, edited and opened. The string "Hello, World" is filled in the cell A1.