I am parsing through an Excel spreadsheet and had some problems with some values, so on the suggestions of a stackoverflow member, I evaluated them as shared strings. Now, however, some of the cell values are not shared strings and my conditional still evaluates as true, meaning they are SharedStrings. So I am wondering if my code for evaluating these is correct or maybe there's something wrong with the Excel spreadsheets I've been working with. Here is an example conditional, which in this case should evaluate false as the SerialNumber column is not a shared string, but it is evaluated as it being a shared string and therefore causes the program to crash.
bool isSharedString = (((Cell)r.ChildElements[0]).DataType.Value == CellValues.SharedString);
row["SerialNumber"] = isSharedString ? stringTable.SharedStringTable.ElementAt(int.Parse(r.ChildElements[0].InnerText)).InnerText : r.ChildElements[0].InnerText;
This is the inner outer xml code for the row that I am working with. The only ones that are not shared strings are A2 and G2.
<x:row r="2" spans="1:14" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:c r="A2"><x:v>20000001</x:v></x:c>
<x:c r="B2" t="s"><x:v>14</x:v></x:c>
<x:c r="C2" s="1" t="s"><x:v>19</x:v></x:c>
<x:c r="D2" t="s"><x:v>19</x:v></x:c>
<x:c r="E2" t="s"><x:v>19</x:v></x:c>
<x:c r="F2" t="s"><x:v>19</x:v></x:c>
<x:c r="G2"><x:v>0</x:v></x:c>
<x:c r="H2" t="s"><x:v>19</x:v></x:c>
<x:c r="I2" t="s"><x:v>19</x:v></x:c>
<x:c r="J2" t="s"><x:v>20</x:v></x:c>
<x:c r="K2" t="s"><x:v>22</x:v></x:c>
<x:c r="L2" t="s"><x:v>20</x:v></x:c>
<x:c r="M2" t="s"><x:v>22</x:v></x:c>
<x:c r="N2" t="s"><x:v>19</x:v></x:c>
</x:row>
r
definitely the row you think it is? Could you output theCellReference
property of theCell
? If it is the correct one, could you extract the raw XML from the excel file in question and show the XML fromxl\worksheets\sheet1.xml
for the row in question? – Mimimimics="1"
ort="s"
, while the non-SharedString cells don't have either of these. Do you know what these mean and how I can differentiate between them in code? – Studrow["Description"] = (((Cell)r.ChildElements[13]).DataType != null) ? stringTable.SharedStringTable.ElementAt(int.Parse(r.ChildElements[13].InnerText)).InnerText : r.ChildElements[13].InnerText;
and now it works. – Studt="s"
means it's a shared string whereast="str"
means it's inline. I think a non-existent type denotes that the value is inline although I can't find any documentation. Thes=1
refers to the style being applied to the cell. Is your code now working? – Mimimimic