Add this
//Set Web Config File
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
public DataSet ExtractDataFromExcelFile(string strPathExcelFile)
{
OleDbConnection MyConnection = null;
string ConStringOLEDB1 = "";
string ConStringOLEDB2 = "";
ConStringOLEDB1 = System.Configuration.ConfigurationManager.AppSettings["ConStringOLEDB1"];
ConStringOLEDB2 = System.Configuration.ConfigurationManager.AppSettings["ConStringOLEDB2"];
DataSet objDS = new DataSet();
object missing = System.Reflection.Missing.Value;
try
{
MyConnection = new OleDbConnection(ConStringOLEDB1 + strPathExcelFile + ConStringOLEDB2);
MyConnection.Open();
DataTable objDT = new DataTable();
objDT = MyConnection.GetSchema("Tables");
DataRow dr = objDT.Rows[0];
string strWorksheetName = dr["TABLE_NAME"].ToString();
System.Data.OleDb.OleDbDataAdapter objDataAdpter = null; ;
objDataAdpter = new System.Data.OleDb.OleDbDataAdapter("select * from [" + strWorksheetName + "]", MyConnection);
objDataAdpter.Fill(objDS);
MyConnection.Close();
}
catch (Exception Ex)
{
MyConnection.Close();
}
return objDS;
}
//Set Web Config File
<add key="ConStringOLEDB1" value="provider=Microsoft.Jet.OLEDB.4.0; data source='"/>
<add key="ConStringOLEDB2" value="';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"/>
<add key="ConStringOLEDB1_2003" value="Provider=Microsoft.ACE.OLEDB.12.0; data source= '"/>
<add key="ConStringOLEDB2_2003" value="';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"/>
No comments:
Post a Comment