I will explain here how to export data from datagridview to excel in c# windows application. Show below Code and excel formate.
using System; using System.Windows.Forms; using System.IO; using System.Data; using System.Data.OleDb; using Excel = Microsoft.Office.Interop.Excel; namespace export_Excel { public partial class Form1 : Form {
xelApp = new Excel.Application(); xelWorkBook = xelApp.Workbooks.Add(misingValue); xelWorkSheet = (Excel.Worksheet)xelWorkBook.Worksheets.get_Item(1); int i = 0; int j = 0;
for (i = 0; i <= GridView1.RowCount - 1; i++) { for (j = 0; j <= GridView1.ColumnCount - 1; j++) { DataGridViewCell cell = GridView1[j, i]; xelWorkSheet.Cells[i + 1, j + 1] = cell.Value; } }
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
There are many options for exporting Excel file. One of them is to use an Excel library like EasyXLS.
The code is below:
For details about exporting other items like colors, fonts, see export datagridview to Excel in C# from EasyXLS website.
I will explain here how to export data from datagridview to excel in c# windows application.
Show below Code and excel formate.
Result