如何从MS Excel工作表中检索数据?

我正在尝试使用下面的C#代码从Excel中获取数据。 我从两列获取单个variables(str)的值。

我想这个值到不同的variables。因此,我可以在运行时发送该值为两个不同的语句。

如何把他们变成两个不同的variables?

string currentSheet = "Sheet1"; excelApp = new Excel.Application(); //Opening/adding Excel file excelWorkbook = excelApp.Workbooks.Add(workbookPath); excelSheets = excelWorkbook.Sheets; excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet); //Gives the used cells in the sheet range = excelWorksheet.UsedRange; for (rowCnt = 1; rowCnt <= range.Rows.Count; rowCnt++) { for (colCnt = 1; colCnt <= range.Rows.Count;colCnt++) { str = (string)(range.Cells[rowCnt,colCnt] as Excel.Range).Value2; System.Console.WriteLine(str); } } 

  string Connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";"; OleDbConnection con = new OleDbConnection(Connection); OleDbCommand command = new OleDbCommand(); System.Data.DataTable dt = new System.Data.DataTable(); OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from [Sheet1$]", con); myCommand.Fill(dt); con.Close(); 
 for (rowCnt = 1; rowCnt <= range.Rows.Count; rowCnt++) { string Charity = (string)(range.Cells[rowCnt, 1] as Excel.Range).Value; string Country = (string)(range.Cells[rowCnt, 2] as Excel.Range).Value; System.Console.WriteLine(Charity + " --- " + Country); }