I am trying to open an excel file through open file dialog but I am getting the following error
this the code i'v written, lemme know where it goes wrong
Excel.Application excelApp = new Excel.Application();
Excel.Workbook newWorkbook = excelApp.Workbooks.Add();
Excel.Workbook excelWorkbook = null;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel Files |*.xlsx";
ofd.InitialDirectory = @"C:\";
if (ofd.ShowDialog() == DialogResult.OK)
{
string path = System.IO.Path.GetFullPath(ofd.FileName);
try
{
excelWorkbook = excelApp.Workbooks.Open(path,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
MessageBox.Show(errorMessage, "Error");
}
}
I am doing this coz I need to get values from the excel sheet. Please let me know if you require more details.
Edit -: when I looked closely I understood that the message box is not prompted for the first trial of opening the file, but the file does not open and then the message box appears during every successive trial of opening it. As suggested by @Pankaj, I tried adding Finally at the bottom but I still get the message box after 2nd trial of opening the file.