Excel转换成XML文件(根据不同需求配制语句),本程序的特点是可以指定Excel的任一子表。(穷鬼求分,勿喷...)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.OleDb;
using System.IO;
using System.Diagnostics;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsApplication1
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
Microsoft.Office.Interop.Excel.Application obj;
Microsoft.Office.Interop.Excel.Workbook objWB;
private void buttonTransfer_Click(object sender, EventArgs e)
{
if (textBoxExcel.Text == string.Empty)
{
MessageBox.Show("还没指定Excel文件!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
textBoxExcel.Focus();
buttonOpenExcel.Focus();
}
else if (textBoxXml.Text == string.Empty)
{
MessageBox.Show("还没指定Xml文件保存路径!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
buttonSaveXml.Focus();
}
else if (comboBoxSheetName.SelectedIndex < -1 || comboBoxSheetName.Enabled == false)
{
MessageBox.Show("还没指定Excel中的Sheet!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
comboBoxSheetName.Focus();
}
else
{
Stopwatch sw = new Stopwatch();//计时器
sw.Start();
Transfer();
sw.Stop();
labelMsg.Text = "Xml生成成功,用时" + sw.ElapsedMilliseconds.ToString() + "ms。";
labelMsg.Visible = true;
}
}
private void buttonOpenExcel_Click(object sender, EventArgs e)
{
if (openFileDialogExcel.ShowDialog() == DialogResult.OK)
{
textBoxExcel.Text = openFileDialogExcel.FileName;
GetSheetName();
}
}
private void buttonSaveXml_Click(object sender, EventArgs e)
{
if (saveFileDialogXml.ShowDialog() == DialogResult.OK)
{
textBoxXml.Text = saveFileDialogXml.FileName;
}
}
private bool OpenExcel(ref OleDbConnection cn)
{
try
{
cn.Open();
return true;
}
catch
{
MessageBox.Show("打开Excel文件失败。", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);
return false;
}
}
private void Transfer()
{
OleDbConnection cn;
OleDbCommand cm = new OleDbCommand();
OleDbDataReader dr;
StreamWriter sw;
string strSnum = "";//用于记录Snum,以便确认是否生成新的
bool isFristLine = true;//是否是每一个插入的snum元素
cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBoxExcel.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\";");
if (OpenExcel(ref cn))
{
cm.Connection = cn;
cm.CommandType = CommandType.Text;
cm.CommandText = "select * from [" + comboBoxSheetName.Text + "$]";
try
{
dr = cm.ExecuteReader();
}
catch
{
MessageBox.Show("读取Excel数据失败!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
sw = File.CreateText(textBoxXml.Text);
//根据不同的需求而不同
sw.WriteLine("
if (dr.FieldCount > 2)
{
while (dr.Read())
{
if (strSnum != dr[0].ToString())
{
strSnum = dr[0].ToString();
if (isFristLine)
{
isFristLine = false;
}
else
{
sw.WriteLine("\t");
sw.WriteLine("\t
}
sw.WriteLine("\t\t
sw.WriteLine("\t\t
}
else
{
sw.WriteLine("\t\t
}
}
}
sw.WriteLine("\t
dr.Close();
cn.Close();
cn.Dispose();
sw.Close();
}
}
private void textBoxExcel_Leave(object sender, EventArgs e)
{
if (!System.IO.File.Exists(textBoxExcel.Text))
{
//MessageBox.Show("该Excel文件不存在,请路径是否正确。", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//textBoxExcel.SelectAll();
//textBoxExcel.Focus();
comboBoxSheetName.Enabled = false;
}
else
{
GetSheetName();
comboBoxSheetName.Enabled = true;
}
}
private void textBoxXml_Leave(object sender, EventArgs e)
{
}
private void GetSheetName()
{
///////////////////////////////////////////////////////////////////////////
/////获取Excel的sheet名与顺序
/////
obj = default(Microsoft.Office.Interop.Excel.Application);
objWB = default(Microsoft.Office.Interop.Excel.Workbook);
try
{
obj = (Microsoft.Office.Interop.Excel.Application)Microsoft.VisualBasic.Interaction.CreateObject("Excel.Application", string.Empty);
objWB = obj.Workbooks.Open(textBoxExcel.Text, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
comboBoxSheetName.Items.Clear();
for (int i = 0; i < objWB.Worksheets.Count; i++)
{
comboBoxSheetName.Items.Add(((Microsoft.Office.Interop.Excel.Worksheet)objWB.Worksheets[i + 1]).Name);
}
comboBoxSheetName.SelectedIndex = 0;
comboBoxSheetName.Enabled = true;
objWB.Close(Type.Missing, Type.Missing, Type.Missing);
objWB = null;
obj.Quit();
obj = null;
}
catch
{
MessageBox.Show("获取Excel的Shee失败!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (obj != null)
{
obj.Quit();
obj = null;
}
}
}
}
1.主界面:
2.载入Excel文件
3.Excel数据例子
4.XML结果
(穷鬼求分,勿喷...)