【发布时间】:2015-02-10 18:46:44
【问题描述】:
我正在构建一个应用程序,它从 excel 文件中获取一些数据并使用它们来做其他事情。
好吧,我让用户浏览一个 excel 文件,然后我问他我应该在哪个表中查找数据。
这是我的代码:
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());
}
然后我有这个事件:
private void cmbSheetName_SelectedIndexChanged(object sender, EventArgs e)
{
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[cmbSheetName.SelectedIndex];
// ...
}
最后一行代码给我一个错误(“无效索引...”)。 而且我不知道如何从该 excel 文件中获取所有行/列。
有人可以帮帮我吗?
请原谅我的英语!
【问题讨论】:
标签: c#