Search code examples
c#.netnpoi

InvalidOperationException while reading Excel file with NPOI


I have simple piece of code

public JsonResult ParseExcel(HttpPostedFileBase file)
{
    var uploadedFileContent = file.InputStream;
    if (POIXMLDocument.HasOOXMLHeader(uploadedFileContent))
    {
        //xlsx
        var workbook = new XSSFWorkbook(uploadedFileContent);
        var sheet = workbook.GetSheetAt(0);
    }
return new JsonResult{};
}

So then I try to create a workbook using NPOI with uploaded Excel file, I get error: Exception details: Message: The hyperlink for cell G33 references relation rId5, but that didn't exist!

I found out that this happens because G33 cell has a hyperlink, which is like this: [email protected]'. It has a single quote in the end. If I remove single quote, everything works as expected. Is there any other way to solve this problem rather that editing Excel file and removing single quote?

I tried using WorkbookFactory.Create and specifying second param as ImportOption.TextOnly. I tried creating OPCPackage object first and then passing it as a param. However, I get the same message.


Solution

  • As passed file to XSSFWorkbook constructor, corrupts the whole thing, I did not find any other solution then to switch to a different library.