Asp.Net provides a service Move the data from Database table to Excel sheet by using C# code, Create a Table in Database with Sql server and write C# code for move the data by creating console application in Microsoft Visual studio IDE.Need to install the nuget package from the manage Nuget package.
in sql create table as tbl_students in Database
Goto solution explorer->right click on the project->select Mange Nuget Package
//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using OfficeOpenXml;
namaspace Movedatatoexcelfile
{
class Program
{
static void Main(string[] args)
{
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);
}
}
}
0 comments:
Post a Comment