C#读取Excel并转化成XML的方法
本文实例讲述了C#读取Excel并转化成Xml的方法。分享给大家供大家参考。具体实现方法如下:
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 = new System.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);
excelData.Load(excelOutputXml);
File.Delete(excelOutputXml);
return excelData;
希望本文所述对大家的C#程序设计有所帮助。