如何在Excel工作表中读取多行和列数据。 [英] How to read multiple rows and column data in excel sheet.
问题描述
我想阅读多个Excel行和列数据。目前我只能读取没有任何循环的第一行和第二列数据(A,12)。迭代时,我无法同时获取多行和多列的数据。下面我提到了excel之类的观点。我需要获取数据(a,12)并将它们存储到两个单独的变量中。所以,我可以进一步使用它们来加载。
Col A Col B
用户名密码
第1行。一个12
Row2。 b 34
我的尝试:
I want to read the multiple Excel Rows and columns data. Currently i am only able to read first row and column data(A, 12) without any loop. While iterating i am not able to get the data of multiple rows and column at the same time. Below i have mentioned the excel like view. I need to get the data (a,12) and store them into two separate variables. So, that i can use them further for loading.
Col A Col B
Username Password
Row1. a 12
Row2. b 34
What I have tried:
<pre> int rCnt;
int cCnt;
string workbookPath = "f:\\Test\\TestData.xlsx";
Excel.Application excelApp;
Excel.Workbook excelWorkbook;
Excel.Sheets excelSheets;
Excel.Worksheet excelWorksheet;
Excel.Range range;
string currentSheet = "DataSet";
excelApp = new Excel.Application();
excelWorkbook = excelApp.Workbooks.Add(workbookPath);
excelSheets = excelWorkbook.Sheets;
excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
range = excelWorksheet.UsedRange;
for (rCnt = 2; rCnt <= range.Rows.Count; rCnt++)
{
for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
{
Loginname = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value;
Password = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value;
}
推荐答案
请参阅我对这个问题的解决方案:如何通过C#在.xlsx文件中逐个读取列[ ^ ] - 它可能是你的同伴......
See my solution to this question: How to read columns one by one in .xlsx file through C#[^] - it's probably a class mate of yours...
这篇关于如何在Excel工作表中读取多行和列数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!