Я использую принятое здесь решение , чтобы преобразовать лист excel в datatable. Это отлично работает, если у меня есть "идеальные" данные, но если у меня есть пустая ячейка в середине моих данных, она, кажется, помещает неправильные данные в каждый столбец.
Я думаю, это потому, что в приведенном ниже коде:
row.Descendants<Cell>().Count()
- количество заполненных ячеек (не все столбцы) И:
GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i));
похоже, найдет следующую заполненную ячейку (не обязательно то, что находится в этом индексе), поэтому, если первый столбец пуст и я вызываю ElementAt (0), он возвращает значение во втором столбце.
Вот полный код синтаксического анализа.
DataRow tempRow = dt.NewRow();
for (int i = 0; i < row.Descendants<Cell>().Count(); i++)
{
tempRow[i] = GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i));
if (tempRow[i].ToString().IndexOf("Latency issues in") > -1)
{
Console.Write(tempRow[i].ToString());
}
}