简体   繁体   English

使用C#将XML数据导出到Excel

[英]Export XML data to Excel using C#

XML:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<IMPORT ExportDate="2016-03-02" Exercise="1" User="asa" Version="8.73" BusinessUnit="abc(1236)">

   <BusinessUnitAdmin>
    ...
    ...
    </BusinessUnitAdmin>
   <BusinessUnit>
    ..
    ..
    </BusinessUnit>
    <Exercise>
    ..
    ..
     </Exercise>
     <Exercise>
     ..
     ..
     </Exercise>
     <Contact>
     ..
     </Contact>
 </IMPORT>

I am trying to export this XML to Excel, while also maintaining the schema of XML. 我正在尝试将此XML导出到Excel,同时还维护XML的架构。 For this, I have tried converting the XML to Dataset and then exporting it to Excel. 为此,我尝试将XML转换为Dataset,然后将其导出到Excel。 It works correctly if I remove the root element - IMPORT from the XML. 如果我从XML中删除根元素-IMPORT,它将正常工作。 However since it has ' BusinessUnit as attribute, its preventing to read "BusinessUnit" as element. 但是,由于它具有' BusinessUnit作为属性,因此阻止将“ BusinessUnit”作为元素读取。 For this, I am trying to read XML from second element - <BusinessUnitAdmin> ; 为此,我试图从第二个元素- <BusinessUnitAdmin>读取XML。 but unable to do. 但无能为力。

C# Code: C#代码:

DataSet ds = new DataSet();
string path = "C:\\abc\\xyz.xml";
ds.ReadXml(path);                         //throws error
ExportDataSetToExcel(ds);

Error: "A column named 'BusinessUnit' already belongs to this DataTable: cannot set a nested table name to the same name." 错误:“名为'BusinessUnit'的列已经属于此DataTable:无法将嵌套表名称设置为相同的名称。”

I have tried using LINQ: 我尝试使用LINQ:

XDocument xdoc = XDocument.Load(path);
XElement import = xdoc.Element("IMPORT");
XElement contact = import.Element("Contact");
StringReader theReader = new StringReader(contact.ToString());
ds.ReadXml(theReader);

It works correctly, but this way, I have to manually read all elements. 它可以正常工作,但是通过这种方式,我必须手动读取所有元素。

I also tried reading elements using - var allElements = xdoc.Descendants() ; 我还尝试使用var allElements = xdoc.Descendants()读取元素; but unable to store specific elements in dataset. 但无法在数据集中存储特定元素。

For me, the problem isn't that you can't export your XML data into Excel. 对我来说,问题在于您不能将XML数据导出到Excel。

The problem is actually that you can't convert your XML - which is in a tree-based hierarchical format - into a 2D array of values, ready to be exported to Excel. 问题实际上是您不能将XML(基于树的分层格式)转换为2D值数组,可以将其导出到Excel。

I mean, if your XML contained three levels of hierarchy, how would you expect Excel to display this ?! 我的意思是,如果您的XML包含三个层次结构的层次结构,您希望Excel如何显示它?

Anyway, let me answer the second part of your question. 无论如何,让我回答您问题的第二部分。

If you can get your data in a regular 2-dimensional format, either as a DataSet or DataTable , then you can use my free C# class to write it to an .xlsx file using one line of code. 如果您可以使用二维二维格式(作为DataSetDataTable ,则可以使用我的免费C#类通过一行代码将其写入.xlsx文件。

Export to Excel 导出到Excel

Now, you just need to get your data into a usable format. 现在,您只需要将数据转换为可用格式即可。

Over to you...! 交给你...!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM