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>

そしてここに1つの例の行があります:

<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 は、Open XML SDK の代わりに OLEDB を使用してファイルを読み取ります。

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 に答える