Asp.net Code for Move the data by using User Interface with C# code.Create a table in SQL server next open the Microsoft Visual Studio IDE Create the new Asp.net web application then right click on the solution go to manage nuget package search the EPPlus excel sheet library and install that package.
n sql create table as tbl_students in Database
Goto solution explorer->right click on the project->select Mange Nuget Package-> install the EPPlus package
//UIexcel.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sqlclient
using System.Data;
using System.Configuration;
using System.IO;
using System.OfficeOpenXml;
namespace MovedatawithUI
{
public partial class UIexcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
txt1.Focus();
}
}
protected void btn1_click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["dbconn"].Connectionstring.Tostring();
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("insert into tbl_tablename(id,name,address) values(@id,@name,@address)",con);
cmd.Parameters.AddWithValue("@id",txt1.Text);
cmd.Parameters.AddWithValue("@id",TextBox1.Text);
cmd.Parameters.AddWithValue("@id",TextBox2.Text);
con.Open();
cmd.ExcuteNonQuery();
con.Close();
lbl4.Text = "Data Saved Successfully";
}
protected void btn2_click(object sender, EventArgs e)
{
var file = FileInfo(@"C:\Users\sample.xlsx");
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using(ExcelPackage excel = new ExcelPackage(file))
{
ExcelWorksheet sheet = excel.Workbook.Worksheets[''sheet1"];
SqlConnection con = new SqlConnection(@"");
SqlCommand cmd = new SqlCommand("select * from tbl_students");
con.Open();
SqlDataAdapter da= new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
int count = dt.Rows.Count;
sheet.Cells.LoadDataFromDataTable(dt,true);
FileInfo xfile = new FileInfo(@"C:\Users\sample.xlsx");
excel.SaveAs(xfile);
}
}
}
//UIexcel.aspx
//UIexcel.aspx
0 comments:
Post a Comment