You are on page 1of 3

// code for Animation in Open

for (int i = 0; i < 300; i++)

using Excel = Microsoft.Office.Interop.Excel;

private void btnExport_Click()


{

DataGridView DTGRIDVIEWEXCEL = new DataGridView();

DTGRIDVIEWEXCEL = dataGridView1;

// creating Excel Application


Excel._Application app = new
Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Excel._Worksheet worksheet = null;
// see the excel sheet behind the program
app.Visible = false;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";

// storing header part in Excel


for (int i = 1; i < DTGRIDVIEWEXCEL.Columns.Count + 1; i++)
{
worksheet.Cells[4, i] = DTGRIDVIEWEXCEL.Columns[i - 1].HeaderText;
worksheet.Cells[4, i].EntireRow.Font.Bold = true;

}
// storing Each row and column value to excel sheet
for (int i = 0; i < DTGRIDVIEWEXCEL.Rows.Count; i++)
{
for (int j = 0; j < DTGRIDVIEWEXCEL.Columns.Count; j++)
{
if (tabControl1.SelectedTab == tabPage1)
{

worksheet.Cells[i + 5, j + 1] =
DTGRIDVIEWEXCEL.Rows[i].Cells[j].Value.ToString();

}
}
worksheet.Columns.AutoFit();

int lastUsedColumn = worksheet.Cells.Find("*",


System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
Excel.XlSearchOrder.xlByColumns,
Excel.XlSearchDirection.xlPrevious,
false, System.Reflection.Missing.Value,
System.Reflection.Missing.Value).Column;

// Code For Header

worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[2,


lastUsedColumn]].Merge();

worksheet.Cells[1, 1] = "OPTECH REPORT";


worksheet.Cells[1, 1].Font.Bold = true;
worksheet.Cells[1, 1].Font.Size = 18;
worksheet.Cells[1, 1].HorizontalAlignment =
Excel.XlHAlign.xlHAlignCenter;
worksheet.Cells[1, 1].VerticalAlignment =
Excel.XlVAlign.xlVAlignCenter;
worksheet.Cells[1, 1].Font.Color = Excel.XlRgbColor.rgbDarkRed;

worksheet.Cells[1, 1].Borders.LineStyle =
Excel.XlLineStyle.xlContinuous;

//Code for Print Date


worksheet.Range[worksheet.Cells[3, 1], worksheet.Cells[3,
lastUsedColumn]].Merge();
worksheet.Cells[3, 1] = DateTime.Now.ToString();
worksheet.Cells[3, 1].Font.Bold = true;
worksheet.Cells[3, 1].Font.Size = 14;
worksheet.Cells[3, 1].HorizontalAlignment =
Excel.XlHAlign.xlHAlignCenter;
worksheet.Cells[3, 1].VerticalAlignment =
Excel.XlVAlign.xlVAlignCenter;
worksheet.Cells[3, 1].Font.Color = Excel.XlRgbColor.rgbDarkBlue;

for (int i = 1; i <= lastUsedColumn; i++)


{
if (worksheet.Columns[i].ColumnWidth < 10)
{
worksheet.Columns[i].ColumnWidth = 10;
}
}

SaveFileDialog1.ShowDialog();
try
{
workbook.SaveAs(SaveFileDialog1.FileName, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing,
Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
this.Cursor = Cursors.Default;
app.Visible = true;
MessageBox.Show("Excel Export Successful");
}
catch (Exception)
{

MessageBox.Show("Excel Export Failed");


}

// Exit from the application


//app.Quit();

You might also like