Skip to content

Instantly share code, notes, and snippets.

@Microsofttechies
Created April 2, 2015 23:42
Show Gist options
  • Select an option

  • Save Microsofttechies/3b66b6e0007f3bf75ca8 to your computer and use it in GitHub Desktop.

Select an option

Save Microsofttechies/3b66b6e0007f3bf75ca8 to your computer and use it in GitHub Desktop.
c# convert excel to xml
OleDbConnection objConnection;
DataSet objDs;
OleDbDataAdapter objCommand;
string strExcelPath = "C:\\MyExcel.xlsm";
if (Path.GetExtension(strExcelPath) == ".xls")
{
objConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strExcelPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
}
else if (Path.GetExtension(strExcelPath) == ".xlsx")
{
objConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strExcelPath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
}
else if (Path.GetExtension(strExcelPath) == ".xlsm")
{
objConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strExcelPath + ";Extended Properties='Excel 12.0 Macro;IMEX=1;';");
}
objCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", objConnection);
objCommand.TableMappings.Add("Table", "Product");
objDs = new System.Data.DataSet();
objCommand.Fill(objDs);
objConnection.Close();
objDs.WriteXml("Product.xml");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment