这是读取XML数据写到XLS里的,
Excel.Application excel = new Excel.Application();
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
excel.AlertBeforeOverwriting = false;
DataSet ds = new DataSet();
ds.ReadXml("test.xml");
System.Data.DataTable table = ds.Tables[0];
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
} foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
excel.Visible = false;
excel.ActiveWorkbook.SaveAs(System.Windows.Forms.Application.StartupPath+"\\test.XLS", Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, true);
excel.Quit();
excel = null;
<table>
<info 字段1="aa" 字段2="bb" 字段3="cc" />
</table>
DataSet dt = new DataSet();
dt.ReadXml("t.xml");
string s1 = dt.Tables["info"].Rows[0]["字段1"].ToString();
string s2 = dt.Tables["info"].Rows[0]["字段2"].ToString();
string s3 = dt.Tables["info"].Rows[0]["字段3"].ToString();
xml:
<a sheet_name="sheet1">
<column old="xx" new="xx">
........然后再读入,对比xml的attrib来实现列的对齐插入,可能我理解错你的意思了,如果是导数据,我真心认为,很多不同EXCEL就自己动手多种几个相同的窗体,里面就列对比自己手写一下还方便得多。
这个不是自己用,是让用户用的,页面太多不太好,还是有局限性,如果以后人家改动数据库或excel,就要不成了
DataSet ds = new DataSet();
ds.ReadXml("test.xml");
string s1 = ds.Tables["info"].Rows[0]["字段1"].ToString();
string s2 = ds.Tables["info"].Rows[0]["字段2"].ToString();
string s3 = ds.Tables["info"].Rows[0]["字段3"].ToString();
//读取XLS的数据,写到ds中
string namefile = System.Windows.Forms.Application.StartupPath + "\\test.XLS";
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + namefile + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
strExcel = "select * from [sheet1$]";
OleDbDataAdapter oadapter = new OleDbDataAdapter(strExcel, strConn);
DataSet ds0 = new DataSet();
oadapter.Fill(ds0, "table1"); //根据XML中的取的字段对应读取ds中的数据
string name = ds0.Tables[0].Rows[0][s1].ToString();
string no = ds0.Tables[0].Rows[0][s2].ToString();
string old = ds0.Tables[0].Rows[0][s3].ToString(); //写入到数据库中
string connstring = "server=.;uid=sa;password=sa;database=test";
SqlConnection con = new SqlConnection(connstring);
con.Open();
string sql = "insert into student values('" + name + "','" + no + "','" + old + "')";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
你这样感觉很烦的
Excel本身就支持 ADODB。可以写上几十行vba代码直接从 Excel 工作簿中读写关系数据库,也可以用 Excel 现成的功能(不用写代码)与关系数据库表进行简单同步。
这跟“xml”这个词儿的噱头没有关系。看到你纠结这个词儿,我不知道你可能会做什么,但是我知道你不会去做什么了。要让用户简单地“描述”数据采集模板,需要研究用户的行为,研究 Excel 对象技术,研究其它有用的东西。而不是什么 xml。