[英]Retrieving data from excel cell that is integer .xslx document in C# documentformat.openxml
Iterating over rows and cells in my spreadsheet document but unable to retrieve the value from a cell if it is a number (string works fine).迭代我的电子表格文档中的行和单元格,但如果它是一个数字,则无法从单元格中检索值(字符串工作正常)。
For example:例如:
// ...
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.
}
}
When a cell is a number and not a string the value is equal to the first cell of the spreadsheet (0, 0).当单元格是数字而不是字符串时,该值等于电子表格的第一个单元格 (0, 0)。
Am I missing something here in retrieving numbers from cells?在从单元格中检索数字时,我在这里遗漏了什么吗?
Code is quick and dirty, but works:代码又快又脏,但有效:
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.