1

我有一个作为 XML 的 Excel 电子表格输出,其列定义如下:

   <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);
      }
4

1 回答 1

1

您可以使用 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);
        }
    }
}
于 2013-02-15T00:48:10.580 回答