Export DataGridView Value in EXCEL in windows Application

Bageshkumarbagi
Posted by Bageshkumarbagi under C# category on | Points: 40 | Views : 1214
using System.Reflection;
using Microsoft.Office.Interop;

private void Button_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook wb = null;
Microsoft.Office.Interop.Excel.Application ExcelApp =
new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Application.Workbooks.Add(Type.Missing);
ExcelApp.Columns.ColumnWidth = 20;
for (int i = 1; i < ddBagi.Columns.Count + 1; i++)
{
ExcelApp.Cells[1, i] = ddBagi.Columns[i - 1].HeaderText;
}

for (int i = 0; i < ddBagi.Rows.Count - 1; i++)
{
for (int j = 0; j < ddBagi.Columns.Count; j++)
{
ExcelApp.Cells[i + 2, j + 1] = ddBagi.Rows[i].Cells[j].Value.ToString();
}
}
ExcelApp.Visible = true;
ExcelApp.ActiveWorkbook.SaveCopyAs("c:\\Excel.xlsx");
ExcelApp.ActiveWorkbook.Saved = true;
}

Comments or Responses

Posted by: chaudharysunny1989-22042 on: 5/6/2013 Level:Starter | Status: [Member] | Points: 10
using Microsoft.Office.Interop; this is generate error where are this file reside

Login to post response