ArgumentNullException when using CreateOpenXmlReader

May 30, 2013 at 3:59 PM
Hi,

I have an Excel file (xlsx), generated by an external software that gives me the following error when I try to open it using createxmlreader.

The file opens perfectly in Excel. When I open it in Excel and resave it, the file works ok, any hint?

System.ArgumentNullException: Value cannot be null.
Parameter name: String
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at Excel.Core.OpenXmlFormat.XlsxWorkbook.ReadStyles(Stream xmlFileStream) in
c:\development\eapidemo\third-party\ExcelDataReader\ExcelDataReader\Excel\Core\OpenXmlFormat\XlsxWorkbook.cs:line 103
at Excel.ExcelOpenXmlReader.ReadGlobals() in c:\development\eapidemo\third-party\ExcelDataReader\ExcelDataReader\Excel\ExcelOpenXmlReader.cs:line 56
at Excel.ExcelReaderFactory.CreateOpenXmlReader(Stream fileStream) in c:\development\eapidemo\third-party\ExcelDataReader\ExcelDataReader\Excel\ExcelReaderFa
ctory.cs:line 37

at ConsoleApplication2.Program.Main(String[] args) in C:\Users\jcarneiro\Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 22

I'm trying to open it in a small console application that just reads and shows the contents of the file.

using System;
using System.Data;
using System.IO;
using Excel;

namespace ConsoleApplication2
{
class Program
{
    static void Main(string[] args)
    {
        //string filePath = args[0];
        string filePath = "c://Excel Tester//Facturas_20130527.xlsx";
        FileStream fileStream = new FileStream(
            filePath,
            FileMode.Open,
            FileAccess.Read,
            FileShare.ReadWrite);
        try
        {
            IExcelDataReader excelReader = filePath.ToLower().EndsWith("xls")
                                               ? ExcelReaderFactory.CreateBinaryReader(fileStream)
                                               : ExcelReaderFactory.CreateOpenXmlReader(fileStream);
            DataSet result = excelReader.AsDataSet();
            int lineNumber = 1;
            while (++lineNumber < result.Tables[0].Rows.Count)
            {
                foreach (
                    string s in
                        Array.ConvertAll<object, string>(result.Tables[0].Rows[lineNumber].ItemArray,
                                                         Convert.ToString))
                {
                    Console.Write(s + "\t");
                }
                Console.Write("\n");
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Error - "+e);
        }
        Console.WriteLine("Press any key...");
        Console.Read();
    }
}
}