希望这个方法对你有用
///
///根据excel路径和sheet名称,返回excel的DataSet
///
public static void GetExcelDataToXml(string path, string tname)
{
/*Office 2007*/
string ace = "Microsoft.ACE.OLEDB.12.0";
/*Office 97 - 2003*/
string jet = "Microsoft.Jet.OLEDB.4.0";
string xl2007 = "Excel 12.0 Xml";
string xl2003 = "Excel 8.0";
string imex = "IMEX=1";
/* csv */
string text = "text";
string fmt = "FMT=Delimited";
string hdr = "Yes";
string conn = "Provider={0};Data Source={1};Extended Properties=\"{2};HDR={3};{4}\";";
string select = string.Format("SELECT * FROM [{0}$]", tname);
//string select = sql;
string ext = Path.GetExtension(path);
OleDbDataAdapter oda;
DataSet ds = newDataSet();
switch (ext.ToLower())
{
case".xlsx":
conn = String.Format(conn, ace, Path.GetFullPath(path), xl2007, hdr, imex);
break;
case".xls":
conn = String.Format(conn, jet, Path.GetFullPath(path), xl2003, hdr, imex);
break;
case".csv":
conn = String.Format(conn, jet, Path.GetDirectoryName(path), text, hdr, fmt);
//sheet = Path.GetFileName(path);
break;
default:
thrownewException("File Not Supported!");
}
OleDbConnection con = newOleDbConnection(conn);
con.Open();
//select = string.Format(select, sql);
oda = newOleDbDataAdapter(select, con);
oda.Fill(ds,"test");
ds.WriteXml("c:/verify.xml");
con.Close();
}
Excel载入到DataSet
DataSet转到XML文件