C#读取Excel并转化成XML C#读取Excel并转化成XML的方法
Regina 人气:0想了解C#读取Excel并转化成XML的方法的相关内容吗,Regina在本文为您仔细讲解C#读取Excel并转化成XML的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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;
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#程序设计有所帮助。
加载全部内容