C#讀取Excel並轉化成XML的辦法。本站提示廣大學習愛好者:(C#讀取Excel並轉化成XML的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是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#法式設計有所贊助。