Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

When am trying to display my 4th table date in 4th excel sheet. it's throwing invalid index exception
C#
xlWorkSheet3 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(4);

C#




any help in solving this query.

Thanks & Regards
sam.198979
Posted
Updated 26-Jun-13 0:14am
v3
Comments
walterhevedeich 26-Jun-13 6:16am    
If it's the 4th, shouldn't the index be 3 since you are using C#? C# indeces are zero-based.
sam.198979 26-Jun-13 6:27am    
xlWorkSheet= (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(0);
xlWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet2 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
xlWorkSheet3 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);

it's giving same Exception

1 solution

By default MS Excel stores 3 worksheets. If you want to save data into 4. worksheet, you need to add it.

How to: Add New Worksheets to Workbooks[^]
How to: Programmatically Add New Worksheets to Workbooks[^]
 
Share this answer
 
Comments
sam.198979 26-Jun-13 8:00am    
thanks
Maciej Los 26-Jun-13 9:34am    
You're welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900