I'm building an app which take some data from a excel file and using them to do something else.
Well, I ask user to browse for a excel file and then I ask him in which sheet i should look for data.
Here is my code:
using Excel = Microsoft.Office.Interop.Excel;
string strFilePath;
Excel.Application xlExcelApp = new Excel.Application() { };
Excel.Workbook xlWorkBook = null;
Excel.Worksheet xlWorkSheet = null;
OpenFileDialog myOpenFileDialog = new OpenFileDialog();
if (myOpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
strFilePath = myOpenFileDialog.FileName;
txtPath.Text = strFilePath;
}
xlWorkBook = xlExcelApp.Workbooks.Open(strFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
foreach (Microsoft.Office.Interop.Excel.Worksheet wSheet in xlWorkBook.Worksheets)
{
cmbSheetName.Items.Add(wSheet.Name.ToString());
}
Then I have this event:
private void cmbSheetName_SelectedIndexChanged(object sender, EventArgs e)
{
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[cmbSheetName.SelectedIndex];
// ...
}
Last line of code give me an error ( "Invalid index ..." ). And I don't know how to get all rows/columns from that excel file.
Can someone help me please?
Please forgive my English!