C#读取Excel并转化成XML的方法
时间:2021-01-23 10:15:11|栏目:.NET代码|点击: 次
本文实例讲述了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#程序设计有所帮助。
上一篇:c#注册表操作类分享
栏 目:.NET代码
下一篇:asp.net datalist绑定数据后可以上移下移实现示例
本文标题:C#读取Excel并转化成XML的方法
本文地址:http://www.codeinn.net/misctech/49784.html