//code for save file dialog when Export to Excel,export to pdf button is clicked
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
}
private void btnExport_Click(object sender, EventArgs e)
{
// For export EPF data in gridview to MS Excell
if (dgvEmployeeEPF.Rows.Count > 0)
{
try
{
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
ExcelApp.Application.Workbooks.Add(Type.Missing);
ExcelApp.Columns.ColumnWidth = 20;
for (int i = 1; i < dgvEmployeeEPF.Columns.Count + 1; i++)
{
ExcelApp.Cells[1, i] = dgvEmployeeEPF.Columns[i - 1].HeaderText;
}
for (int i = 0; i <= dgvEmployeeEPF.Rows.Count - 1; i++)
{
for (int j = 0; j < dgvEmployeeEPF.Columns.Count; j++)
{
ExcelApp.Cells[i + 2, j + 1] = dgvEmployeeEPF.Rows[i].Cells[j].Value.ToString();
}
}
path();
if (saveFileDialog1.FileName != "")
{
ExcelApp.ActiveWorkbook.SaveCopyAs(str);
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
MessageBox.Show("Excel file created");
Process.Start(str);
}
btnExport.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("Report Not Found");
}
}