Getting started with Spire.XLS for .NET

E-iceblue Ltd. is a vendor of powerful components for .NET, Silverlight and WPF development. These products enable the user to easily read and write different formats of office files.

A quick an easy guide to setting up Spire.XLS for use in Visual Studio .NET projects is presented here.

For the purpose of clarity and helping the user get started as soon as possible, a simple ‘HelloWorld’ example is presented.

Step 1: Download and install Spire.XLS

Download from this link:

http://www.e-iceblue.com/Download/download-excel-for-net-now.html

… and run the installer.

Step 2: Create a .NET WinForms project

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:

Spire.XLS1

Step 3: Set the .NET Target Framework

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.

The 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
.NET 3.5, double-click folder NET3.5
.NET 4.0, double-click folder NET4.0

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

Spire.XLS2

Not sure how determine the target .NET framework of the project? First build your HelloWorld .NET project in order to generate the exe. Then open up a Visual Studio Command Prompt. In the command prompt navigate to where the exe is located and run the command:

ildasm HelloWorld.exe

Spire.XLS3

This will invoke ildasm:

Spire.XLS4

Double click on MANIFEST.

Look for the comment beginning with “// Metadata version“, which on my set up tells me that it is .NET version 4.0:

Spire.XLS5

Once you are sure which version of .NET is right for your project, right-click the HelloWorld project properties and make sure the ‘Target framework’ is set to ‘.NET Framework 4’ and not.NET Framework 4 Client Profile’, otherwise build errors may occur:

Spire.XLS6

Step 4: Write the event handling code

In Solution Explorer, double-click the file Form1.cs ( or Form1.vb for Visual Basic) to open the form design view:

Spire.XLS7

Add a button into the form. Change its name to ‘btnRun‘, change its text to ‘Run‘:

Spire.XLS8

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

private void btnRun_Click(object sender, EventArgs e)
{

}

Add the following code to the top of the file

using Spire.Xls;

Add the following code to the method btnRun_Click

private void btnRun_Click(object sender, EventArgs e)
{
    //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 { }
}

Step 5: Startup the project

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:

Spire.XLS9

Using a subset of the boost libraries in Windows

For many C++ applications I very much want to use the Boost libraries, just not the whole set.

This post explains how to use the bcp tool to create a subset of the Boost libraries in order to leave out unnecessary libraries and tailor it to the needs of your project. One nice thing about bcp is that it can be run at any time so as to update your Boost dependencies as and when the need arises.
Continue reading ‘Using a subset of the boost libraries in Windows’ »

Converting a SYSTEMTIME to a std::string in C++

A short recipe outlining how to output a SYSTEMTIME value as a std::string.

The example format will be “YYYY-MM-DD HH:MM:SS.MMM”

I specifically wanted to include milliseconds.

In this example I employ three possible techniques:

1. MFC CString
2. std::ostringstream
3. sprintf
Continue reading ‘Converting a SYSTEMTIME to a std::string in C++’ »

Using boost::filesystem

Examples of using boost::filesystem for various things as and when I encounter them…

Many have been lifted from the StackOverflow site.

For reasons of brevity and clarity I generally avoid extraneous code such as exception handling etc in these examples and just focus on the techniques themselves.


1. Copying a directory

This recursively uses boost::filesystem::create_directory to create a copy of a directory including its contents, sub-directories etc.

For example the MyStuff folder:

BoostCreateDirectory1

Use the following code snippet to re-create a copy of the MyStuff folder, and rename it MyStuff2:
Continue reading ‘Using boost::filesystem’ »

Installing and using OpenSSL SHA-256 in Visual C++

Background

SHA-256 is a cryptographic hash function developed by the US. National Security Agency (NSA) as a U.S. Federal Information Processing Standard (FIPS). The SHA acronym stands for Secure Hash Algorithm. A hash function operates on an arbitrary amount of data and returns a fixed-size bit string, the cryptographic hash value.

So why would such an algorithm be useful? Its utility lies in the fact that attempts to change the original data will almost certainly change the hash value returned by the algorithm. An obvious application would be to apply such an algorithm to a file (text or binary) as a means of determining whether or not it has been tampered with.

This post shows you how to use SHA-256 as implemented by the OpenSSL open source project, and use it within Windows / Visual C++ environments to produce digital signatures of strings or files.
Continue reading ‘Installing and using OpenSSL SHA-256 in Visual C++’ »

Reading the status of DVD drives in C++

A short and to-the-point C++ recipe for determining the status of your PC / laptop DVD.

To study this area in more depth, follow this link for more information, which is where I got much of this inspiration for this code snippet. It works.

http://ws0.org/windows-how-to-get-the-tray-status-of-the-optical-drive/

Full code listing
Continue reading ‘Reading the status of DVD drives in C++’ »

Creating silent installs of audio and video codecs using K-Lite

Suppose you wish to implement a silent installation of audio and video codecs for windows. It may be that you wish to execute this as a pre-requisite for a main application you are installing, while avoiding dialog windows or command prompts from appearing while it is doing this. Hence the use of so-called ‘silent’ or ‘unattended’ installations. This post shows you how to create a silent installation of K-Lite audio/video codecs.
Continue reading ‘Creating silent installs of audio and video codecs using K-Lite’ »

How to use the Boost compiled libraries in Windows

As stated on the boost.org Getting Started for Windows page, most Boost libraries are header-based that require no no separate building. But there exist some Boost libraries that require a separate compilation in order to use them. This page essentially reiterates what is already explained in section 5.2.1 of the Getting Started page, but with additional screenshots to make it easier for newcomers.

A previous post dealt with the separate compilation of Boost library binaries using BoostPro, but this package is no longer supported, alas.
Continue reading ‘How to use the Boost compiled libraries in Windows’ »