C#读取Excel并转化成XML的方法
内容摘要
本文实例讲述了C#读取Excel并转化成Xml的方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:XmlDocument excelData = new XmlDocument();
DataSet excelTa
复制代码 代码如下:XmlDocument excelData = new XmlDocument();
DataSet excelTa
文章正文
本文实例讲述了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#程序设计有所帮助。
代码注释