为什么最后一行永远不会被读取?
Why the last row never gets read?
你好,我有一个问题,为什么最后一行永远不会被读取?它在 excel 文件中只有一行还是 100 行都没有关系。最后一行永远不会出现在列表中。我不知道为什么....
这是我的 Excel 文件:
这是我的方法:
public List<string> getListData(bool skipFirstRow, int numberOfColumns, string filepath)
{
int startpoint = 1;
int cell = 1;
int row = 1;
List<string> stringList = new List<string>();
//Open Excel (Application)
var excelApplication = openExcelApplication();
//Open Excel File
Excel.Workbook excelWorkbook = excelApplication.Workbooks.Open(filepath);
//Get the Worksheets from the file
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
//Select the first Worksheet
Excel.Worksheet worksheet = (Excel.Worksheet)excelSheets.get_Item(1);
if (skipFirstRow == true)
{
startpoint = 2;
}
Excel.Range range = worksheet.get_Range("A" + Convert.ToString(startpoint), Missing.Value);
while ((range.Cells[startpoint, cell] as Excel.Range).Value2 != null)
{
for (int i = 1; i <= numberOfColumns + 1; i++)
{
string sValue = (range.Cells[row, cell] as Excel.Range).Value2.ToString();
stringList.Add(sValue);
cell++;
}
startpoint++;
cell = 1;
row++;
}
closeExcelApplication(excelApplication);
var result =
stringList
.Select((item, index) => new { Item = item, Index = index })
.GroupBy(x => x.Index / numberOfColumns)
.Select(g => string.Join(";", g.Select(x => x.Item)))
.ToList();
return result;
}
我用调试器试过了,甚至 google。然后我用最后用过的行东西试了一下,但没有用。
Excel.Range last = worksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
Excel.Range range = worksheet.get_Range("A1", last);
int lastUsedRow = last.Row;
int lastUsedColumn = last.Column;
如有任何帮助或建议,我们将不胜感激,感谢您的宝贵时间和帮助。
你的算法有问题。
让我们看看当 skipFirstRow 为真并且您的 Excel sheet 有三行 1、2 和 3 时会发生什么。在 while 循环开始时,我们有以下情况:
startpoint = 2
row = 1
在第一次迭代中,你的while循环读取了第1行的内容。在迭代之后,我们有以下情况:
startpoint = 3
row = 2
在第二次迭代中,你的while循环读取了第2行的内容。在迭代之后,我们有以下情况:
startpoint = 4
row = 3
由于 range.Cells[startpoint, cell]
为空,您的代码到此为止。第 1 行和第 2 行已阅读,第 3 行已被忽略。
如您所见,问题的原因是您检查 startpoint
中的行并读取 row
中的行,当这两个不同时,你就有问题了。建议的解决方案:删除 startpoint
变量并改用 row
。
excelsheet显示第一行号为1
但在内部它从 0 开始,可能是它正在读取所有数据,将不同的测试数据放在最后一列
你好,我有一个问题,为什么最后一行永远不会被读取?它在 excel 文件中只有一行还是 100 行都没有关系。最后一行永远不会出现在列表中。我不知道为什么....
这是我的 Excel 文件:
这是我的方法:
public List<string> getListData(bool skipFirstRow, int numberOfColumns, string filepath)
{
int startpoint = 1;
int cell = 1;
int row = 1;
List<string> stringList = new List<string>();
//Open Excel (Application)
var excelApplication = openExcelApplication();
//Open Excel File
Excel.Workbook excelWorkbook = excelApplication.Workbooks.Open(filepath);
//Get the Worksheets from the file
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
//Select the first Worksheet
Excel.Worksheet worksheet = (Excel.Worksheet)excelSheets.get_Item(1);
if (skipFirstRow == true)
{
startpoint = 2;
}
Excel.Range range = worksheet.get_Range("A" + Convert.ToString(startpoint), Missing.Value);
while ((range.Cells[startpoint, cell] as Excel.Range).Value2 != null)
{
for (int i = 1; i <= numberOfColumns + 1; i++)
{
string sValue = (range.Cells[row, cell] as Excel.Range).Value2.ToString();
stringList.Add(sValue);
cell++;
}
startpoint++;
cell = 1;
row++;
}
closeExcelApplication(excelApplication);
var result =
stringList
.Select((item, index) => new { Item = item, Index = index })
.GroupBy(x => x.Index / numberOfColumns)
.Select(g => string.Join(";", g.Select(x => x.Item)))
.ToList();
return result;
}
我用调试器试过了,甚至 google。然后我用最后用过的行东西试了一下,但没有用。
Excel.Range last = worksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
Excel.Range range = worksheet.get_Range("A1", last);
int lastUsedRow = last.Row;
int lastUsedColumn = last.Column;
如有任何帮助或建议,我们将不胜感激,感谢您的宝贵时间和帮助。
你的算法有问题。
让我们看看当 skipFirstRow 为真并且您的 Excel sheet 有三行 1、2 和 3 时会发生什么。在 while 循环开始时,我们有以下情况:
startpoint = 2
row = 1
在第一次迭代中,你的while循环读取了第1行的内容。在迭代之后,我们有以下情况:
startpoint = 3
row = 2
在第二次迭代中,你的while循环读取了第2行的内容。在迭代之后,我们有以下情况:
startpoint = 4
row = 3
由于 range.Cells[startpoint, cell]
为空,您的代码到此为止。第 1 行和第 2 行已阅读,第 3 行已被忽略。
如您所见,问题的原因是您检查 startpoint
中的行并读取 row
中的行,当这两个不同时,你就有问题了。建议的解决方案:删除 startpoint
变量并改用 row
。
excelsheet显示第一行号为1 但在内部它从 0 开始,可能是它正在读取所有数据,将不同的测试数据放在最后一列