posts - 124,  comments - 29,  trackbacks - 0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;


namespace ExcelToXml
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string xmlFile = "";
        private DataSet ds = null;
        private string excelFileName = "";
        private void ReadXMLGetFileName()
        {
            XmlDocument xdoc = new XmlDocument();
            string file = Application.StartupPath + "//excelFile.xml";
            xdoc.Load(file);
            XmlNode xno = xdoc.SelectSingleNode("excelFile");
            for (int i = 0; i < xno.ChildNodes.Count; i++)
            {
                if (xno.ChildNodes[i].Name == "FileName")
                {
                    excelFileName = xno.ChildNodes[i].InnerText;
                    break;
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            xmlFile = Application.StartupPath + "\\CC08Help.xml";
            ReadXMLGetFileName();
            ds = ReadExcelToTree.ExcelToDataSet(@excelFileName);
            if (ds != null)
            {
                xmlFormExcel(ds.Tables[0], xmlFile);
            }
        }
        private XmlNode curNode = null;
        private string curNodeName = "";
        private XmlDocument xdoc = null;
        void xmlFormExcel(DataTable dt,string xmlFile)
        {
           
            xdoc = new XmlDocument();
            xdoc.Load(xmlFile);
            curNodeName = "Help";
            curNode = xdoc.SelectSingleNode(curNodeName);
           
            int colums = dt.Columns.Count;
            XmlNode[] curNodeLevel = new XmlNode[colums];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                for (int j = 1; j <= colums; j++)
                {
                    if (j == 1 && dr[j].ToString() != String.Empty)
                    {
                        XmlElement xe = xdoc.CreateElement("tn");
                        xe.SetAttribute("value",dr[j].ToString());
                        curNode.AppendChild(xe);
                        curNodeLevel[0] = xe;
                        break;
                    }
                    else if (j >= 2)
                    {
                        if (dr[j].ToString() != string.Empty)
                        {
                            XmlElement xe = xdoc.CreateElement("tn");
                            xe.SetAttribute("value", dr[j].ToString());
                         
                            curNodeLevel[j - 2].AppendChild(xe);
                            curNodeLevel[j - 1] = xe;
                            break;
                        }
                    }
                }
            }
            xdoc.Save(xmlFile);
        }
    }
}

posted on 2009-02-20 15:16 天书 阅读(1434) 评论(1)  编辑 收藏 引用

FeedBack:
# re: Excel 文件导出成 XML文件(树结构)
2011-04-30 22:07 | 姚舜理
好  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2009年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

常用链接

留言簿(5)

随笔档案

文章分类

文章档案

好友的Bolg

搜索

  •  

最新评论

阅读排行榜

评论排行榜