Insert records into excel through textboxes


In this blog we will know how to Insert records into excel through text boxes.

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.OleDb;

 

namespace Insert_data_into_excel_asp

{

    public partial class _Default : System.Web.UI.Page

    {

        OleDbConnection con;

        OleDbCommand com;

        string str;

 

        protected void btn_save_Click(object sender, EventArgs e)

        {

            try

            {

 

                com = new OleDbCommand();

                con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='c:\\file\\test.xlsx';Extended Properties=Excel 8.0;");

                con.Open();

                com.Connection = con;

                str = "Insert into [Sheet1$] (EID,EName,Designation) values('" + txt_id.Text + "','" + txt_name.Text + "','" + txt_designation.Text + "')";

                com.CommandText = str;

                com.ExecuteNonQuery();

                con.Close();

                Response.Write("Records inserted successfully");

                clear();

            }

            catch (Exception ex)

            {

                ex.ToString();

            }

        }

 

        private void clear()

        {

            txt_id.Text = "";

            txt_name.Text = "";

            txt_designation.Text = "";

        }


    }

}

 
Ebook Download
View all
Learn
View all