Home » excel » c# – NPOI – Loading an Excel file causes Wrong Local header signature: 0xE011CFD0

c# – NPOI – Loading an Excel file causes Wrong Local header signature: 0xE011CFD0

Posted by: admin May 14, 2020 Leave a comment

Questions:

Has anybody an idea what may cause this error

‘Wrong Local header signature: 0xE011CFD0’

var path = @"C:\Excel.xls";

using (var fs = File.OpenRead(path))
{
    var wb = new XSSFWorkbook(fs);
}

Im using: https://github.com/dotnetcore/NPOI

How to&Answers:

Leaving this in case other people pass by here.

.xls is the old Excel format. With that format you should create a new HSSFWorkbook instance. The XSSFWorkbook is used with the new .xlsx format.

Both types inherit from the IWorkbook interface so you can build you code around this interface and determine the workbook instance type at runtime.

I created a simple Excel component and here is the constructor:

private IWorkbook _workbook;

public NpoiExcelManager(bool useOldVersionOfExcel = false)
{
    try
    {
        UseOldVersionOfExcel = useOldVersionOfExcel;
        if (UseOldVersionOfExcel)
        {
            _workbook = new HSSFWorkbook();
        }
        else
        {
            _workbook = new XSSFWorkbook();
        }
    }
    catch (Exception)
    {
        throw;
    }
}

public bool UseOldVersionOfExcel { get; private set; }

Answer:

Ok, I found solution:

Saved that Excel file as an .xlsx but without macros or .xlsm and it worked