C#读取Excel并转化成Xml
来源:互联网 发布:MySQL 设置多个主键 编辑:程序博客网 时间:2024/06/05 04:35
XmlDocument excelData = new XmlDocument();
DataSet excelTableDataSet = new DataSet();
StreamReader excelContent = new StreamReader(excelFilePath,System.Text.Encoding.Default);
string stringConnectToExcelFile =string.Format("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +excelFilePath + ";Extended Properties="Excel 12.0;"");
System.Data.OleDb.OleDbConnection oleConnectionToExcelFile = newSystem.Data.OleDb.OleDbConnection(stringConnectToExcelFile);
System.Data.OleDb.OleDbDataAdapter oleDataAdapterForGetExcelTable =new System.Data.OleDb.OleDbDataAdapter(string.Format("select * from[Sheet 1$]"), oleConnectionToExcelFile);
try
{
oleDataAdapterForGetExcelTable.Fill(excelTableDataSet);
}
catch
{
return null;
}
string excelOutputXml = Path.GetTempFileName();
excelTableDataSet.WriteXml(excelOutputXml);