NPOI with "Template" gets ReadingNotSupportedException

392 Views Asked by At

I made a small wrapper class that holds NPOI Excel old format (XLS, 1997-2003). The code is something like:

public class NpoiExcelDriver {
    /// <summary>
    /// Underlying workbook to work with.
    /// </summary>
    private HSSFWorkbook workbook;

    // Open an XLS file specified by file name.
    public void Open(string fileName)
    {
        using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
        {
            this.workbook = new HSSFWorkbook(file, true);
        }
    }

    // put data etc
    public void PutData() {...}

    // Save the workbook under specified file name.
    public void SaveAs(string fileName)
    {           
        using (FileStream fff = new FileStream(fileName, FileMode.Create, FileAccess.Write))
        {
            this.workbook.Write(fff);
        }
    }
}

When using it in a client, I am doing as follows:

NpoiExcelDriver output = new NpoiExcelDriver();

output.Open(@"C:\Temp\Test.xls");

output.PutData(1, 2, 1, "Someone");
output.PutData(1, 2, 2, "Was");
output.PutData(1, 2, 3, "Here");

output.SaveAs(@"C:\Temp\Test2.xls");

When I am running this code I get an exception:

A first chance exception of type 'NPOI.HPSF.ReadingNotSupportedException' occurred in NPOI.dll

I have no idea what I am doing wrong - I've checked other examples which seems to do the same but to no avail. I even tried to write to MemoryStream as I've seen in ASP.NET examples but I am getting the same issue.

If I create the workbook by itself and then try to save it it is working fine.

Any suggestions?

0

There are 0 best solutions below