Click here to Skip to main content
11,637,176 members (59,740 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hi,

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



any help in solving this query.

Thanks & Regards
sam.198979
Posted 26-Jun-13 0:11am
Edited 26-Jun-13 0:14am
v3
Comments
walterhevedeich at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
sam.198979 at 26-Jun-13 8:00am
   
thanks
Maciej Los at 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)

  Print Answers RSS
0 OriginalGriff 295
1 stibee 288
2 jyo.net 270
3 Sergey Alexandrovich Kryukov 214
4 himanshu agarwal 184
0 OriginalGriff 9,031
1 Sergey Alexandrovich Kryukov 8,513
2 Mika Wendelius 6,922
3 F-ES Sitecore 2,388
4 Suvendu Shekhar Giri 2,320


Advertise | Privacy | Mobile
Web04 | 2.8.150728.1 | Last Updated 26 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100