vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Monday, September 10, 2012

Export Textbox value or Dynamic values in to Excel Sheet in asp.net


Here I would like to share the bit of code to export the textbox values or any Dynamic values into Excel format.
In a previous article I wrote about import the excel sheet data file in to DataTable
By using this below code you can export the data in to excel sheet. Here we can get the input data from textbox
After give the input to TextBox when you press the button the data will be upload into excel sheet in a particular colum.
In the excel sheet I have two columns Name and Mobile, so from the textbox I upload the value to excel sheet.
I hope this is the simple process to append the data to excel sheet
Here I used two textbox and Button
Write the below code on button click event
Code:
protected void Button1_Click(object sender, EventArgs e)
    {      
        string dbpath1 = Server.MapPath("Test\\Book1.xlsx");
        string dbPath = AppDomain.CurrentDomain.BaseDirectory + "Test\\Book1.xlsx";
        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbPath + ";Extended Properties='Excel 12.0;HDR=YES'";
       
        str1 = "INSERT INTO [sheet1$]([Name],[Mobile]) VALUES(@name,@mobile)";
        cmd = new OleDbCommand(str1, con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Add("@name", System.Data.DbType.String).Value = TextBox1.Text.ToString();
        cmd.Parameters.Add("@mobile", System.Data.DbType.String).Value = TextBox2.Text.ToString();
        con.Open();
        try
        {
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception ex)
        {
          
        }
       
    }

Happy coding..
Post comments

0 comments:

Post a Comment