Wednesday, 18 January 2012

HOW TO UPLOAD EXCEL FILE AND SAVE IT DATABASE IN DYNAMICLY

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
#region
static OleDbConnection cn = null;
static OleDbConnection cn1 = null;
OleDbCommand cm = null;
OleDbDataAdapter da = null;
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
int i,j,k;
string s, ss = "varchar(50)";
string[] st1;
#endregion
protected void Page_Load(object sender, EventArgs e)
{
cn1 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("swash.accdb") + ";Persist Security Info=False;");
if (!IsPostBack)
{
cn1.Open();
//fill1();
}
}
protected void btn_Click(object sender, EventArgs e)
{
try
{
string s = null;
if (fu.HasFile)
{
string[] st = fu.PostedFile.ContentType.ToString().Split('/');
if (st[0] == "application")
{

st1 = fu.FileName.ToString().Split('.');
s = "test" + "." + st1[1].ToString();
fu.SaveAs(Server.MapPath("~/upload/" + s));
string t = Server.MapPath("~/upload/" + s);
if (st1[1].ToString().Trim() == "xlsx".Trim())
cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + t + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"");
else
cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + t + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"");
fill();
ViewState["dt"] = st1[0].ToString();
}
else
ClientScript.RegisterStartupScript(GetType(), "x", "");


}
}
catch (Exception ex)
{
Response.Write(""+ex.Message.ToString()+"");
}

}
void fill1(string t)
{
if (cn1.State == ConnectionState.Closed)
cn1.Open();
da = new OleDbDataAdapter("select * from "+t+"", cn1);
dt1.Clear();
da.Fill(dt1);
gv1.DataSource = dt1;
gv1.DataBind();
}
void fill()
{

da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", cn);
dt.Clear();
da.Fill(dt);
gv.DataSource = dt;
gv.DataBind();

}
protected void Button1_Click(object sender, EventArgs e)
{
fill();
try
{
fill();

k = gv.Rows[j].Cells.Count;
ViewState["c"] = k;
s = string.Empty;
if (Convert.ToInt32(tb.Text) > 1)
{
j = (Convert.ToInt32(tb.Text) - 2);
for (i = 0; i < k; i++) { if (gv.Rows[j].Cells[i].Text != " ") { s = s + gv.Rows[j].Cells[i].Text + " " + ss + ","; } } } else { for (i = 0; i < k; i++) { if (gv.HeaderRow.Cells[i].Text != " ") { s = s + gv.HeaderRow.Cells[i].Text + " " + ss + ","; } } } s=s.Substring(0,(s.ToString().Length-1)); if (cn1.State == ConnectionState.Closed) cn1.Open(); cm = new OleDbCommand("create table " + ViewState["dt"].ToString() + " ( " + s + ")", cn1); cm.ExecuteNonQuery(); j = 0; s = string.Empty; k = Convert.ToInt32(ViewState["c"]); if (Convert.ToInt32(tb1.Text) > 1)
{
for (i = (Convert.ToInt32(tb1.Text) - 2); i < (Convert.ToInt32(tb2.Text)-1); i++) { if (cn1.State == ConnectionState.Closed) cn1.Open(); s = string.Empty; for (j = 0; j < k; j++) if (gv.Rows[i].Cells[j].Text != " ") { s = s + "'" + gv.Rows[i].Cells[j].Text + "'" + ","; } s = s.Substring(0, (s.ToString().Length - 1)); cm = new OleDbCommand("insert into " + ViewState["dt"].ToString() + " values(" + s + ")", cn1); cm.ExecuteNonQuery(); } } else { for (i = (Convert.ToInt32(tb1.Text) - 1); i < (Convert.ToInt32(tb2.Text)-1); i++) { if (cn1.State == ConnectionState.Closed) cn1.Open(); s = string.Empty; for (j = 0; j < k; j++) if (gv.Rows[i].Cells[j].Text != " ") { s = s + "'" + gv.Rows[i].Cells[j].Text + "'" + ","; } s = s.Substring(0, (s.ToString().Length - 1)); cm = new OleDbCommand("insert into " + ViewState["dt"].ToString() + " values(" + s + ")", cn1); cm.ExecuteNonQuery(); } } fill1(ViewState["dt"].ToString()); ClientScript.RegisterStartupScript(GetType(), "x", "");
}
catch (DBConcurrencyException ex)
{
Response.Write("" + ex.Message.ToString() + "");
}
}
}

No comments:

Post a Comment