Articles in this section

How to avoid Index Out of Range Exception while accessing WinForms XlsIO?

Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also, converts Excel documents to PDF files.

Worksheet Indices in an Excel Workbook

The worksheet index in an Excel workbook starts with 0 and must always be non-negative and less than the size of the collection.

Why does the Index Out of Range Exception is thrown?

An Index Out of Range exception occurs when an incorrect index value is specified. For example, if an Excel workbook has 3 worksheets, their respective indices will be 0, 1, and 2. Trying to access a worksheet with index 3 or higher results in an Index Out of Range exception.

C#

ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
 
// Create workbook with 3 worksheets
IWorkbook workbook = application.Workbooks.Create(3);
 
// Accessing first worksheet from the worksheet collection
IWorksheet sheet1 = workbook.Worksheets[0];
 
// Accessing second worksheet from the worksheet collection
IWorksheet sheet2 = workbook.Worksheets[1];
 
// Accessing third worksheet from the worksheet collection
IWorksheet sheet3 = workbook.Worksheets[2];
 
// Accessing fourth worksheet which is not in the worksheet collection. 
IWorksheet sheet4 = workbook.Worksheets[3];

 

The above code snippet results in Index Out Of Range exception while accessing the fourth worksheet, because it is not in the worksheet collection.

Index Out Of Range Exception in WinForms XIsIO

Index Out Of Range Exception

How to overcome this Index Out of Range Exception?

As discussed above, the only way to overcome or avoid this exception is to access only the worksheets that are exist in the worksheet collection.

Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheet or workbook, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheet using Template Markers, and most importantly PDF and Image conversions etc., with code examples.

Click here to explore the rich set of Syncfusion® Excel (XlsIO) library features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.

 

Conclusion

I hope you enjoyed learning about how to avoid Index Out of Range Exception while accessing Excel worksheet.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied