Guest User

Untitled

a guest
May 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. if (dataGridView1.Rows.Count > 0)
  4. {
  5. try
  6. {
  7. // Bind Grid Data to Datatable
  8. DataTable dt = new DataTable();
  9. foreach (DataGridViewColumn col in dataGridView1.Columns)
  10. {
  11. dt.Columns.Add(col.HeaderText, col.ValueType);
  12. }
  13. int count = 0;
  14. foreach (DataGridViewRow row in dataGridView1.Rows)
  15. {
  16. if (count < dataGridView1.Rows.Count - 1)
  17. {
  18. dt.Rows.Add();
  19. foreach (DataGridViewCell cell in row.Cells)
  20. {
  21. dt.Rows[dt.Rows.Count - 1][cell.ColumnIndex] = cell.Value.ToString();
  22. }
  23. }
  24. count++;
  25. }
  26. DateTime now = DateTime.Now;
  27. int hour = now.Hour;
  28. int minute = now.Minute;
  29. int second = now.Second;
  30. int month = now.Month;
  31. int day = now.Day;
  32. int year = now.Year;
  33. // Bind table data to Stream Writer to export data to respective folder
  34. StreamWriter wr = new StreamWriter(@"C:UsersadminDownloads\Book1.xls");
  35. // Write Columns to excel file
  36. for (int i = 0; i < dt.Columns.Count; i++)
  37. {
  38. wr.Write(dt.Columns[i].ToString().ToUpper() + "t");
  39. }
  40. wr.WriteLine();
  41. //write rows to excel file
  42. for (int i = 0; i < (dt.Rows.Count); i++)
  43. {
  44. for (int j = 0; j < dt.Columns.Count; j++)
  45. {
  46. if (dt.Rows[i][j] != null)
  47. {
  48. wr.Write(Convert.ToString(dt.Rows[i][j]) + "t");
  49. }
  50. else
  51. {
  52. wr.Write("t");
  53. }
  54. }
  55. wr.WriteLine();
  56. }
  57. wr.Close();
  58. MessageBox.Show("Data Exported Successfully");
  59. }
  60. catch (Exception ex)
  61. {
  62. throw ex;
  63. }
  64. }
  65. else
  66. {
  67. MessageBox.Show("No data found");
  68. }
  69. }
  70.  
  71. StreamWriter wr = new StreamWriter(@"C:UsersadminDownloads\Extension Report Time", hour, " - ", minute, " - ", second".xls");
Add Comment
Please, Sign In to add comment