private void btnExcel_Click(object sender, EventArgs e) { if (parentForm.employeeID.ToUpper() == "ADMIN" | parentForm.specialCode == parentForm.txtSpecialCode.Text.Trim().ToString().ToUpper()) { if (dataGridView1.RowCount > 0) { this.Enabled = false; Excel_12.Application ReportFile; Excel_12._Workbook WorkBook; Excel_12._Worksheet WorkSheet; //Excel_12.Range Range; string MaxRow = Convert.ToString(dt.Rows.Count + 1); String MaxColumn = ((String)(Convert.ToChar(dt.Columns.Count / 26 + 64).ToString() + Convert.ToChar(dt.Columns.Count % 26 + 64))).Replace('@', ' ').Trim(); String MaxCell = MaxColumn + MaxRow; ReportFile = new Excel_12.Application(); ReportFile.Visible = false; WorkBook = (Excel_12._Workbook)(ReportFile.Workbooks.Add("workbook")); WorkSheet = (Excel_12._Worksheet)WorkBook.ActiveSheet; for (int i = 0; i < dt.Columns.Count; i++) { WorkSheet.Cells[1, i + 1] = dt.Columns[i].ColumnName; } string[,] Values = new string[dt.Rows.Count, dt.Columns.Count]; for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { Values[i, j] = dt.Rows[i][j].ToString(); } } WorkSheet.get_Range("A2", MaxCell).Value2 = Values; ReportFile.Visible = true; ReportFile.UserControl = true; this.Enabled = true; } else { MessageBox.Show("NO DATA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { if (dataGridView1.RowCount > 0) { ExportDataGridViewTo_Excel12(dataGridView1); } else { MessageBox.Show("NO DATA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } }