Search code examples
c#excel.net-corenpoi

NPOI - Loading an Excel file causes Wrong Local header signature: 0xE011CFD0


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


Solution

  • 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 implement the IWorkbook interface so you can build your code around this interface and determine the workbook instance's type at runtime.

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

    private readonly bool _useOldExcelFormat;
    private readonly IWorkbook _workbook;
    
    public NpoiExcelManager(bool useOldExcelFormat = false)
    {
        _useOldExcelFormat= useOldExcelFormat;
        if (_useOldExcelFormat)
        {
            _workbook = new HSSFWorkbook();
        }
        else
        {
            _workbook = new XSSFWorkbook();
        }
    }
    

    You'll discover a few differences between HSSFWorkbook and XSSFWorkbook so sometimes you'd have to write specific code for each implementation but those cases are very rare.

    I'd say around 95% of the IWorkbook interface is working for both implementations.