【问题标题】:OpenXML CellValue is giving Hexadecimal Value While Reading in C#OpenXML CellValue 在 C# 中读取时给出十六进制值
【发布时间】:2021-11-26 11:22:27
【问题描述】:

foreach (Row row in sheetData.Elements<Row>())
                                        {
                                            ArrayList data = new ArrayList();
                                            if (!firstRow)
                                            {
                                                int count = row.Elements<Cell>().Count();
                                                //await Console.Out.WriteLineAsync($"Cell Count:  [{count}]");
                                                var firstCol = true;
                                                foreach (Cell cell in row.Elements<Cell>())
                                                {
                                                    if (!firstCol)
                                                    {
                                                        int id = -1;
                                                        if (cell.DataType != null && cell.DataType == CellValues.SharedString)
                                                        {
                                                            if (int.TryParse(cell.InnerText, out id))
                                                            {
                                                                var value = workbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(id).InnerText;
                                                                data.Add(value);
                                                            }
                                                        }
                                                        else if (cell.DataType == null && cell.CellValue != null)
                                                        {
                                                            data.Add(cell.CellValue.Text.ToString());
                                                        }
                                                        else
                                                        {
                                                            data.Add(string.Empty);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        firstCol = false;
                                                    }

                                                }
                                                rowList.Add(data);
                                            }
                                            else 
                                            {
                                                firstRow = false;
                                            }

                                        }

当 CellValue 不为 null 时,传入的值为 16 进制格式的形状,以字符串形式保存在 ArrayList 中。我想要保存在 Excel 工作表中的实际值。

【问题讨论】:

  • 不要读取原始数据,而是使用 ExcelReader 或 Epplus 等库
  • 什么原始值? hexadecimal format 是什么意思?如果单元格包含字符串 Text 将返回它。 ToString() 不需要,因为 Text 已经是一个字符串。
  • 这是单元格 0.01887 中的实际值,单元格值显示 1.8870000000000001E-2
  • 科学计数法中的数字相同。不是十六进制 - 类似于0x402C9CDA2D0。 Excel 中数字的显示方式取决于单元格的样式。 Text 是显示文本,不是实际存储的值

标签: c# .net excel .net-core openxml


【解决方案1】:

这里不使用 cell.InnerText

if (int.TryParse(cell.InnerText, out id))

您应该使用 cell.CellValue.Text 以便将该行更改为

if (int.TryParse(cell.CellValue.Text, out id))

我曾经在从单元格中读取字符串文本时执行以下操作

private static string ReadCell(DocumentFormat.OpenXml.Spreadsheet.Cell cell, DocumentFormat.OpenXml.Packaging.WorkbookPart workbookPart)
{
  var cellValue = cell.CellValue;
  var text = (cellValue == null) ? cell.InnerText : cellValue.Text;
  if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString))
  {
    text = workbookPart.SharedStringTablePart.SharedStringTable
      .Elements<DocumentFormat.OpenXml.Spreadsheet.SharedStringItem>().ElementAt(Convert.ToInt32(cell.CellValue.Text)).InnerText;
  }
  return (text ?? string.Empty).Trim();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 2010-12-07
    • 2015-05-01
    • 1970-01-01
    • 2016-02-28
    • 2015-01-26
    • 2011-10-07
    • 2015-12-09
    相关资源
    最近更新 更多