Export DataGridView To Excel In c# Windows Application Step By Step

Sdílet
Vložit
  • čas přidán 28. 11. 2020
  • DataGridView tutorial. This tutorial I will show you, export DataGridView to excel in c# windows form application easily step by step. This is very simple code to export DataGridView to excel easily. You can learn how to export DataGridView to excel in c# using copy to clipboard step by step following this tutorial.
    This tutorial also covered
    -how to load data from sql server database to DataGridView in c# windows form application.
    -how to connect sql server database with windows form application using c#.
    -how to add reference Microsoft excel in your project.
    -how to export DataGridView to excel using c#.
    Follow these steps-
    1. Create a project.
    2. Design the form with label, button and DataGridView .
    3. Write code in button click event to load data in DataGridView using c#.
    4. Write code in export button click event to export DataGridView to excel c#.
    private void ExportToExcel_Click(object sender, EventArgs e)
    {
    dataGridView1.SelectAll();
    DataObject copydata = dataGridView1.GetClipboardContent();
    if (copydata != null) Clipboard.SetDataObject(copydata);
    Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
    xlapp.Visible = true;
    Microsoft.Office.Interop.Excel.Workbook xlWbook;
    Microsoft.Office.Interop.Excel.Worksheet xlsheet;
    object miseddata = System.Reflection.Missing.Value;
    xlWbook = xlapp.Workbooks.Add(miseddata);
    xlsheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWbook.Worksheets.get_Item(1);
    Microsoft.Office.Interop.Excel.Range xlr = (Microsoft.Office.Interop.Excel.Range)xlsheet.Cells[1, 1];
    xlr.Select();
    xlsheet.PasteSpecial(xlr, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
    }
    Note- You must have installed Microsoft office as well as excel in the PC- export datagridview to excel in c#.
    ------
    This channel covers all the programming tutorial related with .Net- C#, linq, VB, SQL, Android, HTML, CSS, jQuery, Crystal Report and Microsoft Report.
    So, Please subscribe and keep in touch.
    / @programminggeek
    Visit my page in Facebook
    / programminggeek7
    ------
    More Tags
    #ExportDataGridViewToExcelInCSharp #ExportToExceTutorial #ProgrammingGeek
    programminggeek,export datagridview to excel in c#, export datagridview to excel,export gridview to excel c#, c# export to excel, export datagridview to excel windows application, how to export datagridview data instantly to excel on button click, export grid data to excel in c#,visual studio c# export to excel, export dataview to excel c

Komentáře • 64

  • @maheswariganapathy
    @maheswariganapathy Před rokem

    Simply Superb....Thanks for the Video...😃

  • @paulinedonor
    @paulinedonor Před rokem

    Well demonstrated! I have been watching videos as this was for my exams but this one is the best! It worked on me. Thank you sir!❤

  • @martinmanalansan8941
    @martinmanalansan8941 Před rokem

    Thanks for the Video!😎

  • @azamchy6019
    @azamchy6019 Před 3 lety

    Nice demo.

  • @aimanmalkawi8831
    @aimanmalkawi8831 Před 2 lety +1

    Thanks man... i have been struggling with this one until i met your video. All the best and keepup the good work

    • @ProgrammingGeek
      @ProgrammingGeek  Před 2 lety

      Glad it helped. You are very kind. Please keep connected.

  • @aminaali5905
    @aminaali5905 Před 3 lety +2

    Great tutorial thanks 👍😍😍👍

  • @rasulvalipoor7125
    @rasulvalipoor7125 Před 2 lety +1

    Oooh thanks boy, great and simple, I was really stock

    • @ProgrammingGeek
      @ProgrammingGeek  Před 2 lety

      Glad I could help. You are also great and kind. Please keep in touch!!

  • @dulalmia5243
    @dulalmia5243 Před 3 lety

    Well demonstration. Keep it up.

  • @nizamuddin4128
    @nizamuddin4128 Před 3 lety +1

    Wow! its working fine. thanks.

  • @wildanfirdaus7502
    @wildanfirdaus7502 Před 2 lety +1

    amazing, thanks !!!

  • @user-ig8pg8ov9y
    @user-ig8pg8ov9y Před 3 lety +1

    Thank you!

  • @lynguyen-sh5jw
    @lynguyen-sh5jw Před 3 lety +1

    thank you!!! it is worked

  • @yashomanityagi4104
    @yashomanityagi4104 Před 11 měsíci +1

    but data is exporting without column headers
    how we can do that ???

  • @furkanturkylmaz3781
    @furkanturkylmaz3781 Před rokem

    Thanks for video, Can you explain how we can export the cell headers to excel

  • @rambuddinagalakshmi7515
    @rambuddinagalakshmi7515 Před 2 lety +1

    So helpful thankyou so much

  • @purbahazra7462
    @purbahazra7462 Před 2 lety +1

    Thanks a lot , really it helps me a lot

  • @TenzinThinley
    @TenzinThinley Před rokem

    super content. did u make video on how to import excel to datagridview?

  • @rasulvalipoor7125
    @rasulvalipoor7125 Před 2 lety

    What should I do if I want to extract a table with border

  • @user-wx8xp9kq4i
    @user-wx8xp9kq4i Před rokem

    sir can i make visible the column name on the top of excel report ????

  • @hindbenkouch9819
    @hindbenkouch9819 Před 2 lety +1

    thank you soooooooooo much

  • @prosaa9168
    @prosaa9168 Před 3 lety

    i have an exception when export date, it will become ###### can you fix it, thanks alot

  • @mo-awny
    @mo-awny Před 8 měsíci

    how can i set sheet right to left ? with ur code ?! can you help me sir

  • @ehtishamakram4437
    @ehtishamakram4437 Před rokem

    I want to apend data in existing excel file but its always make new file. can anyone help me out ?

  • @fatenaljabal3478
    @fatenaljabal3478 Před 2 lety +1

    thanx

  • @miurangadananjaya1479
    @miurangadananjaya1479 Před 2 lety +1

    good

  • @user-eo7dx5bp3f
    @user-eo7dx5bp3f Před rokem

    i USE GRAPH ,IS POSSIBLE TO EXPORT EXCEL

  • @kyawaungmoe540mdy
    @kyawaungmoe540mdy Před 3 lety +1

    i want to known how to export selected column in C# with window application

  • @parcasio25
    @parcasio25 Před 3 lety +1

    I have an error " System.IO.FileNotFoundException: 'Could not load file or assembly 'office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.'" Could you help me?

    • @ProgrammingGeek
      @ProgrammingGeek  Před 3 lety

      If you don't have latest Microsoft Office please install Microsoft Office latest version.

    • @parcasio25
      @parcasio25 Před 3 lety +1

      @@ProgrammingGeek Currently I am using Office 2010 :

    • @ProgrammingGeek
      @ProgrammingGeek  Před 3 lety

      @@parcasio25 Add references with Microsoft office excel by following this tutorial. May be you will success. thanks.

  • @KaranbirJanjua
    @KaranbirJanjua Před 3 lety +1

    Thanks for the tutorial but I was wondering why the headers are not getting copied as well ? thanks

    • @ProgrammingGeek
      @ProgrammingGeek  Před 3 lety +2

      OH! You got it. Actually i didn't see it during the video creation. I will check it. thanks for your comment.

    • @bullseyerendan7486
      @bullseyerendan7486 Před 2 lety

      @@ProgrammingGeek so did you find out why it aint getting copied, if yes then can you please share your code

    • @ncueBenson
      @ncueBenson Před rokem

      @@bullseyerendan7486 because header won't be copy just set yourgrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;

    • @joaodematos4083
      @joaodematos4083 Před měsícem

      You just need to enable this propriety in your datagrid. ClipBoardCopyMode = EnableAlwaysIncludeHeaderText

  • @surya.banerjee
    @surya.banerjee Před rokem

    For windows 11 it's not working 😢 why ? How to solve?

  • @marcadrianc.benales1015
    @marcadrianc.benales1015 Před 2 lety +1

    How about the header Title?

    • @joaodematos4083
      @joaodematos4083 Před měsícem

      You just need to enable this propriety in your datagrid. ClipBoardCopyMode = EnableAlwaysIncludeHeaderText

  • @womercabrestante7938
    @womercabrestante7938 Před 2 lety

    thing kwu sa Code priii agiiiik!!!

  • @CesarHernandez-vx7ti
    @CesarHernandez-vx7ti Před rokem +1

    TO PDF???

  • @kusinanidaddymonyo4844
    @kusinanidaddymonyo4844 Před 5 měsíci

    how to include the column name?

    • @ProgrammingGeek
      @ProgrammingGeek  Před 5 měsíci

      Mentioned in the tutorial. thanks

    • @joaodematos4083
      @joaodematos4083 Před měsícem

      You just need to enable this propriety in your datagrid. ClipBoardCopyMode = EnableAlwaysIncludeHeaderText