Advertisement
Guest User

0000

a guest
Oct 18th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. private void btnExport_Click(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5.  
  6. Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
  7.  
  8. excel.Application.Workbooks.Add(true);
  9.  
  10. Dataset myDS = new Dataset();
  11.  
  12. System.Data.DataTable table = myDS.Tables[0];
  13. int ColumnIndex = 0;
  14. foreach (System.Data.DataColumn col in table.Columns)
  15. {
  16. ColumnIndex++;
  17. excel.Cells[1, ColumnIndex] = col.ColumnName;
  18. }
  19. int rowIndex = 0;
  20. foreach (DataRow row in table.Rows)
  21. {
  22. rowIndex++;
  23. ColumnIndex = 0; foreach (DataColumn col in table.Columns)
  24. {
  25. ColumnIndex++;
  26. excel.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName];
  27. }
  28. }
  29.  
  30. excel.Visible = true;
  31.  
  32. Microsoft.Office.Interop.Excel._Worksheet worksheet = (Microsoft.Office.Interop.Excel._Worksheet)excel.A ctiveSheet;
  33.  
  34. worksheet.Activate();
  35.  
  36. }
  37. catch (Exception ex)
  38. {
  39. MessageBox.Show(ex.Message, "Export Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement