Export DataGridView To Excel In c# Windows Application Step By Step
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
Simply Superb....Thanks for the Video...😃
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!❤
Thanks for the Video!😎
Nice demo.
Thanks man... i have been struggling with this one until i met your video. All the best and keepup the good work
Glad it helped. You are very kind. Please keep connected.
Great tutorial thanks 👍😍😍👍
Thanks for your comments 😃
Oooh thanks boy, great and simple, I was really stock
Glad I could help. You are also great and kind. Please keep in touch!!
Well demonstration. Keep it up.
Thanks, will do!
Wow! its working fine. thanks.
Thanks for your comment. please keep in touch.
amazing, thanks !!!
You are really intelligent. Thanks
Thank you!
Most welcome.
thank you!!! it is worked
You're welcome!
but data is exporting without column headers
how we can do that ???
Thanks for video, Can you explain how we can export the cell headers to excel
So helpful thankyou so much
Glad it was helpful! Please keep connected. Thanks
Thanks a lot , really it helps me a lot
Glad it helped! Thanks.
super content. did u make video on how to import excel to datagridview?
What should I do if I want to extract a table with border
sir can i make visible the column name on the top of excel report ????
thank you soooooooooo much
Thanks you too for your time.
i have an exception when export date, it will become ###### can you fix it, thanks alot
how can i set sheet right to left ? with ur code ?! can you help me sir
I want to apend data in existing excel file but its always make new file. can anyone help me out ?
thanx
You're welcome!
good
Thanks for your comment.
i USE GRAPH ,IS POSSIBLE TO EXPORT EXCEL
i want to known how to export selected column in C# with window application
I will come with this tutorial very soon.
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?
If you don't have latest Microsoft Office please install Microsoft Office latest version.
@@ProgrammingGeek Currently I am using Office 2010 :
@@parcasio25 Add references with Microsoft office excel by following this tutorial. May be you will success. thanks.
Thanks for the tutorial but I was wondering why the headers are not getting copied as well ? thanks
OH! You got it. Actually i didn't see it during the video creation. I will check it. thanks for your comment.
@@ProgrammingGeek so did you find out why it aint getting copied, if yes then can you please share your code
@@bullseyerendan7486 because header won't be copy just set yourgrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
You just need to enable this propriety in your datagrid. ClipBoardCopyMode = EnableAlwaysIncludeHeaderText
For windows 11 it's not working 😢 why ? How to solve?
How about the header Title?
You just need to enable this propriety in your datagrid. ClipBoardCopyMode = EnableAlwaysIncludeHeaderText
thing kwu sa Code priii agiiiik!!!
TO PDF???
I will do it soon.
how to include the column name?
Mentioned in the tutorial. thanks
You just need to enable this propriety in your datagrid. ClipBoardCopyMode = EnableAlwaysIncludeHeaderText