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) 编辑 收藏 引用