Advertisement
Balda

Untitled

Dec 25th, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. private void ExportToExcel(DataGridView dgv)
  2. {
  3. if (dgv_report.Rows.Count > 0)
  4. {
  5.  
  6. Microsoft.Office.Interop.Excel.Application xcelApp = new Microsoft.Office.Interop.Excel.Application();
  7. xcelApp.Application.Workbooks.Add(Type.Missing);
  8.  
  9. for (int i = 1; i < dgv_report.Columns.Count + 1; i++)
  10. {
  11. xcelApp.Cells[1, i] = dgv_report.Columns[i - 1].HeaderText;
  12. }
  13.  
  14. for (int i = 0; i < dgv_report.Rows.Count; i++)
  15. {
  16. for (int j = 0; j < dgv_report.Columns.Count; j++)
  17. {
  18. xcelApp.Cells[i + 2, j + 1] = dgv_report.Rows[i].Cells[j].Value.ToString();
  19. }
  20. }
  21. xcelApp.Columns.AutoFit();
  22. xcelApp.Visible = true;
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement