Pomster
Pomster

Reputation: 15207

Buffer cannot be null error when opening excel file?

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

Upvotes: 1

Views: 3656

Answers (5)

pim
pim

Reputation: 12607

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

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

Upvotes: 3

Bruce
Bruce

Reputation: 1

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

Try

excelFileName = string.Format(@"{0}",excelFileName);

Upvotes: -1

Andre Mesquita
Andre Mesquita

Reputation: 91

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.

Upvotes: 1

Muhammad Zaighum
Muhammad Zaighum

Reputation: 560

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];

Upvotes: 0

Sinatr
Sinatr

Reputation: 22008

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).

Upvotes: 0

Related Questions