Using NExcel To Avoid MS-Office Dependencies

I recently was writing a utility application to was required to read data from an Excel spreadsheet and write it to both XML documents and plain text files.  The catch was that this had to be done on machines on which it was not guaranteed that Microsoft Office would be installed.

I was creating the utility using C# and the .NET Framework.  The writing to XML and text files was easy enough; the .NET Framework provides several ways to accomplish that.  But what about reading from Excel?  I went looking, and found NExcel.

NExcel (http://nexcel.sourceforge.net/) is an open-source port of JExcelAPI (an open source Excel API for Java) written entirely in C# that does not require the presence of MS Office Excel or any other 3rd party libraries.

NExcel does have its limitations.  For one, it reads Excel 97, 2000, XP, and 2003 documents, but not Excel 2007 (or presumably 2010) documents.  Also, it only supports reading of spreadsheets, not writing.  A lesser limitation is that it cannot read spreadsheets with images.

Unfortunately, it appears that further development of the tool has died, as the latest released version is more than five years old.  So I expect that the limitations of the released API will not be removed.  However, the source code for the API is available, so anyone with the need/interest/ability can modify the API to suit their needs.

Despite the limitations, NExcel is good at what it does.  And, the fact that it eliminates dependencies on Office can be a tremendous benefit, particularly for server-based applications or situations in which the presence of Office cannot be assured.

Following is a short example of using NExcel to read data from a spreadsheet.  In this example, the location of data within the spreadsheet is known in advance, so it is possible to simply read from specific cells.

// Include a reference to the NExcel namespace
using NExcel;

public bool ReadExcelData(string excelFilePath)
{
   Workbook wb = null;

   try
   {
       // Open the Excel workbook
       wb = Workbook.getWorkbook(excelFilePath);

       // Read the data from the workbook
       string title = string.Empty;
       string publisher = string.Empty;
       string publisherPlace = string.Empty;
       List<string> creators = new List<string>();

       // The workbook has two sheets from which to read data
       foreach (Sheet sheet in wb.Sheets)
       {
           switch (sheet.Name)
           {
               case "Title":
                   // Read data from the 4th column of the 3rd, 7th, and 8th rows
                   title = sheet.getCell(4, 3).Contents;
                   publisherPlace = sheet.getCell(4, 7).Contents;
                   publisher = sheet.getCell(4, 8).Contents;
                   break;
               case "Creator":
                   // Starting with the 4th row, and continuing until an empty cell is found,
                   // read the first column from each row
                   for (int irow = 4; irow < sheet.Rows; irow++)
                   {
                       if (string.IsNullOrEmpty(sheet.getCell(1, irow).Contents)) break;
                       creators.Add(sheet.getCell(1, irow).Contents);
                   }
                   break;
           }
       }

       // Do something with the data

       return true;
   }
   catch (Exception ex)
   {
       // Log the error here
       return false;
   }
   finally
   {
       // Close the Excel workbook
       if (wb != null) wb.close();
   }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: