IWorkbook workbook; string fileExt = Path.GetExtension(filePath); try { using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { if (fileExt == ".xls") { workbook = new HSSFWorkbook(file); } else if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(file); } else { return null; } } }