Firstly, we create a form as following.
And then Add a reference by right clicking to the “References” in the Solution Explorer.
Then we will see the Reference Manager in the panel. We have to add the Microsoft Excel 16.0 Object Library into our project as a reference for exporting to the Excel.
After the adding is finished, let’s start the project.
Don’t forget to add those refereces to using.
1 2 3 4 | using Excel = Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | private void Form1_Load(object sender, EventArgs e) { dataGridView1.ColumnCount = 3; dataGridView1.Columns[0].Name = "First Name"; dataGridView1.Columns[1].Name = "Last Name"; dataGridView1.Columns[2].Name = "Tel"; dataGridView1.Rows.Add(); dataGridView1.Rows[0].Cells[0].Value = "John"; dataGridView1.Rows[0].Cells[1].Value = "DOE"; dataGridView1.Rows[0].Cells[2].Value = "55982121"; dataGridView1.Rows[1].Cells[0].Value = "David"; dataGridView1.Rows[1].Cells[1].Value = "JOE"; dataGridView1.Rows[1].Cells[2].Value = "54532135"; } |
Button1_Click event
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | private void button1_Click(object sender, EventArgs e) { Excel.Application excel = new Excel.Application(); excel.Visible = true; object Missing = Type.Missing; Workbook workbook = excel.Workbooks.Add(Missing); Worksheet sheet1 = (Worksheet)workbook.Sheets[1]; int StartCol = 1; int StartRow = 1; for (int j = 0; j < dataGridView1.Columns.Count; j++) { Range myRange = (Range)sheet1.Cells[StartRow, StartCol + j]; myRange.Value2 = dataGridView1.Columns[j].HeaderText; } StartRow++; for (int i = 0; i < dataGridView1.Rows.Count; i++) { for (int j = 0; j < dataGridView1.Columns.Count; j++) { Range myRange = (Range)sheet1.Cells[StartRow + i, StartCol + j]; myRange.Value2 = dataGridView1[j, i].Value == null ? "" : dataGridView1[j, i].Value; myRange.Select(); } } } |
All of the Codes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel; namespace DatagridtoExcel { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { dataGridView1.ColumnCount = 3; dataGridView1.Columns[0].Name = "First Name"; dataGridView1.Columns[1].Name = "Last Name"; dataGridView1.Columns[2].Name = "Tel"; dataGridView1.Rows.Add(); dataGridView1.Rows[0].Cells[0].Value = "John"; dataGridView1.Rows[0].Cells[1].Value = "DOE"; dataGridView1.Rows[0].Cells[2].Value = "55982121"; dataGridView1.Rows[1].Cells[0].Value = "David"; dataGridView1.Rows[1].Cells[1].Value = "JOE"; dataGridView1.Rows[1].Cells[2].Value = "54532135"; } private void button1_Click(object sender, EventArgs e) { Excel.Application excel = new Excel.Application(); excel.Visible = true; object Missing = Type.Missing; Workbook workbook = excel.Workbooks.Add(Missing); Worksheet sheet1 = (Worksheet)workbook.Sheets[1]; int StartCol = 1; int StartRow = 1; for (int j = 0; j < dataGridView1.Columns.Count; j++) { Range myRange = (Range)sheet1.Cells[StartRow, StartCol + j]; myRange.Value2 = dataGridView1.Columns[j].HeaderText; } StartRow++; for (int i = 0; i < dataGridView1.Rows.Count; i++) { for (int j = 0; j < dataGridView1.Columns.Count; j++) { Range myRange = (Range)sheet1.Cells[StartRow + i, StartCol + j]; myRange.Value2 = dataGridView1[j, i].Value == null ? "" : dataGridView1[j, i].Value; myRange.Select(); } } } } } |
Output:
I have getting an error Error 1 ‘System.Windows.Forms.DataGridViewTextBoxColumn’ does not contain a definition for ‘Missing’ and no extension method ‘Missing’ accepting a first argument of type ‘System.Windows.Forms.DataGridViewTextBoxColumn’ could be found (are you missing a using directive or an assembly reference?) C:\Users\admin\Desktop\sang solutions\sRoofMetals\sRoofMetals\FrmBank.cs 84 35 sRoofMetals