我下面有一段代码 - 循环通过Excel工作簿将数据添加到SQL数据库 - 但是当数据用完时,forloop不会停止。
int rowCount = worksheet.Rows.Count -1;
for (int i = 3; i < rowCount; i++ )
{
Spreadsheet spreadsheetToSave = new Spreadsheet();
Estimate estimateToSave = new Estimate();
RAA raaToSave = new RAA();
spreadsheetToSave.Phases = worksheet.Cells[4, i].Value;
spreadsheetToSave.Deliverables = worksheet.Cells[5, i].Value;
if (worksheet.Cells[6,i].Value == "Y")
{
spreadsheetToSave.Scope = true;
}
else
{
spreadsheetToSave.Scope = false;
}
spreadsheetToSave.Description = worksheet.Cells[7, i].Value;
//estimateToSave.Estimate1 = Convert.ToDecimal(worksheet.Cells[8, i].Value);
//estimateToSave.Estimate2 = Convert.ToDecimal(worksheet.Cells[9, i].Value);
//estimateToSave.Estimate3 = Convert.ToDecimal(worksheet.Cells[10, i].Value);
estimateToSave.Estimate1 = 1;
estimateToSave.Estimate2 = 1;
estimateToSave.Estimate3 = 1;
spreadsheetToSave.Estimate = estimateToSave;
db.Spreadsheet.Add(spreadsheetToSave);
有人可以帮忙吗?
谢谢:)
答案 0 :(得分:0)
尝试用以下代码替换前两行代码:
int rowCount = worksheet.UsedRange.Rows.Count;
for (int i = 3; i <= rowCount; i++)
这从第3行循环到最后一行。
另外,您应该检查其余代码:据
所知worksheet.Cells[rowIndex, columnIndex]
rowIndex首先出现:)