如何从Excel创建XML文件?

时间:2014-06-25 11:34:24

标签: c# asp.net xml excel

我的EXCEL文件格式

Name      Gender   DateofBirth  HomeZipcode   CoverageTier
----------------------------------------------------------
Test1        F      1966-07-21    78201        Fam
Test2        M      1966-07-21    78201        E

使用以下代码转换

  System.Data.OleDb.OleDbConnection MyConnection;
  System.Data.DataSet ds;
  System.Data.OleDb.OleDbDataAdapter MyCommand;
  MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:/Users/guru/Desktop/CensusData2013.xlsx';Extended Properties=Excel 12.0;");
  MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Test$]", MyConnection);
  MyCommand.TableMappings.Add("Table", "Member");
  ds = new System.Data.DataSet("Members");       
  MyCommand.Fill(ds);
  MyConnection.Close();
  string path = Server.MapPath("CensusData.xml");
  ds.WriteXml(path);

我当前的XML输出格式

 <?xml version="1.0" standalone="true"?>
 <Members> 
    <Member> 
       <Name>Test1</Name>
       <Gender>F</Gender> 
       <DateofBirth>1966-07-21T00:00:00+05:30</DateofBirth>
       <HomeZipcode>78201</HomeZipcode>
       <CoverageTier>Fam</CoverageTier>
     </Member>
     <Member> 
       <Name>Test2</Name>
       <Gender>M</Gender> 
       <DateofBirth>1966-07-21T00:00:00+05:30</DateofBirth>
       <HomeZipcode>78202</HomeZipcode>
       <CoverageTier>E</CoverageTier>
     </Member>
 </Members>

我需要添加excel文件

   Location 
   LX

对所有用户来说都很常见,例如Test1,Test2

现在我需要在xml中获得以下结果

  <?xml version="1.0" standalone="true"?>
 <Members> 
    <CompanyLocation>
     <Location>LX</Location>
    </CompanyLocation>

    <Member> 
       <Name>Test1</Name>
       <Gender>F</Gender> 
       <DateofBirth>1966-07-21T00:00:00+05:30</DateofBirth>
       <HomeZipcode>78201</HomeZipcode>
       <CoverageTier>Fam</CoverageTier>
    </Member>

    <Member> 
       <Name>Test2</Name>
       <Gender>M</Gender> 
       <DateofBirth>1966-07-21T00:00:00+05:30</DateofBirth>
       <HomeZipcode>78202</HomeZipcode>
       <CoverageTier>E</CoverageTier>
     </Member>
 </Members>  

如何获得如上所述的xml输出。

0 个答案:

没有答案