Friday, 16 May 2014

How to import Excel File to DataGridView in window application



                                                                                                                                              Previous..
                                                                                                                                                  Next..
Here i will explain How to import Excel File to DataGridView in window application.Before this i explain how to create excel file in window application.

Step(1) :Form
Drag and down a DataGridView and 2 button. One for Choose File and other for Loading data into DataGridView, And 2 textBox.

Step(2) :Code
Double click on "Choose Button" and  "Load excel" write this code. In tutorial 24 i write this code.


private void Choose_Btn_click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog ();

if(openfile .ShowDialog() ==System.Window.Forms.DialogResult.OK)
{
this.textBox1.Text = openfile .FileName;

}

you add some namespace here.
using system.data.oleDB;
using System.data.sqlClient;

and code on button click on Load Excel Button.
private void LoadExcel_btn_click(object sender, EventArgs e)
{
string Pathconn = "Provider = Microsoft.Jet.OLEDB.4.0; DataSource = "+textBox1.Text+" ; Extended properties=\"Excel 8.0;HDR=YES;\";";

oledebconnection  con =  new oledebconnection ( Pathconn);

OledbDataAdapter MydaAtadapter =  new OledbDataAdapter ( "Select * from [ "+TextBox2.Text+ "$]", con;
DataTable dt = new DataTable ();

MydaAtadapter .Fill(dt);

dataGridView1.DataSource = dt;

}

So Code Is
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.data.oleDB;

using System.data.sqlClient;

using System.Text;
using System.IO;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
private void Choose_Btn_click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog ();

if(openfile .ShowDialog() ==System.Window.Forms.DialogResult.OK)
{
this.textBox1.Text = openfile .FileName;

}

private void LoadExcel_btn_click(object sender, EventArgs e)
{
string Pathconn = "Provider = Microsoft.Jet.OLEDB.4.0; DataSource = "+textBox1.Text+" ; Extended properties=\"Excel 8.0;HDR=YES;\";";

oledebconnection  con =  new oledebconnection ( Pathconn);

OledbDataAdapter MydaAtadapter =  new OledbDataAdapter ( "Select * from [ "+TextBox2.Text+ "$]", con;
DataTable dt = new DataTable ();

MydaAtadapter .Fill(dt);

dataGridView1.DataSource = dt;

}

Step(3) :OutPut
Now Run your application

                                                                                                                                              Previous..
                                                                                                                                                  Next..

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..

Monday, 12 May 2014

Create Excel(.XLS and .XLSX) file from C# using excellibrary in window application


                                                                                                                                        Previous....
                                                                                                                                              Next..

Here i will explain how to Create Excel(.XLS and .XLSX) file from C# using excellibrary in window application.

Step(1): Form
Drag and down a button from tool box and give it name a "Create Excel File".

Step(2): Add excellibrary file in application
Before working for creating excel file we have to add a Excel File throw this we can access Excel properties.
To add excel file search on google As excellibrary and Open it 1st link.

Open this First link then you will see a tab for download. From here you download "excellibrary.dll" .

And extract this file into your application.

Step(3): Code
Double click on button and write this code. For this we have to add 2 namespace.
using ExcelLibrary.CompoundDocumentFormat;
using ExcelLibrary.SpreadSheet;
Before this you add reference of  "excellibrary.dll" into your application for this right click on reference and "add reference" and go to browser and select .dll file.




For writing code you can copy code from this link and use this code for practice.




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 System.Text;
using System.IO;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
private void Button1_click(object sender, EventArgs e)
{
//create new xls file
string file = "newdoc.xls";
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("First Sheet");
worksheet.Cells[0, 1] = new Cell((short)1);
worksheet.Cells[2, 0] = new Cell(9999999);
worksheet.Cells[3, 3] = new Cell((decimal)3.45);
worksheet.Cells[2, 2] = new Cell("Text string");
worksheet.Cells[2, 4] = new Cell("Second string");
worksheet.Cells[4, 0] = new Cell(32764.5, "#,##0.00");
worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY\-MM\-DD");
worksheet.Cells.ColumnWidth[0, 1] = 3000;
workbook.Worksheets.Add(worksheet);
workbook.Save(file);
// open xls file
Workbook book = Workbook.Load(file);
Worksheet sheet = book.Worksheets[0];

}


Step(3): Run
Now Run your application and you will see excel file like this.
                                                                                                                                        Previous....
                                                                                                                                              Next..

Create a Text File and write in it in window application


                                                                                                                                    Previous....
                                                                                                                                          Next..

Hare i will explain how Create a Text File and  write in it in window application.

Step(1): Form
Drag and down a Button from tool box and give name it as Create File.


Step(2): Before creating a Text File.
Right click on Solution Explorer and click on "Open Folder in Window Explorer" Go to this path.
"WindowFormApplication2 -> Bin -> Debug"
Here you will see that there is not any Text File.





Step(3): Code

Double click on Button and write this code.
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 System.Text;
using System.IO;

using System.Text;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

private void Button1_click(object sender, EventArgs e)
{
  StreamWriter File = new StreamWriter ("Text_File.Text");
File.Write("Hi Munesh, How r u");
File.Write("Hi Munesh, How r u");
File.Write("Hi Munesh, How r u");
}

Step(4): After creating a Text File.
Now run your application and then you will go on that path you will see that file.

Step(5): Run application
Run your application and open that Text File.
                                                                                                                                    Previous....
                                                                                                                                          Next..

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...