1.>Put Excel Sheet in Root Directory Of Your Project
2.>Create New Page.
3.>C# Code
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
public partial class UploadContactData : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ReadExcel();
}
}
public void ReadExcel()
{
try
{
if (File.Exists(Server.MapPath("~/Import.xls")))
{
string connectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/Import.xls") + ";";
connectionString += "Extended Properties=Excel 8.0;";
OleDbConnection myConnection = new OleDbConnection(connectionString);
DataSet ds = new DataSet();
OleDbCommand myCommand = new OleDbCommand("Select * from [ContactCard$];", myConnection);//Sheet Name
myConnection.Open();
OleDbDataAdapter da = new OleDbDataAdapter(myCommand);
da.Fill(ds);
myConnection.Close();
int count = 0;
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i]["ReferenceNo"].ToString().Trim().Length > 0)
{
CommonFunction CF = new CommonFunction();
SqlCommand cmd = new SqlCommand("DataImport", CF.GetConnection());
cmd.Parameters.AddWithValue("@ReferenceNo", ds.Tables[0].Rows[i]["ReferenceNo"].ToString().Trim());
cmd.Parameters.AddWithValue("@ContactFullName", ds.Tables[0].Rows[i]["Name"].ToString().Trim());
cmd.Parameters.AddWithValue("@Address", ds.Tables[0].Rows[i]["Address"].ToString().Trim());
cmd.Parameters.AddWithValue("@IPAddress", CommonFunction.GetIPAddress());
cmd.Parameters.AddWithValue("@Browser", CommonFunction.GetWebBrowser());
cmd.Parameters.AddWithValue("@WebURL", CommonFunction.GetWebURL());
cmd.Parameters.AddWithValue("@CreatedBy", "00001");
cmd.Parameters.Add("@msg", SqlDbType.NVarChar, 10000);
cmd.Parameters["@msg"].Direction = ParameterDirection.Output;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 1600;
cmd.ExecuteNonQuery();
CF.CloseConnection();
lblMsg.Text += Convert.ToString(cmd.Parameters["@msg"].Value);
if (!Convert.ToString(cmd.Parameters["@msg"].Value).Contains("Error"))
{
count++;
}
}
}
lblError.Text += count.ToString() + " Record(s) Imported Successfully from " + ds.Tables[0].Rows.Count + " Record(s)"; ;
}
else
{
lblError.Text += "File did not have any Record(s) for Import";
}
}
else
{
lblError.Text += "File Does not exist on given path";
}
}
catch (Exception ex)
{
lblError.Text = ex.Message.ToString();
}
}
}
2.>Create New Page.
3.>C# Code
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
public partial class UploadContactData : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ReadExcel();
}
}
public void ReadExcel()
{
try
{
if (File.Exists(Server.MapPath("~/Import.xls")))
{
string connectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/Import.xls") + ";";
connectionString += "Extended Properties=Excel 8.0;";
OleDbConnection myConnection = new OleDbConnection(connectionString);
DataSet ds = new DataSet();
OleDbCommand myCommand = new OleDbCommand("Select * from [ContactCard$];", myConnection);//Sheet Name
myConnection.Open();
OleDbDataAdapter da = new OleDbDataAdapter(myCommand);
da.Fill(ds);
myConnection.Close();
int count = 0;
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i]["ReferenceNo"].ToString().Trim().Length > 0)
{
CommonFunction CF = new CommonFunction();
SqlCommand cmd = new SqlCommand("DataImport", CF.GetConnection());
cmd.Parameters.AddWithValue("@ReferenceNo", ds.Tables[0].Rows[i]["ReferenceNo"].ToString().Trim());
cmd.Parameters.AddWithValue("@ContactFullName", ds.Tables[0].Rows[i]["Name"].ToString().Trim());
cmd.Parameters.AddWithValue("@Address", ds.Tables[0].Rows[i]["Address"].ToString().Trim());
cmd.Parameters.AddWithValue("@IPAddress", CommonFunction.GetIPAddress());
cmd.Parameters.AddWithValue("@Browser", CommonFunction.GetWebBrowser());
cmd.Parameters.AddWithValue("@WebURL", CommonFunction.GetWebURL());
cmd.Parameters.AddWithValue("@CreatedBy", "00001");
cmd.Parameters.Add("@msg", SqlDbType.NVarChar, 10000);
cmd.Parameters["@msg"].Direction = ParameterDirection.Output;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 1600;
cmd.ExecuteNonQuery();
CF.CloseConnection();
lblMsg.Text += Convert.ToString(cmd.Parameters["@msg"].Value);
if (!Convert.ToString(cmd.Parameters["@msg"].Value).Contains("Error"))
{
count++;
}
}
}
lblError.Text += count.ToString() + " Record(s) Imported Successfully from " + ds.Tables[0].Rows.Count + " Record(s)"; ;
}
else
{
lblError.Text += "File did not have any Record(s) for Import";
}
}
else
{
lblError.Text += "File Does not exist on given path";
}
}
catch (Exception ex)
{
lblError.Text = ex.Message.ToString();
}
}
}
No comments:
Post a Comment