1

I am using excel Library to open a spread Sheet and read its contents, I am using this library over the interop library as the interop library causes issues on the server machine i hope to host on.

https://code.google.com/p/excellibrary/

using ExcelLibrary.SpreadSheet;

Stream fileStream = System.IO.File.OpenRead(excelFileName); //FilePath
Workbook book = Workbook.Load(fileStream); //Exception thrown
Worksheet sheet = book.Worksheets[0];

When running the code, you can see the file stream load:

enter image description here

When i step to next line i get the exception. Buffer cannot be null.

enter image description here

After the exception the stream reader becomes canRead false:

enter image description here

Pomster
  • 14,567
  • 55
  • 128
  • 204

5 Answers5

3

As @pomster pointed out, the problem most times here is:

The XLS file being of type "5.0/95" instead of "97-2003"

pim
  • 12,019
  • 6
  • 66
  • 69
1

VB.NET Code:

Public Function importSheet(fileName As String) As Boolean
  Dim fileStream = System.IO.File.OpenRead(fileName)        
  Dim book = ExcelLibrary.SpreadSheet.Workbook.Load(fileStream)
  Dim sheet = book.Worksheets(0)

  'TO DO
  Return True
End Function

Look for folder's permission.

When System.IO library has many Exceptions, look read/write folder's and files permission.

0

You have to specify file name, not stream to method Workbook.Load, see example in own link:

string file = "C:\\newdoc.xls";
// open xls file
Workbook book = Workbook.Load(file);
Worksheet sheet = book.Worksheets[0];

If there is an overload for it, then you have to ask library author for assistance (it looks like a bug).

Sinatr
  • 20,892
  • 15
  • 90
  • 319
0

I just created a simple program and I dont get this Exception,

Can you check you Excel file ? may be it is corrupt.

you can make a simple program that is just opening this application ?

string excelFileName = "";
        excelFileName = @"E:\Innovation\PAKISTAN.xls";
        Stream fileStream = System.IO.File.OpenRead(excelFileName); //FilePath
        Workbook book = Workbook.Load(fileStream); //Exception thrown
        Worksheet sheet = book.Worksheets[0];
Muhammad Zaighum
  • 560
  • 4
  • 21
  • 1
    My Excels are Microsoft Excel 5.0/95 Workbook, .xls if i save them as Microsoft Excel 97-2003, .xls it looks to work – Pomster May 26 '14 at 11:59
  • the issue is already [reported](https://code.google.com/p/excellibrary/issues/detail?id=94&q=buffer&colspec=ID%20Type%20Status%20Priority%20ReportedBy%20Owner%20Summary%20Opened) – Muhammad Zaighum May 26 '14 at 12:34
-1

You need the @ symbol at the front of your path filename.

Try

excelFileName = string.Format(@"{0}",excelFileName);
sheilak
  • 5,833
  • 7
  • 34
  • 43
Bruce
  • 1