Interacting with Microsoft Excel from C# using EPPlus – Part 1

I’ve been working on moving the reporting functionality of an existing application from HTML to Microsoft Excel format. I find HTML fine for creating simple reports or sometimes log files from C# applications, but these reports were churning out at around 50MB, which Internet Explorer was having serious problems dealing with. So, I decided to solve this issue by moving all reporting functionality in the application to Microsoft Excel format, which with hindsight I would have done in the original design.

EPPlus is an open source .NET library for reading an writing Excel files. I’ve used this in many projects, and have found it invaluable when the requirement to either read or write Excel files crops up. In this post, I’ll give examples of using EPPlus to write to a Microsoft Excel file.

Setup

Create a new C# console application project in Visual Studio. Download the EPPlus binaries from the Downloads section on the EPPlus CodePlex Site.

Extract the files and add EPPlus.dll as a reference to your project.

Writing to Excel

EPPlus writes Excel 2007/2010 files using the Open Office Xml format (xlsx). The first thing to do, after the initial setup has been completed, is to add the following imports to your code:

using OfficeOpenXml;
using OfficeOpenXml.Style;

Next, let’s create a new ExcelPackage and add some properties to it such as the author, title and company:

using (ExcelPackage p = new ExcelPackage())
{
    p.Workbook.Properties.Author = "Miles Dyson";
    p.Workbook.Properties.Title = "SkyNet Monthly Report";
    p.Workbook.Properties.Company = "Cyberdyne Systems";
    
    // The rest of our code will go here...

}

Now we’ll need to create a new worksheet where we will add our data:

    p.Workbook.Worksheets.Add("April 2012");
    ExcelWorksheet ws = p.Workbook.Worksheets[1]; // 1 is the position of the worksheet
    ws.Name = "April 2012";

We’ll be adding some simple data to this worksheet, contained in 3 columns. We might want to add a header to this worksheet with some column names, and some basic formatting, like making the column header background color something different.

This is simple to achieve using EPPlus:

    int rowIndex = 1;
    int colIndex = 1;

    do
    {
        // Set the background colours
        var cell = ws.Cells[rowIndex, colIndex];
        var fill = cell.Style.Fill;
        fill.PatternType = ExcelFillStyle.Solid;
        fill.BackgroundColor.SetColor(Color.LightGray);
        colIndex++;
    }
    while (colIndex != 4);

    // Set the cell values
    var cell_actionName = ws.Cells[1, 1];
    var cell_timeTaken = ws.Cells[1, 2];
    var cell_processorsUsed = ws.Cells[1, 3];

    cell_actionName.Value = "Action Name";
    cell_timeTaken.Value = "Time Taken";
    cell_processorUsed.Value = "Processing Unit";

The above two actions will be quite common if you use EPPlus to write to Excel files in a lot of different projects. I’d recommend created a static helper class to perform both of these functions (adding the properties and creating a header), I’ve done this with these and other common functions, and I’ve found it’s saved me some time.

Note that we haven’t actually saved the Excel file yet, it’s in memory but we haven’t saved it to disk. Before we do, let’s add some data to it as well as the header. For the purpose of this example, let’s say we already have the data (wherever it may have come from), defined as a List of hypothetical ProcessorAction Objects.

In order to write the data to the file, we can just iterate over this List and write a new row for each ProcessorAction Object to the Excel file:

    // Get hypothetical data...
    List processorActions = DataAccessHelper.GetProcessorData(DataTime.Now);
    
    // Column indexes for clarity
    int actionColIndex = 1;
    int timeColIndex = 2;
    int processorColIndex = 3;

    int rowIndex = 2; // Row 1 is the header

    foreach(ProcessorAction p in processorActions)
    {
        // Action
        var actionCell = ws.Cells[rowIndex, actionColIndex];
        actionCell.Value = p.Action;        

        // Time
        var timeCell = ws.Cells[rowIndex, timeColIndex];
        timeCell.Value = p.Time;
        
        // Processor
        var processorCell = ws.Cells[rowIndex, processorColIndex];
        processorCell.Value = p.Processor;

        rowIndex++;
    }

Now that all our data is written, we want to save the Excel file for distribution:

    // Save the Excel file
     Byte[] bin = p.GetAsByteArray();
     File.WriteAllBytes(@"C:\Reports\Report.xlsx, bin);

Your file should save successfully. That’s the basics of writing to Excel files using EPPlus. In the next post, I’ll outline how to read data contained in an Excel file into memory.

Happy Coding 😉

Interacting with web pages using Selenium WebDriver for C#

I’ve been using Selenium WebDriver for C# a lot lately, for a number of projects that involved interacting with a web browser in some manner. I’ve used a lot of applications and libraries over the past few years that provide this functionality, but I’ve never come across one as intuitive and reliable as Selenium WebDriver – if you work on any projects that involve interacting with a web browser to automate some process, you need to read this post.

In this post I’ll take you through the process of using Selenium WebDriver to automate some interaction with a web browser and hopefully show you how powerful Selenium is. We’ll take a simple scenario as an example – submitting a request to the Google search engine.

Boot up Visual Studio and create a new C# console application. You’ll need to download the Selenim WebDriver for C# ZIP from Google Code, and add the DLL’s it contains as references to your project. For this example, you are only required to add WebDriver.dll and Newtonsoft.Json.Net35.dll. You’ll need to add the following using statements also:

using OpenQA.Selenium;
using OpenQA.Selenium.IE;

Now your ready to write some code that can drive your web browser. First, we’ll need to create the object that can do just that. With Selenium WebDriver, that is an IWebDriver object. This object can be instantiated to control Internet Explorer, Firefox, or Chrome. In this example, I’m going to use Internet Explorer.

IWebDriver driver = new InternetExplorerDriver();

You could also use ChromeDriver or FirefoxDriver above.

Next, let’s navigate to the Google website. The following code will handle this. One of the nice things about Selenium is that this call won’t return until the page is loaded in the browser. Other frameworks and libraries return immediately, requiring you to add waits/sleeps in your code to ensure the page is actually loaded, which is a terrible approach.

driver.Navigate().GoToUrl("http://www.google.com");

Aside – Internet Explorer Problems

One setting has to be changed in Internet Explorer in order for Selenium WebDriver to work correctly. Your security zones need to have protected mode either enabled or disabled – it doesn’t matter which, as long as it’s the same for each zone. I’ve got it set to on for each zone on my machine. To achieve this follow these steps:

  1. Open IE and go to Internet Options.
  2. Go to the ‘Security’ tab.
  3. Click on each of the zones, i.e. ‘Internet’, ‘Local intranet’, ‘Trusted sites’ and ‘Restricted sites’ and ensure that the ‘Enable Protected Mode (requires restarting Internet Explorer)’ check box is checked.

At this point, build your project in Visual Studio and run it. If you have followed the above steps correctly you will see Internet Explorer open, and navigate to Google automatically.

Now to actually interact with the open web page, Google in this case. In order to submit a search term, we’ll need to interact with the search term text box (to enter a search term), and the ‘Google Search’ button (to submit the request).

To do this, we’ll need to look at the source code for the page, to find the information we’ll need to interact with these controls. From looking at the source code, we can see that the markup for these controls looks like the following (formatted and commented for clarity):

Google Search

So, lets define the search term text box, and enter a search search:

IWebElement searchTermTB = driver.FindElement(By.Name("q"));
searchTermTB.SendKeys("jimmy collins blog");

Take note of what we’re doing here – we’re using the browser object we defined earlier to find an element with the name ‘q’. This is another great thing about Selenium – we can use just about any element attribute to try to find it, you could also use the ID, class name, tag name, or even the XPATH to find an element on the page.

Now build and run your application – you will once again see Internet Explorer opening up, but this time you’ll also see the search term being entered.

The final step is to actually click the ‘Google Search’ button and submit the query. The same approach that we used to find and interact the search term text box is followed:

IWebElement searchBtn = driver.FindElement(By.Name("btnG"));
searchBtn.Click();

Running your application now will open up Google, enter your search term, and hit the search button. The final thing to do is some cleanup – you will notice that currently when your application runs, the browser is left open once it completes. All we have to do to close the browser is:

driver.Close();

That’s how easy Selenium is to use. The ideal scenario is that you have interaction with your development team, and get them to agree to providing static IDs on all controls, that don’t change between versions of your site (unless in the case of a substantial UI revamp). That would make it a simple task to provide re-usable automation that can automatically verify changes to your site, or be used for regression testing.

Java Needs Automatic Properties

One of my main grievances with the Java programming language is it’s lack of Automatic Properties. These have existed in C# since .NET 3.0.

Automatic Properties make property-declaration more concise when no additional logic is required in the property accessors. For example, let’s say we want to implement a simple Book abstraction in an application, with four simple properties for arguments sake.

This is how it may look in C#:

public class Book
{
    public string Name { get; set; }

    public string Author { get; set; }

    public string ISBN { get; set; }

    public string Genre { get; set; }
}

Nice and simple, and written in about 1 minute when coupled with Visual Studio’s intellisense etc.

Now, lets take a look at a class offering the same basic functionality in Java:

public class Book 
{
    public String Name;
    public String Author;
    public String ISBN;
    public String Genre;
    
    public void setName(String name)
    {
        Name = name;
    }
    
    public String getName()
    {
        return Name;
    }
    
    public void setAuthor(String author)
    {
        Author = author;
    }
    
    public String getAuthor()
    {
        return Author;
    }
    
    public void setISBN(String isbn)
    {
        ISBN = isbn;
    }
    
    public String getISBN()
    {
        return ISBN;
    }
    
    public void setGenre(String genre)
    {
        Genre = genre;
    }
    
    public String getGenre()
    {
        return Genre;
    }
}

Just on a lines of code basis, it’s easy to see that C# wins overall. I understand that the designers of Java may not want to add this functionality to the language due to potentially breaking millions of current Java applications – but I’m sure it could be added in such a way that new applications could use it without breaking legacy applications.

Hell, a third party library, Project Lombok, already provides support for using Automatic Properties in Java, so it’s definitely possible.

I find this limitation really frustrating when working with Java.

Taking screenshots with Selenium WebDriver

Time to do something a bit more useful using Selenium WebDriver. Taking screenshots as we cycle through our web pages has obvious advantages for unit testing. Firstly we can just run an application overnight, have it visit every page on our website, and review the screenshots for obvious bugs the next morning before releasing to QA. Secondly, we can screenshot our localized websites, and have them linguistically reviewed, or hand off the English screenshots to our translators to provide context during the translation process.

Taking screenshots with WebDriver is exceptionally easy. The thing I like about it most, is that it takes a screenshot of just the area of the browser in which the content resides – no IE/FF/Chrome toolbars etc. But, it also takes the entire webpage – even content not visible on the screen at that time. This is a great feature. I’ve had the problem in the past with long web pages, ones for example that contain a EULA or other long document, where the screenshots had to be taken at intervals as the page scrolled. Obviously not the best process, and not very reusable code.

As mentioned in a previous post, I’m using the Selenium Client Driver for C#. I’ve created a library into which I’m adding useful functions like taking screenshots etc.

The code to take a screenshot is not complex:

using OpenQA.Selenium.IE;
using OpenQA.Selenium;
using System.Drawing.Imaging;

public void TakeScreenshot(IWebDriver driver, string saveLocation)
{
    ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;
    Screenshot screenshot = screenshotDriver.GetScreenshot();
    screenshot.SaveAsFile(saveLocation, ImageFormat.Png);
}

The libraries referenced can be found in the Selenium Client Driver for C# package which can be downloaded from the Selenium website. These will need to be added to your C# project.

Notice in the above function we pass an ‘IWebDriver’ Object, as opposed to a specific InternetExplorerDriver, FirefoxDriver etc. This means this function can be used regardless of which browser you are dealing with.

Using the function is also a trival matter. Let’s browse to Google and take a screenshot, saving it in the root of the ‘C:\’ drive:

IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.google.com");
TakeScreenshot(driver, @"C:\screenshot.png");

Note that the TakeScreenshot function can be customized to use any image format – in fact it should probably be a function parameter. I chose PNG simply because of the small size of the generated image.

Running Selenium Tests with C# & NUnit

Something I’m working on currently requires some automation of a web browser, so what a perfect opportunity to get some exposure to Selenium.

In this post I’ll outline the basics of creating and running a simple Selenium test using Selenium and NUnit. The implementation language will be C#. To get started, you will need to download the following:

Extract the Selenium Client Driver files, these DLL’s will be referenced in the Visual Studio project we create. Install NUnit using the .msi installer.

Now, let’s create the actual test:

  1. Launch Visual Studio 2010 and create a new class library project.
  2. Add a reference to nunit.framework.dll. This can be found under the NUnit installation directory at ‘bin\net-2.0\framework’.
  3. Add references to all the DLL’s contained in the Selenium Client Driver package you downloaded earlier.
  4. We’re now ready to add the code that will run a Selenium test. Add the following code to your class library project:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;

namespace FirstSeleniumTest
{
    [TestFixture]
    public class SeleniumTest
    {
        private IWebDriver driver;

        [SetUp]
        public void SetUp()
        {
            driver = new InternetExplorerDriver();
        }

        [Test]
        public void TestGoogle()
        {
            driver.Navigate().GoToUrl("http://www.google.com");
        }

        [TearDown]
        public void TearDown()
        {
            driver.Quit();
            driver.Dispose();
        } 
    }
}

The above code should be pretty easy to understand. Notice the annotations around the functions.

  • [SetUp] – This is where any test setup should be completed. In the above example we’re creating a new instance of InternetExplorerDriver, setting it up for our test to run later.
  • [Test] – This is where the test steps are defined. In this example, we’re just navigating to Google.
  • [TearDown] – In this section, any steps to be taken to cleanup the environment after your test has run can be defined. Here, all we’ll do is close Internet Explorer

Now that we have written a simple test, let’s try running it using NUnit. Before moving on, ensure that the above code builds successfully in your environment.

To run the test it’s just a matter of launching NUnit and opening up the DLL built from the Visual Studio project created above. You should see the ‘TestGoogle’ test listed. Simply select the test and hit the ‘Run’ button to initiate the test. You will see Internet Explorer launch, and then close.

NUnit

One thing you may need to do, depending on your IE version, is to disable protected mode for Internet and Restricted Zones in Internet Explorer Security Settings (don’t forget to re-enable these once you’ve finished experimenting with Selenium).

In a future post, I’ll outline how to do more complex tasks during your tests such as taking screenshots and navigating around web pages under tests.

Refactoring horrible nested if-else statements

I wrote this at some point this week, I was looking back at it tonight and released how truly awful it looks:

if(filePath.Contains(".CSS"))
    return true;
else
    return false;
else if(filePath.Contains(".JS"))
    return true;
else
    return false;
else if(filePath.Contains("_STR"))
    return true;
else
    return false;
else if(filePath.Contains(".VBS"))
    return true;
else
    return false;
else if(filePath.Contains(".HTM"))
    return true;
else
    return false;
else if(filePath.Contains(".BMP"))
    return true;
else
    return false;
else if(filePath.Contains("GIF"))
    return true;
else
    return false;

Terrible huh? What was I thinking?

How much better does this look:


bool copyFile;

copyFile = (filePath.Contains(".CSS"))      ? true:
               (filePath.Contains(".JS"))   ? true:
               (filePath.Contains("_STR"))  ? true:
               (filePath.Contains(".VBS"))  ? true:
               (filePath.Contains(".HTM"))  ? true:
               (filePath.Contains(".BMP"))  ? true:
               (filePath.Contains("GIF"))   ? true:
                                              false;
return copyFile;

I like it, impressed with that one for a Friday.