我正在通過一些excel單元循環來對單元格的值進行求和,其中一些單元格爲空,並且在傳遞它們時出現Input string was not in the correct format
錯誤。這裏是我的代碼:使用int.parse添加值
int total = 0;
int rowstart = 4;
while (ws.Cells[rowstart, 1].Value.ToString() != "")
{
if (ws.Cells[rowstart, 1].Value.ToString() !=
ws.Cells[rowstart + 1, 1].Value.ToString())
{
ws.InsertRow(rowstart + 1, 1);
ws.Cells[rowstart + 1, 3].Value = total;
}
else
{
total = total + int.Parse(ws.Cells[rowstart, 3].Value.ToString());
// I'm adding the value of Column 3 to the variable total,
I get the error if the cell is empty
rowstart = rowstart + 1;
}
}
我想那是因爲你不能解析一個空字符串,讓我怎麼只加0,如果細胞是空的?
的代碼,你可以提供什麼類型的'ws.Cells [rowstart,3] .Value'是什麼? – Kamo
@Kamo你是什麼意思?單元格中的值是整數或空單元格。如果循環經過空單元格,我遇到了添加問題。 – crimson
如果您的單元格爲空或者此單元格不包含有效整數,您是否只想添加「0」**? –