用C#读取Excel电子表格,不相等的列/值

我有一个excel电子表格输出作为XML与定义为这样的列:

<Row ss:AutoFitHeight="0"> <Cell ss:StyleID="ColumnHead"> <ss:Data ss:Type="String">#</ss:Data> </Cell> <Cell ss:StyleID="ColumnHead"> <ss:Data ss:Type="String">prefix</ss:Data> </Cell> <Cell ss:StyleID="ColumnHead"> <ss:Data ss:Type="String">name</ss:Data> </Cell> <Cell ss:StyleID="ColumnHead"> <ss:Data ss:Type="String">label</ss:Data> </Cell> <Cell ss:StyleID="ColumnHead"> <ss:Data ss:Type="String">totalLabel</ss:Data> </Cell> <Cell ss:StyleID="ColumnHead"> <ss:Data ss:Type="String">base schema</ss:Data> </Cell> <Cell ss:StyleID="ColumnHead"> <ss:Data ss:Type="String">systemid</ss:Data> </Cell> <Cell ss:StyleID="ColumnHead"> <ss:Data ss:Type="String">prohibit</ss:Data> </Cell> </Row> 

这里有一个例子:

 <Row ss:AutoFitHeight="0"> <Cell ss:StyleID="NoBorderNumberCell"> <ss:Data ss:Type="Number">1</ss:Data> </Cell> <Cell ss:StyleID="NoBorderCell"> <ss:Data ss:Type="String">ifrs</ss:Data> </Cell> <Cell ss:StyleID="NoBorderCell"> <ss:Data ss:Type="String">AccountingProfit</ss:Data> </Cell> <Cell ss:StyleID="NoBorderCell"> <ss:Data ss:Type="String">Accounting profit</ss:Data> </Cell> <Cell ss:StyleID="NoBorderCell"/> <Cell ss:StyleID="NoBorderCell"> <ss:Data ss:Type="String">full_entry_point</ss:Data> </Cell> </Row> 

问题是,我如何检测哪些单元丢失了哪些列? 是否需要为所有空单元格提供空白自闭标签,以便每次都能将每列与每个值alignment?

我将如何在C#中pipe理这种情况? 我有最低限度的权利,不知道如何分开它,以弥补缺失的列。

  if (reader.Name == "ss:Data") { while (reader.Read()) Response.Write(reader.Value); } 

您可以使用LinqToExcel来读取数据,并且它应该更快,因为它不需要加载整个文件。 但是,LinqToExcel使用OLEDB来读取文件而不是Open XML SDK。

 var excel = new ExcelQueryFactory("excelFileName"); var firstRow = (from c in excel.Worksheet() select c).First(); 

请参阅LinqToExcel的其他文档 。

否则,你可以用LINQ来做:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using System.Xml.Linq; namespace UnitTest { [TestFixture] public class TestCode { [Test] public void ReadExcelCellTest() { XDocument document = XDocument.Load(@"C:\TheFile.xml"); XNamespace workbookNameSpace = @"urn:schemas-microsoft-com:office:spreadsheet"; // Get worksheet var query = from w in document.Elements(workbookNameSpace + "Workbook").Elements(workbookNameSpace + "Worksheet") where w.Attribute(workbookNameSpace + "Name").Value.Equals("Settings") select w; List<XElement> foundWoksheets = query.ToList<XElement>(); if (foundWoksheets.Count() <= 0) { throw new ApplicationException("Worksheet Settings could not be found"); } XElement worksheet = query.ToList<XElement>()[0]; // Get the row for "Seat" query = from d in worksheet.Elements(workbookNameSpace + "Table").Elements(workbookNameSpace + "Row").Elements(workbookNameSpace + "Cell").Elements(workbookNameSpace + "Data") where d.Value.Equals("Seat") select d; List<XElement> foundData = query.ToList<XElement>(); if (foundData.Count() <= 0) { throw new ApplicationException("Row 'Seat' could not be found"); } XElement row = query.ToList<XElement>()[0].Parent.Parent; // Get value cell of Etl_SPIImportLocation_ImportPath setting XElement cell = row.Elements().ToList<XElement>()[1]; // Get the value "Leon" string cellValue = cell.Elements(workbookNameSpace + "Data").ToList<XElement>()[0].Value; Console.WriteLine(cellValue); } } }