【发布时间】: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