我编写了一个快速代码,尝试从XML文件中获取元素并将其放在excel表中,但我的节点没有出现任何异常。我不明白为什么。
我一直在尝试使用Microsoft的XMLDocument文档从节点中获取所需内容的不同方法,但是结果始终是相同的,它使我空了一行。
[STAThread]
static void Main(string[] args)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
XmlDocument xmlDocument = new XmlDocument();
System.Data.DataTable table = new System.Data.DataTable();
Microsoft.Office.Interop.Excel.Application excel;
Microsoft.Office.Interop.Excel.Workbook workBook;
Microsoft.Office.Interop.Excel.Worksheet workSheet;
int rowCount = 1;
openFileDialog.Filter = "XML Files|*.xml";
openFileDialog.Title = "Select the SMS backup";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
xmlDocument.PreserveWhitespace = true;
try
{
xmlDocument.Load(openFileDialog.FileName);
}
catch (System.IO.FileNotFoundException)
{
}
table.Columns.Add("Contact name", typeof(string));
table.Columns.Add("Date", typeof(DataFormats));
table.Columns.Add("Message", typeof(string));
foreach (XmlNode node in xmlDocument.DocumentElement.ChildNodes)
{
System.Diagnostics.Debug.WriteLine(node.InnerText);
if (node.InnerText.Contains("contact_name"))
{
table.Rows.Add(
node.Attributes["contact_name"]?.InnerText,
node.Attributes["readable_date"]?.InnerText,
node.Attributes["body"]?.InnerText
);
}
}
try
{
excel = new Microsoft.Office.Interop.Excel.Application
{
Visible = true,
DisplayAlerts = false
};
workBook = excel.Workbooks.Add(Type.Missing);
workSheet = (Microsoft.Office.Interop.Excel.Worksheet) workBook.ActiveSheet;
workSheet.Name = "SMS backup";
foreach (System.Data.DataRow dataRow in table.Rows)
{
rowCount += 1;
for (int i = 1; i <= table.Columns.Count; i++)
{
workSheet.Cells[rowCount, i] = dataRow[i - 1].ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
workSheet = null;
workBook = null;
}
}
}
答案 0 :(得分:1)
在我的评论之后,这里有一些更深入的建议。我是在手机上写的,请原谅这不是完整的工作代码
在解决方案资源管理器中右键单击您的解决方案节点,然后选择“管理解决方案的Nuget程序包”
安装epplus-它可以直接创建excel文件(实际上只是一个名为xlsx的zip文件中的xml文件),而无需安装excel互操作可能会给您带来痛苦,并且最好避免
将xml文档读入数据集:
DataSet da = new DataSet();
da.ReadXml(path to your file);
在readxml行上放置一个断点,然后启动应用程序。跨过readxml,然后指向ds
,然后单击工具提示中显示的放大镜
熟悉xml文档在DataSet中的显示方式-具有子节点的节点往往被表示为具有将它们链接在一起的关系的分离表,因此对于像xml这样的xml,您可能最终会结束
xml
parentnode1
childnode1
parentnode2
childnode2
/xml
您将有两个表,分别称为parentnode和childnode,并在如下的循环集中读取它们:
foreach(var ro in da.Tables["parentnode"].Rows)
//do stuff with parent rows
//iterate child rows
foreach(var cro in ro.GetChildRows()){
//do stuff with child rows
}
}
目前看来您的xml并非采用这种结构,但是很难分辨
因此,您现在将xml文档作为DataSet中的一组表,并且希望将它们作为excel工作表。 Epplus可以通过几行代码从数据表(数据集是数据表的集合)创建excel文件,请参见Export DataTable to excel with EPPlus
如果您的数据集仅包含1个表,则工作已结束。如果它有多个表,我建议您为自己创建一个新的DataTable,然后从各种ds.Tables [“ table name here”]]中循环选择DataSet的值,并填充您的DataTable
因此,要实现您的程序,请将这组注释作为代码实现
//read xml file to DataSet - 2 lines of code
//EITHER xml is flat structure, results in one datatable in the set
//so just remove or rename columns as required - few lines
//OR DataSet has 3 tables, make a new datatable and loop over the 3,
//populating the new one, to flatten it - few lines
//use epplus to turn the single datatable to an excel sheet - 4 lines
问题已解决,希望可以包含大约10行代码