1

I'm trying to read an xlsx file, and I got a basic overview from This Codeproject Link
Now, I'm getting the following exception message:
Exception Message
The code segment related to this exception is given below:

public static sst SharedStrings;

    /// <summary>
    /// All worksheets in the Excel workbook deserialized
    /// </summary>
    /// <param name="ExcelFileName">Full path and filename of the Excel xlsx-file</param>
    /// <returns></returns>
    public static IEnumerable<worksheet> Worksheets(string ExcelFileName)
    {
        worksheet ws;
        using (ZipArchive zipArchive = ZipFile.Open(ExcelFileName, ZipArchiveMode.Read))
        {
            SharedStrings = DeserializedZipEntry<sst>(GetZipArchiveEntry(zipArchive, @"xl/sharedStrings.xml"));
            foreach (var worksheetEntry in (WorkSheetFileNames(zipArchive)).OrderBy(x => x.FullName))
            {
                ws = DeserializedZipEntry<worksheet>(worksheetEntry);
                ws.NumberOfColumns = worksheet.MaxColumnIndex + 1;
                ws.ExpandRows();
                yield return ws;
            }
        }
    }


After some searching, I figured out that I needed to target .NET 4.5 or above version, (I'm targetting 4.6.1, but I also have tried 4.5, still same results). Also, as far as the dependencies are concerned, my references look like this:
References Image
After doing all this, I'm still getting the exception mentioned above, and have no idea why this is happening.
EDIT 1: I've been through This StackOverflow Link, where I figured that I need the latest DLLs. I went to the Nuget Package Manager, and there were "No Packages Found" which were available for the updates.

Syed Ali Hamza
  • 193
  • 1
  • 10

1 Answers1

1

I have tried .NET 4.6.1 and 4.6.2. Both work OK for me.

My Visual Studio is Community Edition 2017

The project references:

enter image description here

Note that System.IO.Compression.ZipFile is not referenced. It was not possible to reference it in my environment.

Use Workbook.Worksheets() or declare the calling class as sub-class of Excel.Workbook.

Excel is the namespace of the CodeProject article source which provides the necessary classes.

using Excel;
using System;

namespace akExcelAsZipDemo
{
    class Program : Workbook
    {
        //  from
        //  https://www.codeproject.com/tips/801032/csharp-how-to-read-xlsx-excel-file-with-lines-ofthe
        //
        //  inspired:
        //  https://github.com/ahmadalli/ExcelReader

        static void Main(string[] args)
        {
            const string fileName = @"E:\AK\export.xlsx";

            var worksheets = Worksheets(fileName);

            foreach (worksheet ws in worksheets)
            {
                Console.WriteLine($"cols={ws.NumberOfColumns} rows={ws.Rows.Length}");
            }

            Console.WriteLine();
        }
    }
}
Axel Kemper
  • 10,544
  • 2
  • 31
  • 54
  • I don't know, but it still isn't working for me. Same exception. The problem arises inside the `var worksheets = Worksheets(fileName);` part. – Syed Ali Hamza Jun 25 '17 at 05:01
  • Check the `Github` project [ExcelReader](https://github.com/ahmadalli/ExcelReader) and compare, if you copied the source 1:1. Can you debug to pinpoint the exact source code line where the exception gets thrown? – Axel Kemper Jun 25 '17 at 08:26
  • I have no idea how it did work out, but I definitely copy/pasted the Workbook.cs code again from the github source, cleaned and rebuilt, and it somehow worked out. – Syed Ali Hamza Jun 25 '17 at 10:31