Wednesday, 14 May 2014

How to export data from database to Excel file in window form


                                                                                                                                          Previous..
                                                                                                                                              Next..

Here i will explain How  to export data from database to Excel file in window form.In Tutorial 28 i show you How to create Excel sheet ,So i suggest you before coming on  this tutorial you go on  Tutorial 28.

Step(1) :Form
In tutorial 14 i show you how to show data in datagridview from database. Here i will show you that data in Excel File.

 Step(2) : Load the data into Data Grid View from database
Click on the Load button and write code for loading data. For this code you Go on my tutorial 14.

Step(3) :  Code
In Tutorial 28 i show you How to create Excel sheet ,So i suggest you before coming on  this tutorial you go on  Tutorial 28.

here we will add two more Namespace.
using ExcelLibrary.BinaryDrawingFormat;
using ExcelLibrary.BinaryFileFormat;



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using ExcelLibrary.CompoundDocumentFormat;
using ExcelLibrary.SpreadSheet;

using ExcelLibrary.BinaryDrawingFormat;
using ExcelLibrary.BinaryFileFormat;

using System.Text;
using System.IO;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 private void Load_button_Click(object sender, EventArgs e)
    SqlConnection con = new SqlConnection("server = MUNESH;Database=datastore;UID=sa;Password=123;");
    SqlCommand cmd = new SqlCommand("Select  ID,Name,SurName,Age, from data1", con);
    try
   {
        
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataTable dt = new DataTable();
        da.Fill(dt);
      BindingSource bsource = new BindingSource ();
    
    bsource .DataSource = dt;
        dataGridView1.DataSource = bsource ;
da.Update(dt);


DataSet ds = new DataSet("New_DataSet");
ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
da.Fill( dt );
ds.Tables.Add( dt );
ExcelLibrary. DataSetHelper.CreateWorkBook("MyFirstExcelSheet.xls"  ,ds);
       
    }
    catch (Exception ec)
   {
        MessageBox.Show(ec.Message);
   }



Step(4) :  OutPut

Run your application and click on load event.
                                                                                                                                          Previous..
                                                                                                                                              Next..

1 comment:

  1. ExcelLibrary. DataSetHelper.CreateWorkBook Not worked for small data, always show incorrect file format.
    Works well in normal data.

    ReplyDelete

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...