static XmlDocument ExcelToXml(string excelFilePath)
{
XmlDocument excelData = new XmlDocument();
DataSet excelTableDataSet = new DataSet();
StreamReader excelContent = new StreamReader(excelFilePath, System.Text.Encoding.Default);
FileInfo file = new FileInfo(excelFilePath);
string str = file.Extension;
string stringConnectToExcelFile = "";
switch (str)
{
case ".xls":
stringConnectToExcelFile = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFilePath + ";" + "Extended Properties=Excel 8.0;";
break;
case ".xlsx":
stringConnectToExcelFile = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath + ";Extended Properties=Excel 12.0;";
break;
default:
break;
}
System.Data.OleDb.OleDbConnection oleConnectionToExcelFile = new System.Data.OleDb.OleDbConnection(stringConnectToExcelFile);
System.Data.OleDb.OleDbDataAdapter oleDataAdapterForGetExcelTable = new System.Data.OleDb.OleDbDataAdapter(string.Format("select * from [Sheet1$]"), oleConnectionToExcelFile);
try
{
oleDataAdapterForGetExcelTable.Fill(excelTableDataSet);
}
catch
{
return null;
}
// string excelOutputXml = Path.GetTempFileName();
string excelOutputXml = "F:/test01.xml";
excelTableDataSet.WriteXml(excelOutputXml);
Console.WriteLine("装换成功");
//excelData.Load(excelOutputXml);
//File.Delete(excelOutputXml);
return excelData;
}