Hi,
My code looks like this
object misValue = System.Reflection.Missing.Value;
Excel.Application xlApp = new Excel.Application();
xlApp.SheetsInNewWorkbook = SCANLA.Run.BusDetails.Count() ;
Excel.Workbook xlWorkBook = xlApp.Workbooks.Add();
xlApp.StandardFont = Constants.FONT_NAME;
xlApp.StandardFontSize = Constants.FONT_SIZE;
int SheetNumber = 0;
string SheetName = null;
try
{
string path = Properties.Settings.Default.OutputDirPath + "\\" + Constants.GT_EXCEL_NAME;
if ((string)cmbNoBuses.SelectedItem == "2")
SheetName = "Bus 1,Bus 2";
if ((string)cmbNoBuses.SelectedItem == "3")
SheetName = "Bus 1,Bus 2,Bus 3";
if ((string)cmbNoBuses.SelectedItem == "4")
SheetName = "Bus 1,Bus 2,Bus 3,Bus 4";
if (File.Exists(path))
File.Delete(path);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(SheetNumber);
GenerateExcelSheets( xlWorkSheet, SheetName);
MessageBox.Show(Constants.GT_STATUS, Constants.GT_HEADING, MessageBoxButtons.OK, MessageBoxIcon.Information);
xlWorkBook.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
ReleaseObject(xlWorkBook);
ReleaseObject(xlApp);
}
Exception:
An unhandled exception of type 'System.Exception' occurred in SCANLA.exe
Additional information: Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
I am getting exception while executing this code.
Can anyone helpme in solving this
Thanks
John