XML细胞被不正确地显示为SharedStrings当他们不 [英] XML cells incorrectly being shown as SharedStrings when they are not

查看:123
本文介绍了XML细胞被不正确地显示为SharedStrings当他们不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解析通过一个Excel US preadsheet并且有一些问题,一些值,因此在计算器成员的建议,我评价他们为共享字符串。然而,现在一些单元格的值是不共享的字符串和我的条件还评估为真,这意味着他们SharedStrings。所以我想知道如果我的code评价这些是正确的或者是有什么毛病的Excel US preadsheets我一直在使用。这里是有条件的一个例子,而在这种情况下,应该评估false作为的SerialNumber列不是共享字符串,但它被评价为它是一个共享串并因此导致崩溃的程序。

 布尔isSharedString =(((Cell)的r.ChildElements [0])DataType.Value == CellValues​​.SharedString);行[的SerialNumber] = isSharedString? stringTable.SharedStringTable.ElementAt(int.Parse(r.ChildElements [0] .InnerText))的InnerText:r.ChildElements [0] .InnerText;

这是内外部XML code对于我一起工作的一行。未共享的串的,仅仅是A2和G2。

 < X:行r =2跨度=1:14的xmlns:X =htt​​p://schemas.openxmlformats.org/s$p$padsheetml/ 2006 /主>
    &所述; X:C R =A2>&所述; X:V> 20000001&下; / X:V>&下; / X:c取代;
    &所述; X:C R =B2T =的>&所述; X:V&GT 14所述; / X:V>&下; / X:c取代;
    &所述; X:C R =C2S =1T =的>&所述; X:V&GT 19所述; / X:V>&下; / X:c取代;
    &所述; X:C R =D2T =的>&所述; X:V&GT 19所述; / X:V>&下; / X:c取代;
    &所述; X:C R =E2T =的>&所述; X:V&GT 19所述; / X:V>&下; / X:c取代;
    &所述; X:C R =F2T =的>&所述; X:V&GT 19所述; / X:V>&下; / X:c取代;
    &所述; X:C R =G2>&所述; X:V> 0℃/ X:V>&下; / X:c取代;
    &所述; X:C R =H2T =的>&所述; X:V&GT 19所述; / X:V>&下; / X:c取代;
    &所述; X:C R =I2T =的>&所述; X:V&GT 19所述; / X:V>&下; / X:c取代;
    &所述; X:C R =J2T =的>&所述; X:V> 20℃/ X:V>&下; / X:c取代;
    &所述; X:C R =K2T =的>&所述; X:V> 22℃/ X:V>&下; / X:c取代;
    &所述; X:C R =L 2T =的>&所述; X:V> 20℃/ X:V>&下; / X:c取代;
    &所述; X:C R =M2T =的>&所述; X:V> 22℃/ X:V>&下; / X:c取代;
    &所述; X:C R =N 2T =的>&所述; X:V&GT 19所述; / X:V>&下; / X:c取代;
< / X:行>


解决方案

我想通了之后petelids告诉我一下从Excel的小号preadsheet的XML。我原来的计划一直从我虽然是三元条件一直为真,即使在特定的Excel单元格没有使用SharedStrings崩溃。然而,XML显示,虽然不具有SharedStrings细胞用t =S,那些不这样做,这意味着非sharedString细胞不具有相应的数据类型。正因为如此,在code是因为数据类型的失败的属性(((Cell)的r.ChildElements [0]),里面竟是空,所以试图让它导致了一个空指针异常。为了解决这个问题,我结束了简单的检查,看看如果数据类型为null。当它是,然后我就得到了内部XML的内容。如果不是这样,那么我使用的XML内容的SharedStringsTable索引来查找实际的字符串。结案!

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>

解决方案

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!

这篇关于XML细胞被不正确地显示为SharedStrings当他们不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆