簡體   English   中英

Openxml單元格返回InnerText而不是實際值

[英]Openxml cell returns InnerText not the actual value

我正在開發一個可重用的代碼來讀取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 = "TRUE";
                                    break;
                            }
                            break;
                    }
                }
            }
            return value;
        }

我從以下代碼塊調用此方法:

public string IsPackingListValid()
        {           

            // Open the spreadsheet document for read-only access.
            using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
            {
                // Retrieve a reference to the workbook part.
                WorkbookPart wbPart = document.WorkbookPart;

                // Find the sheet with the supplied name, and then use that 
                // Sheet object to retrieve a reference to the first worksheet.
                Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().
                  Where(s => s.Name == sheetName).FirstOrDefault();

                // Throw an exception if there is no sheet.
                if (theSheet == null)
                {
                    throw new ArgumentException("Could not find work sheet: " + sheetName);
                }

                // Retrieve a reference to the worksheet part.
                WorksheetPart worksheetPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));

                return ReadCellValue(WorksheetPart worksheetPart, "B2")
            }   
}

該方法不返回某些單元格的實際值。 對於某些單元格,它返回值,對於某些單元格,它返回內部文本。 我調試並檢查

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

上面的代碼返回null。

我找不到為什么它對某些單元格有效而對某些單元格無效。 任何幫助解決此問題。

根據我的理解(至少對於我檢查過的文檔而言,這是正確的),SharedStringTablePart是WorkbookPart的后代而不是WorksheetPart。 因此,修復代碼的方法是從IsPackingListValid()方法中的wbPart獲取共享字符串的列表:

List<SharedStringItem> sharedStrings = wbPart.SharedStringTablePart.SharedStringTable.ChildElements.OfType<SharedStringItem>().ToList();

然后將其作為附加參數傳遞給ReadCellValue方法:

return ReadCellValue(worksheetPart, "B2", sharedStrings);

之后,您可以在ReadCellValue()方法中在交換機內部獲取Shared字符串的實際值:

value = cell.InnerText;
int sharedStringIndex;

if (int.TryParse(cell.InnerText, out sharedStringIndex) && sharedStrings.Count > sharedStringIndex
    && sharedStrings[sharedStringIndex].Text != null)
{
    value = sharedStrings[sharedStringIndex].Text.Text;
}

另外,還需要對SharedString數據類型進行額外檢查(有關更多信息,請參見http://msdn.microsoft.com/zh-cn/library/office/ff921204(v=office.14).aspx ):

    private static string ReadCellValue(WorksheetPart worksheetPart, string cellAddress, List<SharedStringItem> sharedStrings)
    {
        try
        {
            string value = null;
            var cell = worksheetPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == cellAddress).FirstOrDefault();
            value = cell.InnerText;
            if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
            {
                int sharedStringIndex;
                if (int.TryParse(cell.InnerText, out sharedStringIndex) && sharedStrings.Count > sharedStringIndex && sharedStrings[sharedStringIndex].Text != null)
                {
                    value = sharedStrings[sharedStringIndex].Text.Text;
                }
            }
            return value;
        }
        catch (Exception ex) { throw ex; }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM