簡體   English   中英

從 excel 單元格中檢索數據,即 C# documentformat.openxml 中的 integer.xslx 文檔

[英]Retrieving data from excel cell that is integer .xslx document in C# documentformat.openxml

迭代我的電子表格文檔中的行和單元格,但如果它是一個數字,則無法從單元格中檢索值(字符串工作正常)。

例如:

// ...

List<Row> rows = sheetData.Elements<Row>().Where(r => r.Elements<Cell>().Any(ce => ce.DataType != null)).ToList();
foreach (Row row in rows) {
  foreach (Cell cell in row.Elements<Cell>()) {
    int id = int32.Parse(cell.InnerText);
    var value = workbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(id);

    Console.WriteLine(value); // the cell data
    // cell data is a string = fine.
    // cell data is a number = returns cell data from row 1, cell 1.

  }
}

當單元格是數字而不是字符串時,該值等於電子表格的第一個單元格 (0, 0)。

在從單元格中檢索數字時,我在這里遺漏了什么嗎?

代碼又快又臟,但有效:

if (cell.DataType == CellValues.SharedString && cell.DataType != null) {
  value = workbookPart.SharedStringTablePart.SharedStringTable
    .Elements<SharedStringItem>()
    .ElementAt(Convert.ToInt32(cell.InnerText)).InnerText;
} else {
  value = cell.InnerText.Replace(".", string.Empty);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM