Using System;
Using System.Collections.Generic;
Using System.Data;
Using System.Data.OleDb;
Using System.Linq;
Using System.Text;
Using Log;
Namespace Common
{
public class Excelinputhelper
{
public static Excelinputhelper self = new Excelinputhelper ();
public static DataSet Execledataset (String filePath)
{
String OleDbConnection = "provider=microsoft.jet.oledb.4.0;" + "Data source=" + FilePath + "; Extended properties= ' Excel 8.0; Hdr=yes; Imex=1 ' ";
OleDbConnection conn = new OleDbConnection (OleDbConnection);
Try
{
Conn. Open ();
}
catch (Exception ee)
{
if (EE. Message.contains ("External table is not the expected format")
{
OleDbConnection = "provider=microsoft.ace.oledb.12.0;" + "Data source=" + FilePath + "; Extended properties= ' Excel 12.0; Hdr=yes; Imex=1 ' ";
Conn. ConnectionString = OleDbConnection;
Conn. Open ();
}
Else
{
Log.Log4NetUtility.Error (Self, "Unable to open file");
return null;
}
}
DataSet ds = new DataSet ();
OleDbDataAdapter Odda = new OleDbDataAdapter ("SELECT * from [sheet1$]", conn);
Odda. Fill (DS);
Conn. Close ();
return DS;
}
}
}
Parse Excel table as DataSet