Search code examples
c#npoi

How would I go about converting NPOI.SS to NPOI.XSSF?


I'm currently attempting to replace the need to use the Excel Interop libraries but moving to NPOI. I am hitting a bit of a snag at the moment with the following code:

XSSFSheet SheetName = excelWorkbook.GetSheet(sheet);
System.Collections.IEnumerator rows = SheetName.GetRowEnumerator();
while (rows.MoveNext())
{
    XSSFRow row = (XSSFRow)rows.Current;
    foreach (XSSFCell cell in row.Cells)
    {
        cellContent = cell.StringCellValue;
    } 
}

The error I am receiving is:

Cannot implicitly convert type 'NPOI.SS.UserModel.ISheet' to 'NPOI.XSSF.UserModel.XSSFSheet'. An explicit conversion exists (are you missing a cast?)

Where am I going wrong? I am certain I haven't any references to HSSF.

Note: I am certain I've made more mistakes than just this, so do point them out to me if you notice anything.


Solution

  • The GetSheet method returns a type ISheet, not XSSFSheet.

    So, you need to change

    XSSFSheet SheetName = excelWorkbook.GetSheet(sheet);
    

    to

    XSSFSheet SheetName = (XSSFSheet)excelWorkbook.GetSheet(sheet);
    

    explicitly casting the ISheet to XSSFSheet