0

When using Poi, even it doesn't have the sheet, it automatically creates the sheet. How to know whether it contains the sheet?

public bool Check(string Filepath, string sheetname)
            { 
                HSSFWorkbook workbook;
                using(FileStream stream = new FileStream(Filepath, FileMode.Open, FileAccess.Read){
                      workbook = new HSSFWorkbook(stream);
                      }
                return workbook.contains(sheetname);
nooogii
  • 25
  • 4
  • I found the one solution. Which is just use try{} and in there write something like sheet.getrow... then if its not exist it will occurs nullexception – nooogii May 12 '17 at 12:54
  • to be crystal clear, you're using NPOI, not POI. These libraries are pretty different these days. In POI, you can do `Workbook.getSheet(sheetName)`, which will return `null` if the workbook doesn't contain a sheet with the specified `sheetName`. https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Workbook.html#getSheet(java.lang.String) – IceArdor May 16 '17 at 10:29

1 Answers1

0

Unfortunately, NPOI / POI does not really have that feature. There is a worksheet.GetSheetAt(index) method, but it throws an exception when you try to get an index that does not exist. So if you want to try looping through possible index numbers, make sure you put a try-catch inside your loop.

William
  • 491
  • 5
  • 9