简体   繁体   中英

Proper way to detect #N/A in .XLSX with Spreadsheet Light

I have an Excel file (.xlsx), that I open in C# via Spreadsheet Light.

Some Cells can have the value #N/A (or whichever representation your language version of Excel uses) as a result of an SVERWEIS (in German, I think it is VLOOKUP in English).

What is the proper way to detect this cell error via SL? I currently check, if the string representation of the cell is "#N/A", which is most probably not the best way to do it. Is there any "correct" check, that I missed?

What I currently do:

using(SLDocument doc = new SLDocument(filename))
{
    if(doc.GetCellValueAsString("A11") != "#N/A")
        //Do error handling here
}

What I would like to do would be more like:

using(SLDocument doc = new SLDocument(filename))
{
    if(doc.HasCellError("A11")) //This function doesn't exist (yet?)
        //Do error handling here
}

This would eliminate the hackish looking solution with the magic string #N/A .

There is a function in excel called IFERROR which allows you to catch errors and return your own custom value when there is an error. If VLOOKUP returns a value normally, there is no error and the looked up value is returned. If VLOOKUP returns the #N/A error, IFERROR takes over and returns the value you supply.

=IFERROR(VLOOKUP(B10,table,2,FALSE),"Not found")

This will return the message "Not found" instead of #N/A, and then you can check for that specific value you set. I'm not sure if this is the best way to check, but i believe it improves your current solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM