0% found this document useful (0 votes)
5 views1 page

Export To Excel CODE

Uploaded by

Rena Salazar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Export To Excel CODE

Uploaded by

Rena Salazar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

if (dataGridView1.Rows.

Count > 0)
{
Microsoft.Office.Interop.Excel.ApplicationClass MExcel = new
Microsoft.Office.Interop.Excel.ApplicationClass();
MExcel.Application.Workbooks.Add(Type.Missing);
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
MExcel.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
MExcel.Cells[i + 2, j + 1] =
dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
MExcel.Columns.AutoFit();
MExcel.Rows.AutoFit();
MExcel.Columns.Font.Size = 12;
MExcel.Visible = true;
}
else
{
MessageBox.Show("No records found!", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}

You might also like