📄 converter.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using LC_XMLControl;
using System.Data.OleDb;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace XmlToExcel
{
public class Converter : IThransProtocol
{
#region IThransProtocol 成员
public void closeDesConnection()
{
}
public void closeSourceConnection()
{
}
public void createDesContentction(string constr)
{
try
{
string cstr = "Provider=Microsoft.Jet." + "OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + constr;
_desconnection = new OleDbConnection(cstr);
_desconnection.Open();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
}
public void createSourceContenction(string constr)
{
}
private int _cursum;
public int cursum
{
get
{
return _cursum;
}
set
{
_cursum = value;
}
}
private OleDbConnection _desconnection;
public object desconnection
{
get
{
return _desconnection;
}
set
{
_desconnection = (OleDbConnection)value;
}
}
private int _dtype;
public int dtype
{
get
{
return _dtype;
}
set
{
_dtype = value;
}
}
public System.Collections.ArrayList getAllDesField(string table)
{
ArrayList altemp = new ArrayList();
DataTable dt = new DataTable();
dt = _desconnection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new Object[] { null, null, table, null });
for (int i = 0; i < dt.Rows.Count; i++)
{
altemp.Add(dt.Rows[i].ItemArray[3].ToString());
}
return altemp;
}
public System.Collections.ArrayList getAllDesTable()
{
ArrayList altemp = new ArrayList();
DataTable dt = new DataTable();
dt = _desconnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" });
for (int i = 0; i < dt.Rows.Count; i++)
{
altemp.Add(dt.Rows[i].ItemArray[2].ToString());
}
return altemp;
}
public System.Collections.ArrayList getAllSourceField(string table)
{
return null;
}
public System.Collections.ArrayList getAllSourceTable()
{
return null;
}
private string _info;
public string info
{
get
{
return _info;
}
set
{
_info = value;
}
}
private ArrayList _msg;
public System.Collections.ArrayList msg
{
get
{
return _msg;
}
set
{
_msg = (ArrayList)_msg;
}
}
public System.Data.DataTable sourceToXml(string source, System.Collections.ArrayList allfield)
{
return null;
}
private object _sourceconnection;
public object sourceconnection
{
get
{
return _sourceconnection;
}
set
{
_sourceconnection = value;
}
}
private string _spliter;
public string spliter
{
get
{
return _spliter;
}
set
{
_spliter = value;
}
}
private int _stype;
public int stype
{
get
{
return _stype;
}
set
{
_stype = value;
}
}
private ArrayList _allres = new ArrayList();
public ArrayList allres
{
get
{
return _allres;
}
set
{
_allres = (ArrayList)value;
}
}
private ArrayList _errorres = new ArrayList();
public ArrayList errorres
{
get
{
return _errorres;
}
set
{
_errorres = (ArrayList)value;
}
}
public void xmlToDes(string des, System.Collections.ArrayList rows, System.Collections.ArrayList field)
{
_allres.Clear();
_errorres.Clear();
OleDbCommand ocmd = new OleDbCommand();
ocmd.Connection = _desconnection;
//[1] = "type=数字■id=1133|type=字符串■pwd=10124160|type=字符串■username=Hewitt|"
string temp;
string[] fields;
string str = "";
string f = " ";
f = " (";
for (int i = 0; i < field.Count; i++)
{
f += field[i].ToString() + " , ";
}
f = f.Substring(0, f.Length - 2);
f += ") ";
for (int i = 0; i < rows.Count; i++)
{
str = "insert into [" + des+"]" + f + "values(";
temp = rows[i].ToString();
fields = temp.Split('|');
//
for (int k = 0; k < fields.Length; k++)
{
if (fields[k].Trim() != "")
{
str += getFieldValue(fields[k]) + ",";
}
}
str = str.Substring(0, str.Length - 1);
str += ")";
try
{
_cursum = i + 1;
ocmd.CommandText = str;
ocmd.ExecuteNonQuery();
info = _cursum + " " + DateTime.Now.ToString() + " : " + str + "被成功执行!";
_allres.Add(info);
Thread.Sleep(100);
}
catch (Exception ee)
{
info = _cursum + " " + DateTime.Now.ToString() + " : " + "在执行" + str + "时候发生了 " + ee.Message.ToString();
_allres.Add(info);
_errorres.Add(info);
continue;
}
}
}
private string getFieldValue(string str)
{
string type = str.Substring(str.IndexOf('=') + 1, str.IndexOf('■') - 5);
string value = str.Substring(str.LastIndexOf('=') + 1);
if (type == "字符串")
return "'" + value + "'";
return value;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -