C# 将Excel转xml
发布日期:2021-06-30 19:37:44
浏览次数:2
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年05月04日 16时01分36秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
PostgreSQL中 drop table指令出现ERROR: cannot drop table userinfo because other objects depend on it
2019-04-30
YModem协议理解
2019-04-30
Java中,jsp文件编译之后部署显示中文乱码
2019-04-30
PostgreSQL之通过数据库模型生成数据库表
2019-04-30
Mac电脑终端下查看电脑信息的相关指令
2019-04-30
使用lshw工具查看linux的系统硬件信息
2019-04-30
Mac NTFS 解决方法
2019-04-30
Eclipse安装tomcat插件
2019-04-30
SiteMesh相关(一)
2019-04-30
Mac终端启动tomcat的相关问题
2019-04-30
Eclipse卸载插件SpringSoource-tool-suite
2019-04-30
Mac上Eclipse安装插件SpringSoource-tool-suite
2019-04-30
比特币重生计划:Metanet 技术实现
2019-04-30
Metanet 开发者指南
2019-04-30
比特币重生计划:万链归一
2019-04-30
淘宝新手卖家请警惕合并订单骗局
2019-04-30
你问我,为什么还有人做SEO?
2019-04-30
SEOer转型SEM,该如何转型,给SEOer的四个建议
2019-04-30