C# 将Excel转xml
发布日期:2021-06-30 19:37:44
浏览次数:3
分类:技术文章
本文共 3480 字,大约阅读时间需要 11 分钟。
花了点时间研究了下C#转xml,Excel读取后的xml格式按个人习惯写,我习惯一个Item带多个属性,而不是一个Item带一堆Item作为属性
为了批量转换,可以做一个配置
Excel2XmlCfg.xml
C#代码如下,各位看官如有更好的方法,欢迎分享哦
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Xml;using System.IO;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { ReadExcel2XmlCfgAndDoExchange(); } private static void ReadExcel2XmlCfgAndDoExchange() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("Excel2XmlCfg.xml"); if (xmlDoc == null) { Console.WriteLine("Read Excel2XmlCfg Failed!"); return; } XmlNodeList resList = xmlDoc.GetElementsByTagName("item"); for (int i = 0; i < resList.Count; ++i) { XmlNode node = resList.Item(i); string ExcelPath = node.Attributes["excelPath"].Value; string XmlPath = node.Attributes["xmlPath"].Value; XmlDocument outXmlDoc = Excel2Xml(ExcelPath); CreateXml(outXmlDoc, XmlPath); } } private static void CreateXml(XmlDocument xmlDoc,string xmlName) { XmlWriterSettings ws = new XmlWriterSettings(); ws.NewLineHandling = NewLineHandling.Entitize; ws.Encoding = System.Text.Encoding.UTF8; ws.Indent = true; XmlWriter writer = XmlWriter.Create(xmlName, ws); writer.WriteStartElement("root"); XmlNodeList resList = xmlDoc.GetElementsByTagName("item"); for (int i = 0; i < resList.Count; ++i) { XmlNode node = resList.Item(i); //写入子节点// writer.WriteStartElement("item"); System.Collections.IEnumerator erator = node.GetEnumerator(); while (erator.MoveNext()) { //写入属性// writer.WriteAttributeString(((XmlNode)erator.Current).Name, ((XmlNode)erator.Current).InnerText); } writer.WriteEndElement(); } writer.Close(); } private static XmlDocument Excel2Xml(string excelFilePath) { XmlDocument excelData = new XmlDocument(); DataSet excelTableDataSet = new DataSet(); StreamReader excelContent = new StreamReader(excelFilePath, System.Text.Encoding.Default); string stringConnectToExcelFile = string.Format("provider=Microsoft.Jet.OLEDB.4.0;data source={0};Extended Properties=Excel 8.0;", excelFilePath); System.Data.OleDb.OleDbConnection oleConnectionToExcelFile = new System.Data.OleDb.OleDbConnection(stringConnectToExcelFile); System.Data.OleDb.OleDbDataAdapter oleDataAdapterForGetExcelTable = new System.Data.OleDb.OleDbDataAdapter( string.Format("select * from [Sheet1$]"), oleConnectionToExcelFile); try { oleDataAdapterForGetExcelTable.Fill(excelTableDataSet,"item"); } catch (System.Exception ex) { Console.WriteLine("error:" + ex.Message); } string excelOutputXml = "tmp.xml"; excelTableDataSet.WriteXml(excelOutputXml); excelData.Load(excelOutputXml); File.Delete(excelOutputXml); return excelData; } }}
转载地址:https://linxinfa.blog.csdn.net/article/details/46315809 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月20日 05时51分51秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
优化算法(四)——粒子群优化算法(PSO)
2019-04-30
数据在Oracle中的存储
2019-04-30
轨迹规划 trajectory planning
2019-04-30
AGV自动导引运输车
2019-04-30
Trie树(字典树)
2019-04-30
COMP7404 Machine Learing——KNN
2019-04-30
COMP7404 Machine Learing——SVM
2019-04-30
COMP7404 Machine Learing——ROC
2019-04-30
Python量子计算qiskit
2019-04-30
Python的多线程不是真的多线程(GIL全局解释器锁)
2019-04-30
Python手动读取MNIST数据集
2019-04-30
Python手动读取CIFAR-10数据集
2019-04-30
Pytorch(十一) —— 分布式(多GPU)训练
2019-04-30