0
我正在構建一個應用程序,它從Excel文件中獲取一些數據並使用它們來執行其他操作。我不知道如何從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文件中的所有行/列。
有人可以幫我嗎?
請原諒我的英文!基於
可能重複[?在VSTO/C#中使用的行數和列數如何得到一個Excel範圍(http://stackoverflow.com/問題/ 2333202/how-do-i-get-an-excel-range-using-row-and-column-numbers-in-vsto-c) – igorushi 2015-02-10 10:50:58