Рет қаралды 33,032
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