簡體   English   中英

使用C#讀取Excel電子表格,列/值不相等

[英]Reading Excel Spreadsheet with C#, unequal Column/Values

我有一個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);
      }

您可以使用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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM