This file contains 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"); |