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

时间:2013-02-14 23:51:05

标签: c# xml excel xml-parsing

我有一个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>

问题是,如何检测哪些单元格缺少哪些列?是否要求源为所有空单元格都有一个空白自关闭标记,以便每次都能将每列配对每个值?

我如何在C#中管理这种情况?我有最低限度的权利,不知道如何将其分开来解释缺少的列。

 if (reader.Name == "ss:Data")
      {                                       

          while (reader.Read())
               Response.Write(reader.Value);
      }

1 个答案:

答案 0 :(得分:1)

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

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

请参阅documentation for 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);
        }
    }
}