openxml如何用OpenXmlReader读取inlineStr
我有以下代码:
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(); } } } }
此代码可以读取数据types为数字的任何单元格,但无法读取inlineStr
用生产力工具查看XML,我认为代码可以阅读下面的XML
<x:cr="D2" t="n"> <x:v>328</x:v> </x:c>
但它不能阅读这个(或者我不知道该怎么做)
<x:cr="F1" s="6" t="inlineStr"> <x:is> <x:t>T1</x:t> </x:is> </x:c>
任何帮助将不胜感激。
谢谢
你的代码看起来应该正确地读取值,但是更多的信息对于检测问题的原因是有用的。
我想你可以尝试检查types是否是InlineString
,但不能只使用GetText()
方法,因为InlineString
不是从OpenXMlLeafTextElement
衍生出来的。
我还没有testing过,但我build议你尝试一下:
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; } }
或者类似的东西。 如果你有这个代码的问题,让我知道,所以我会纠正它。
一些参考: