Search code examples
excelexcel-2007openxmlopenxml-sdk

openxml how to read an inlineStr with an OpenXmlReader


I have the following code:

public static void ReadExcelFileSAX(string filename)
{
    using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(filename, true))
    {
        WorkbookPart workbookPart = myDoc.WorkbookPart;
        WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();

        OpenXmlReader reader = OpenXmlReader.Create(worksheetPart);
        string text;
        while (reader.Read())
        {
           if (reader.ElementType == typeof(CellValue))
           {
                text = reader.GetText();
            }
        }
    }
}

This code can read any cell where the data type is a number, but it cannot read inlineStr

Looking at the XML with Productivity tool, I think the code can read the following XML

<x:c r="D2" t="n">
    <x:v>328</x:v>
</x:c>

But it can't read the this one (or I don't know how to do it)

<x:c r="F1" s="6" t="inlineStr">
    <x:is>
        <x:t>T1</x:t>
    </x:is>
</x:c>

Any help will be appreciated.

Thanks


Solution

  • Your code look like it should read the values correctly, but some more information would be useful to detect the cause of the issue.

    I suppose you can try to check if the type is InlineString, but you can't just use GetText() method, because InlineString is not dervide from OpenXMlLeafTextElement.

    I haven't tested this yet, but I suggest you to try:

    while (reader.Read())
    {
       if (reader.ElementType == typeof(CellValue))
       {
            text = reader.GetText();
       }
       else if (reader.ElementType == typeof(InlineString)) //or instead of this, check type of its child node and use it inside this if statement
       {
            text = (reader.LoadCurrentElement() as InlineString).Text.Text;
       }
    }
    

    Or something similiar to this. If you have any issues with this code, let me know, so I'll correct it.

    Some references: