问题
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>
回答1:
I figured it out after petelids told me to look at the XML from the excel spreadsheet. My original program kept crashing from what I though was ternary conditional always being true, even when the particular excel cells were not using SharedStrings. However, the xml shows that while the cells with the t="s", the ones that do not have SharedStrings do not, meaning that the non-sharedString cells did not have a corresponding DataType. Because of this, the code was failing because of DataType
attribute of the (((Cell)r.ChildElements[0])
, which was actually null, so trying to get the Value
of it resulted in a null pointer exception. To fix this, I ended up simply checking to see if the DataType
was null. When it was, then I just got the inner xml contents. When it was not, then I used the xml contents as the SharedStringsTable index to find the actual string. Case closed!
来源:https://stackoverflow.com/questions/25187020/xml-cells-incorrectly-being-shown-as-sharedstrings-when-they-are-not