简体   繁体   English

从 excel 单元格中检索数据,即 C# documentformat.openxml 中的 integer.xslx 文档

[英]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.

相关问题 如何使用 DocumentFormat.OpenXML 或 ClosedXML C# 从 Excel 获取合并单元格 - How to get a merged cell from Excel using DocumentFormat.OpenXML or ClosedXML C# 使用DocumentFormat.OpenXml(C#)更新.docx文档中的目录 - Update TOC in .docx document using DocumentFormat.OpenXml (C#) 使用Documentformat.OpenXML在C#中验证Excel文件 - Validate Excel file in c# using Documentformat.OpenXML 如何使用DocumentFormat.OpenXml C#获取excel工作表中的特定列? - How can I get the specific column in excel worksheet using DocumentFormat.OpenXml C#? 使用Microsoft DocumentFormat.OpenXml SDK在C#中读取Excel文件 - Reading excel file in c# using Microsoft DocumentFormat.OpenXml SDK 如何使用 C# 和 DocumentFormat.OpenXml nuget ZEFE90A8E604A7C84A70E8 格式化 Excel 中的数字 - How to format number in Excel by using C# and DocumentFormat.OpenXml nuget package? 使用DocumentFormat.OpenXml(c#)合并Word文档 - Merging word-documents using DocumentFormat.OpenXml(c#) CSharp DocumentFormat.OpenXML和Excel-将工作表中的数据从一个电子表格复制到另一个电子表格(具有代码) - CSharp DocumentFormat.OpenXML and Excel - Copy Data in Worksheet from one spreadsheet to another (Have code) DocumentFormat.OpenXml修改文档的创建者属性 - DocumentFormat.OpenXml Modify Creator Propery of Document 使用DocumentFormat.OpenXml替换Word中的数据 - Replacing data in word using DocumentFormat.OpenXml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM