如何使用c#将包含父节点和子节点的xml转换为单个excel文件 [英] How do I convert xml which contain parent and child node into single excel file using c#

查看:69
本文介绍了如何使用c#将包含父节点和子节点的xml转换为单个excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的xml文档包含这种情况:

My xml document contain this kind of scenario:

<Employee>
 <Name>
  <FirstName>George</FirstName>
  <LastName> Desh </LastName>
 </Name>
 <Address>
   <State>
     <city>Pune</city>
     <Dist>Pune</Dist>
   </State>
 </Address>
</Employee>





那么我的excel将如何查找这个嵌套的xml元素。我们如何使用C#将此xml转换为excel?



So how my excel will look for this nested xml elements. And how we can convert this xml into excel using C#?

推荐答案

请使用以下代码生成基于xml的excel文件。





public void XMLToExcel(string strXMLPath,string outputFileName,string sheetName)

{

string strPath = Path .GetDirectoryName(strXMLPath);

DataSet dsXML = new DataSet();

dsXML.ReadXml(strXMLPath);

strPath = strPath + \\+ outputFileName +。xlsx;

DataTable tblXML = dsXML.Tables [0];

tblXML.TableName = sheetName;

DataTableToFilePath(tblXML,strPath);

}
Please use the following code generating excel file based on the xml.


public void XMLToExcel(string strXMLPath, string outputFileName, string sheetName)
{
string strPath = Path.GetDirectoryName(strXMLPath);
DataSet dsXML = new DataSet();
dsXML.ReadXml(strXMLPath);
strPath = strPath + "\\" + outputFileName + ".xlsx";
DataTable tblXML = dsXML.Tables[0];
tblXML.TableName = sheetName;
DataTableToFilePath(tblXML, strPath);
}


这篇关于如何使用c#将包含父节点和子节点的xml转换为单个excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆