C#读取Excel并转化成XML的方法
程序员文章站
2022-06-21 09:41:01
本文实例讲述了c#读取excel并转化成xml的方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:xmldocument exceldata = new...
本文实例讲述了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#程序设计有所帮助。