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 → New → Project → WPF Application → Name → OK
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 View → Toolbox → Button → Properties (right click button1) → Content → set 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 Project → Add Reference → Browse → Choose the folder contains Spire.XLS for WPF → Bin → WPF 4.0 → Spire.XLS.Wpf.dll
2. Double click the "Run" button, you can see the following method has been added automatically:
namespace HelloWorld { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { } } }
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.
using Spire.Xls;
Imports Spire.Xls
Step 3. Write "Hello, World!" in the method.
Add the following code to the method button1_click
//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");
'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