我的代码用来读取excel文件中的值有点问题。我使用循环读取每行的行数和每个单元格的单元格。
excel文件中的第152行正确读取但在第153行之后,我只获得每个单元格的空值。
这是我的代码:
public DataTable parseExcelFile(string path,bool firstLineHeader)
{
DataTable values = new DataTable("Impayés");
Excel.Application appExcel = new Excel.Application();
Excel.Workbook wb = appExcel.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Worksheet sheet = (Excel.Worksheet)wb.Sheets["Feuil1"];
Excel.Range excelRange = sheet.UsedRange;
bool premiere = firstLineHeader;
foreach (Excel.Range row in excelRange.Rows)
{
if (!premiere)
{
int rowNumber = row.Row;//Récupération du numéro de ligne
//Récupération des valeurs des cellules de la ligne en cours
string groupe = Convert.ToString(row.Range["A" + rowNumber.ToString()].Value);
if (groupe == null)
MessageBox.Show("Eh c'est vide ! (Ligne "+ rowNumber.ToString() + ")");
string sousgroupe = Convert.ToString(row.Range["B" + rowNumber.ToString()].Value);
string codeapporteur = Convert.ToString(row.Range["C" + rowNumber.ToString()].Value);
string apporteur = Convert.ToString(row.Range["D" + rowNumber.ToString()].Value);
string dossier = Convert.ToString(row.Range["E" + rowNumber.ToString()].Value);
string siren = Convert.ToString(row.Range["F" + rowNumber.ToString()].Value);
string client = Convert.ToString(row.Range["G" + rowNumber.ToString()].Value);
string adresse = Convert.ToString(row.Range["H" + rowNumber.ToString()].Value);
string localite = Convert.ToString(row.Range["I" + rowNumber.ToString()].Value);
string cp = Convert.ToString(row.Range["J" + rowNumber.ToString()].Value);
string ville = Convert.ToString(row.Range["K" + rowNumber.ToString()].Value);
string telephone = string.Format("{0:00}{1:00}{2:00}{3:00}{4:00}", row.Range["L" + rowNumber.ToString()].Value, row.Range["M" + rowNumber.ToString()].Value,row.Range["N" + rowNumber.ToString()].Value,row.Range["O" + rowNumber.ToString()].Value,row.Range["P" + rowNumber.ToString()].Value);
string etat = Convert.ToString(row.Range["Q" + rowNumber.ToString()].Value);
string bic = Convert.ToString(row.Range["R" + rowNumber.ToString()].Value);
string iban = Convert.ToString(row.Range["S" + rowNumber.ToString()].Value);
RIB rib = new RIB(bic, iban);
string domiciliationrib = Convert.ToString(row.Range["T" + rowNumber.ToString()].Value);
string bienfinance = Convert.ToString(row.Range["U" + rowNumber.ToString()].Value);
decimal mt_ht_fi = Convert.ToDecimal(row.Range["V" + rowNumber.ToString()].Value);
decimal mt_ech = Convert.ToDecimal(row.Range["W" + rowNumber.ToString()].Value);
string dateapp = Convert.ToString(row.Range["X" + rowNumber.ToString()].Value) + "/" + Convert.ToString(row.Range["Y" + rowNumber.ToString()].Value) + "/" + Convert.ToString(row.Range["Z" + rowNumber.ToString()].Value);
string terme = Convert.ToString(row.Range["AA" + rowNumber.ToString()].Value);
string premiereech = Convert.ToString(row.Range["AB" + rowNumber.ToString()].Value) + "/" + Convert.ToString(row.Range["AC" + rowNumber.ToString()].Value) + "/" + Convert.ToString(row.Range["AD" + rowNumber.ToString()].Value);
string derniereech = Convert.ToString(row.Range["AE" + rowNumber.ToString()].Value) + "/" + Convert.ToString(row.Range["AF" + rowNumber.ToString()].Value) + "/" + Convert.ToString(row.Range["AG" + rowNumber.ToString()].Value);
int nbimp = MyConverter.ToInt(Convert.ToString(row.Range["AH" + rowNumber.ToString()].Value));
decimal mt_imp_1 = Convert.ToDecimal(row.Range["AI" + rowNumber.ToString()].Value);
string dateimpaye1 = Convert.ToString(row.Range["AJ" + rowNumber.ToString()].Value) + "/" + Convert.ToString(row.Range["AK" + rowNumber.ToString()].Value) + "/" + Convert.ToString(row.Range["AL" + rowNumber.ToString()].Value);
int motifimpaye1 = MyConverter.ToInt(Convert.ToString(row.Range["AM" + rowNumber.ToString()].Value));
string dateimpaye2 = Convert.ToString(row.Range["AN" + rowNumber.ToString()].Value) + "/" + Convert.ToString(row.Range["AO" + rowNumber.ToString()].Value) + "/" + Convert.ToString(row.Range["AP" + rowNumber.ToString()].Value);
int motifimpaye2 = MyConverter.ToInt(Convert.ToString(row.Range["AQ" + rowNumber.ToString()].Value));
string dateimpaye3 = Convert.ToString(row.Range["AR" + rowNumber.ToString()].Value) + "/" + Convert.ToString(row.Range["AS" + rowNumber.ToString()].Value) + "/" + Convert.ToString(row.Range["AT" + rowNumber.ToString()].Value);
int motifimpaye3 = MyConverter.ToInt(Convert.ToString(row.Range["AU" + rowNumber.ToString()].Value));
DataRow dr = values.Rows.Add(groupe, sousgroupe, codeapporteur, apporteur, dossier, siren, client, adresse, localite, cp, ville, telephone, etat, bic, iban, domiciliationrib, bienfinance, mt_ht_fi, mt_ech, dateapp, terme, premiereech, derniereech, nbimp, mt_imp_1, dateimpaye1, motifimpaye1, dateimpaye2, motifimpaye2, dateimpaye3, motifimpaye3);
}
else
{
foreach (Excel.Range eRange in row.Range["A1:AU1"].Cells)
{
if (eRange.Value != "TEL2" && eRange.Value != "TEL3" && eRange.Value != "TEL4" && eRange.Value != "TEL5" && eRange.Value != "Col_21" && eRange.Value != "Col_22" && eRange.Value != "Col_25" && eRange.Value != "Col_26" && eRange.Value != "Col_28" && eRange.Value != "Col_29")
values.Columns.Add(eRange.Value);
}
premiere = false;
}
}
return values;
}
这是我的excel文件的示例行:
答案 0 :(得分:1)
尝试删除该行152,或在文档中向上或向下移动,并查看该行后面是否有错误。
如果将该行移动到145,然后程序在第145行停止,那么我们找到了问题的根源:它是数据。该行中可能存在无效或不可读的字符,并且会使程序崩溃。它最有可能是其中一行中应该是十进制或整数的字母。
此外,每次运行循环时,都要实例化所有字符串,小数和整数。你应该在循环之前声明它们,然后在循环中设置它们的值。这是一般的良好做法。