按C#中的列读取XML Spread Sheet 2003

时间:2010-09-20 20:14:53

标签: c# xml excel

我需要在项目中阅读XML Spread Sheet 2003,我对列有问题。

此示例显示您拥有模板之王我的数据是:

Name Of | FirstName | SecondName | ... | ... 

Monday  |     1     |     2      |   3 |   2 

Tuesday |     0     |     1      |   1 |   ?

Wednesday |   2     |     ?      |   ? |   ?

我需要提取每列中的每个数据,问题出现在这里:C#只能逐行阅读这个XML Spread Sheet,在这个例子中,我不知道如何为每一列准确地提供每个数据。而且我事先无法知道数据。

这是我的代码的开头

var queryPeriodSheet = from worksheet in data.Descendants(ss + "Worksheet")
                               where worksheet.Attribute(ss + "Name").Value == "sheet1"
                               select worksheet;

var rows = from row in queryPeriodSheet.Descendants(ss + "Row")
select row;

...

我需要你的帮助,

谢谢

1 个答案:

答案 0 :(得分:1)

您需要查看每行中每个单元格元素的ss:Index属性,因为空单元格没有XML元素。

我使用以下代码在一行中的特定单元格中查找文本:

    /// <summary>
    /// 
    /// </summary>
    /// <param name="rowElement"></param>
    /// <param name="cellIndex">0-based cell index.</param>
    /// <returns></returns>
    public string GetCellData(XmlElement rowElement, int cellIndex)
    {
        XmlNodeList cellElements = rowElement.SelectNodes("ss:Cell", xmlnsManager);
        if (cellElements != null)
        {
            int elementIndex = 0;

            foreach (XmlElement cellElement in cellElements)
            {
                // Special case: cells can be skipped. If so, there is a ss:Index attribute that contains the 1-based index of the cell.
                if (cellElement.HasAttribute("ss:Index"))
                {
                    elementIndex = Convert.ToInt32(cellElement.GetAttribute("ss:Index")) - 1;
                }

                // If cell has been skipped.
                if (cellIndex < elementIndex)
                    return null;

                if (cellIndex == elementIndex)
                    return cellElement.InnerText.Trim();

                elementIndex++;
            }
        }
        return null;
    }