Created
April 2, 2015 23:42
-
-
Save Microsofttechies/3b66b6e0007f3bf75ca8 to your computer and use it in GitHub Desktop.
c# convert excel to xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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