我是XML的新手,我尝试了解XML的格式,并亲自尝试过并在线搜索。但似乎无法找到一个例子来指导我所需要的东西。所以我将从这个问题开始。我已经解压缩了一个XLSX excel文件,这是它的结尾部分,它给了我与excel中看到的不同的数量:
<row r="2306" spans="1:20" x14ac:dyDescent="0.3">
<c r="A2306" s="2">
<v>42872</v>
</c><c r="B2306" t="s">
<v>53</v>
</c><c r="C2306">
<v>12002398</v>
</c><c r="D2306" t="s">
<v>15</v>
</c><c r="E2306" t="s">
<v>20</v>
</c><c r="F2306" t="s">
<v>0</v>
</c><c r="G2306" t="s">
<v>1</v>
</c><c r="H2306" t="s">
<v>2</v>
</c><c r="I2306">
<v>120002122</v>
</c><c r="J2306">
<v>10</v>
</c><c r="K2306" s="3" t="s">
<v>50</v>
</c><c r="L2306" s="3" t="s">
<v>18</v>
</c><c r="M2306">
<v>2800</v>
</c><c r="N2306" t="s">
<v>3</v>
</c><c r="O2306" s="4">
<v>6.15</v>
</c><c r="P2306" s="5">
<v>17220</v>
</c><c r="Q2306" t="s">
<v>4</v>
</c><c r="R2306" t="s">
<v>5</v>
</c>
<c r="S2306" t="s">
<v>342</v>
</c>
<c r="T2306" t="s">
<v>6</v>
</c>
</row>
<row r="10451" spans="1:1" x14ac:dyDescent="0.3">
<c r="A10451" s="1"/>
</row>
<row r="23679" spans="1:1" x14ac:dyDescent="0.3">
<c r="A23679" s="1"/>
</row>
这里在XML文件中我可以在EXCEL中看到2306行,但是当我使用OpenXML SAX方法读取ROW属性并计算行时,我得到2308.额外的2行数据由空行10451和无论如何,我可以忽略这两行吗?我试图检查单元格是否有cellValue,如果有,我将跳到下一行并增加totalRow。但是,我尝试过使用reader.skip(),它只跳过1个单元格。无论如何,我可以跳过整行并转到下一行?这是我目前的代码,希望有人可以指导我。感谢。
// The SAX approach.
static void ReadExcelFileSAX(string fileName)
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
OpenXmlReader reader = OpenXmlReader.Create(worksheetPart);
int totalRow = 0;
while (reader.Read())
{
if (reader.ElementType == typeof(Row))
{
reader.ReadFirstChild();
do
{
if (reader.ElementType == typeof(Cell))
{
Cell c = (Cell)reader.LoadCurrentElement();
if(c.CellReference != null)
{
string column = c.CellReference;
column = column.Substring(0,1);
if(column == "B")
{
//MessageBox.Show(GetCellValue(workbookPart.SharedStringTablePart.SharedStringTable, c));
totalRow++;
}
}
}
}
while (reader.ReadNextSibling());
}
}
MessageBox.Show("Total Row: " + totalRow.ToString());
}
}
public static string GetCellValue(SharedStringTable sharedStringTable, Cell cell)
{
string value = cell.CellValue.InnerText;
if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
{
return sharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
}
else
{
return value;
}
}