C# How do I read Excel cell XML values with character level formatting? (Multiple <t> elements within <si>) -
i working on small application read contents of excel worksheet , import data strings windows form datagridview.
i have implemented dietmar schoder's code example this. special him posting it.
i new xml , have been stuck on problem while now.
the cells "character level" formatting contain 2 or more separate <t>
text values within <si>
xml element.
here snippet excel file's sharedstrings.xml file
<si> <r> <rpr> <b/> <sz val="12"/> <color rgb="ffff0000"/> <rfont val="arial"/> <family val="2"/> </rpr> <t>text a</t> </r> <r> <rpr> <b/> <sz val="12"/> <color theme="1"/> <rfont val="arial"/> <family val="2"/> </rpr> <t xml:space="preserve"> text b</t> </r> </si>
this cell contains text "text text b" returns null because cell has character level formatting , therefore 2 <t>
tags. "text a" has strike-through, colored differently or bold etc , "text b" doesn't.
the text values assigned following line of code.
text = workbook.sharedstrings.si[convert.toint32(_value)].t;
is there anyway concatenate strings both <t>
elements before assigning text variable?
edit: think have no narrowed problem down sharedstrings.cs file , deserialization of sharedstrings.xml
sharedstrings = deserializedzipentry<sst>(getziparchiveentry(ziparchive, @"xl/sharedstrings.xml"));
sst class:
[serializable()] [xmltype(namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")] [xmlroot("sst", namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")] public class sst { [xmlattribute] public string uniquecount; [xmlattribute] public string count; [xmlelement("si")] public sharedstring[] si; public sst() { } } public class sharedstring { public string t; }
i have been unable edit class in way correctly interpret both t elements text values.
solved 1 myself after studying more xml serialization , many other similar questions 1 on here.
sst class:
public class sst { [xmlattribute] public string uniquecount; [xmlattribute] public string count; [xmlelement("si")] public sharedstring[] si; public sst() { } } public class sharedstring { public string t; [xmlelement("r")] public nestedstring[] ns; public sharedstring() { } } public class nestedstring { public string t; }
and assignment of cell's text:
if (workbook.sharedstrings.si[convert.toint32(_value)].t != null) { text = workbook.sharedstrings.si[convert.toint32(_value)].t; } else if (workbook.sharedstrings.si[convert.toint32(_value)].ns != null) { (int = 0; < workbook.sharedstrings.si[convert.toint32(_value)].ns.length; i++) { text += workbook.sharedstrings.si[convert.toint32(_value)].ns[i].t; } }
Comments
Post a Comment