Cannot read a simple xlsx file?

Topics: Developer Forum, User Forum
Oct 27, 2009 at 11:29 AM

Hi all - Just came accross this code and it looks as though it may be very useful, but I cannot seem to get started.

I have a simple file "book1.xlsx" created in excel 2007 with two sheets both with a text string in the first cell on each sheet.

I copied all the CS files into the app_code folder in a simple project modelled on the simple read example:

( I have checked that the filename is correct in the FileInfo object and it opens the file without a problem)

     String szFile = ""; szFile += szFilesFolder; szFile += "book2.xlsx;";
        FileInfo objFileInfo = new FileInfo(szFile);
        using (ExcelPackage xlPackage = new ExcelPackage(objFileInfo))
            // get the first worksheet in the workbook
            ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];
            string szText = worksheet.Cell(1, 1).Value;
            Label1.Text = szText;

        } // the using statement calls Dispose() which closes the package.

However I get an error when it cannot find the worksheets

Specifically the call: " foreach (XmlNode sheetNode in _worksheetsNode.ChildNodes)" in ExcelWorksheets.cs fails as the sheets node has no children

Upon closer inspection the innerxml that it is looking at is this, so I can see why it fails - So what am I doing wrong here?

The inner xml is : "<workbook xmlns:r=\"\" xmlns=\"\"><bookViews><workbookView /></bookViews><sheets /></workbook>"

This is the simplest xlsx file!