![](/img/trans.png)
[英]How to get a merged cell from Excel using DocumentFormat.OpenXML or ClosedXML C#
[英]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.