小编use*_*569的帖子

Openxml单元格返回InnerText而不是实际值

我正在开发一个可重用的代码来读取excel单元格值.我的代码如下:

private string ReadCellValue(WorksheetPart worksheetPart, string cellAddress)
        {
            string value = null;

            Cell theCell = worksheetPart.Worksheet.Descendants<Cell>().
                  Where(c => c.CellReference == cellAddress).FirstOrDefault();

            // If the cell does not exist, return an empty string.
            if (theCell != null)
            {
                value = theCell.InnerText;

                if (theCell.DataType != null)
                {
                    switch (theCell.DataType.Value)
                    {
                        case CellValues.SharedString:

                            var stringTable =
                                worksheetPart.GetPartsOfType<SharedStringTablePart>()
                                .FirstOrDefault();

                            if (stringTable != null)
                            {
                                value =
                                    stringTable.SharedStringTable
                                    .ElementAt(int.Parse(value)).InnerText;
                            }
                            break;

                        case CellValues.Boolean:
                            switch (value)
                            {
                                case "0":
                                    value = "FALSE";
                                    break;
                                default:
                                    value = …
Run Code Online (Sandbox Code Playgroud)

excel openxml openxml-sdk

2
推荐指数
1
解决办法
2619
查看次数

标签 统计

excel ×1

openxml ×1

openxml-sdk ×1