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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Unity3D log写入文件
下一篇:Unity3D 动态创建图集并压入精灵(NGUI)

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月20日 05时51分51秒