Used spread gear DLL
DB must have following column ..I will add later
public void readExcel(String FilePath, String SheetName)
{
string row = "";
string column = "";
string transectionocurs = "";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlTransaction myTrans = con.BeginTransaction();
SqlCommand cmd1 = new SqlCommand("sp_InserXSLData", con);
cmd1.Transaction = myTrans;
try
{
SpreadsheetGear.IWorkbook oWB;
SpreadsheetGear.IWorksheet OSHEET;
SpreadsheetGear.IRange oRng;
// DataTable ds = new DataTable();
oWB = SpreadsheetGear.Factory.GetWorkbookSet().Workbooks.OpenFromStream(FileUpload1.PostedFile.InputStream);
SpreadsheetGear.IWorksheet templateWorksheet = oWB.Worksheets[0];
DataSet ds = oWB.GetDataSet(SpreadsheetGear.Data.GetDataFlags.FormattedText);
bool saveit = true;
DateTimeFormatInfo info = new System.Globalization.CultureInfo("en-gb").DateTimeFormat;
if (ds.Tables[0].Rows.Count > 0)
{
if ((ds.Tables[0].Columns["Mobilenumber"].ToString().ToLower() == "mobile") &&
(ds.Tables[0].Columns["Name"].ToString().ToLower() == "name") &&
(ds.Tables[0].Columns["flightdate"].ToString().ToLower() == "flightdate") &&
(ds.Tables[0].Columns["scheduleddeparturetime"].ToString().ToLower() == "departuretime") &&
(ds.Tables[0].Columns["Airport"].ToString().ToLower() == "airport") &&
(ds.Tables[0].Columns["terminal"].ToString().ToLower() == "terminal"))
{
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
row = j.ToString();
DateTime FlightDate = Convert.ToDateTime(ds.Tables[0].Rows[j]["flightdate"],info);
objClass = new WisemiserSMS();
column = ds.Tables[0].Columns["Mobilenumber"].ToString().ToLower();
objClass.PhoneNoxsl = ds.Tables[0].Rows[j]["Mobilenumber"].ToString();
column = ds.Tables[0].Columns["Name"].ToString().ToLower();
objClass.name = ds.Tables[0].Rows[j]["Name"].ToString();
objClass.servertime = System.DateTime.Now;
column = ds.Tables[0].Columns["Airport"].ToString().ToLower();
objClass.airport = ds.Tables[0].Rows[j]["Airport"].ToString();
column = ds.Tables[0].Columns["terminal"].ToString().ToLower();
objClass.terminal = ds.Tables[0].Rows[j]["terminal"].ToString();
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@phoneno", ds.Tables[0].Rows[j]["Mobilenumber"].ToString());
cmd1.Parameters.AddWithValue("@name", ds.Tables[0].Rows[j]["Name"].ToString());
column = ds.Tables[0].Columns["flightdate"].ToString().ToLower();
cmd1.Parameters.AddWithValue("@flightdate", FlightDate.Month + "/" + FlightDate.Day + "/" + FlightDate.Year);
cmd1.Parameters.AddWithValue("@airport", ds.Tables[0].Rows[j]["Airport"].ToString());
cmd1.Parameters.AddWithValue("@terminal", ds.Tables[0].Rows[j]["terminal"].ToString());
column = ds.Tables[0].Columns["scheduleddeparturetime"].ToString().ToLower();
cmd1.Parameters.AddWithValue("@schedularDepartureTime", Convert.ToDateTime(ds.Tables[0].Rows[j]["scheduleddeparturetime"]));
cmd1.ExecuteNonQuery();
transectionocurs = "y";
cmd1.Parameters.Clear();
}
}
else
{
saveit = false;
lblError.Text = "Error:Column name is not correct";
if (transectionocurs == "y")
myTrans.Rollback();
System.IO.File.Delete(filePath);
}
}
else
{
saveit = false;
lblError.Text = "Error:No data found in the file";
if (transectionocurs == "y")
myTrans.Rollback();
System.IO.File.Delete(filePath);
}
if (saveit == true)
{
myTrans.Commit();
lblError.Text = "Data uploaded successfully";
}
//con.Close();
//cmd1.Dispose();
}
catch (Exception ex)
{
if (row != "" && column != "")
{
lblError.Text = "Row " + (Convert.ToInt32(row) + 1) + " and column " + column + " has wrong data";
System.IO.File.Delete(filePath);
if (transectionocurs == "y")
myTrans.Rollback();
}
else
{
lblError.Text = "You have wrong data in excel sheet.Please rectify";
System.IO.File.Delete(filePath);
if (transectionocurs == "y")
myTrans.Rollback();
}
}
}