Read XML Spread Sheet 2003 by column in C#
I need to read a XML Spread Sheet 2003 in a project and I have a problem with columns.
This example show you have king of template my data are :
Name Of | FirstName | SecondName | ... | ... Monday | 1 | 2 | 3 | 2 Tuesday | 0 | 1 | 1 | ? Wednesday | 2 | ? | ? | ?
I need to extract every data in each columns and the problem is here : C# read this XML Spread Sheet row by row only and with this example I don't know how I can have exactly every data for each column. Moreover I can't know in a开发者_StackOverflow中文版dvance the data.
Here is the beginning of my code
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;
...
I need your help,
Thank you
You need to look at the ss:Index attribute in each cell element in each row since there are no XML elements for empty cells.
I use the following code to find text in a specific cell in a row:
/// <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;
}
精彩评论