I am facing issue with excel reports during creation of two excel sheets ine one workbook using vb.net, I was using Microsoft Excel 2007 for report generation it was good but unfortunately I need to uninstall all Microsoft Office Components and then I installed WPS office.Then this problem arise during generation of excel report. here is the source code.
Dim wb As Excel.Workbook
Dim range, range1 As Excel.Range
wb = a.Workbooks.Add
Dim ws As Excel.Worksheet = wb.Worksheets(1)
**Dim ws1 As Excel.Worksheet = wb.Worksheets(2)** -> On This Line
ws1.Name = "MySheet2"
All was going good with Microsoft Office 2007 but its happening with WPS excel, In source code the section i marked as bold, when .net debug r goes there it directly goes to catch block and its gives this error Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX)) Please Help me out Thanks in Advance
When you create a new workbook like you have then you only get a Sheet1 so when you try to access a sheet with the index of 2 you get an invalid index because it does not exist.
Instead, you need to call the Worksheets.Add
method.
Try this:
Dim ws1 As Excel.Worksheet = wb.Worksheets.Add()
ws1.Name = "MySheet2"
User contributions licensed under CC BY-SA 3.0