Yogesh Vedpathak

Yogesh Vedpathak

  • 682
  • 1.4k
  • 188.3k

I want to save form data in excel sheet (csv) using asp.net

Jan 27 2017 1:40 AM
web.config file ...i not getting how to write web.config for excel sheet ....
 
<connectionStrings>
<add name="ConStr" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|InsertDataExcel.xlsx;Extended Properties=Excel 12.0"/>
</connectionStrings>
 
 
 
 
 
my code is here ...aspx file 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
namespace Test
{
public partial class FormToExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string ConStr = "";
string path = Server.MapPath("InsertDataExcel.xlsx");
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;ReadOnly=False;HDR=Yes;\"";
string query = "INSERT INTO [InsertDataExcel.xlsx] ([FirstName], [LastName], [Address], [City], [State],[zip],[IsActive] ) VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + DropDownList1.SelectedValue + "','" + TextBox6.Text + "','" + CheckBoxList1.SelectedValue + "')";
OleDbConnection conn = new OleDbConnection(ConStr);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.Parameters.AddWithValue("@FirstName", TextBox1.Text);
cmd.Parameters.AddWithValue("@LastName", TextBox2.Text);
cmd.Parameters.AddWithValue("@LastName", TextBox3.Text);
cmd.Parameters.AddWithValue("@City", TextBox4.Text);
cmd.Parameters.AddWithValue("@State", DropDownList1.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Zip", TextBox6.Text);
cmd.Parameters.AddWithValue("@FirstName", TextBox1.Text);
cmd.Parameters.AddWithValue("@IsActive", CheckBoxList1.SelectedItem.Text);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
Response.Write("<script>alert('Sucessfully Data Inserted Into Excel')</script>");
}
else
{
Response.Write("<script>alert('Sorry!\n Insertion Failed')</script>");
}
conn.Close();
}
}
}
 
 

Answers (1)