你可以先把Excel中的数据读取到DataSet中,然后DataSet直接Save为Xml文件即可。
DataSet ds = new DataSet();
OleDbCommand excelCommand = new OleDbCommand();
OleDbDataAdapter excelDataAdapter = new OleDbDataAdapter();
string excelConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";
OleDbConnection excelConn = new OleDbConnection(excelConnStr);
excelConn.Open();
DataTable dtPatterns = new DataTable();
excelCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", excelConn);
excelDataAdapter.SelectCommand = excelCommand;
excelDataAdapter.Fill(ds, "Customers");
// Get a StreamWriter object
StreamWriter xmlDoc = new StreamWriter("Customers.xml");
// Apply the WriteXml method to write an XML document
ds.WriteXml(xmlDoc);
xmlDoc.Close();
用pageoffice吧,很好用的。