Creating an Excel Application in C#

This is a tutorial wherein we will going to create a program that creates an excel application using C#. This tutorial uses Microsoft.Office.Interop.Excel in our Windows Form Application. 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New, and choose Windows Form Application. 2. Insert a Button control named Button1 in the form. Design must be like this: design 3. Add a reference to Microsoft Excel Object Library to your project. To do this follow the image below. Note: this is really an important library to add an excel file. On the COM tab, locate Microsoft Excel Object Library and then click Select Microsoft Excel 14.0 Object Library. Then Click OK. Follow this image below. On the COM tab, locate Microsoft Excel Object Library and then click Select Microsoft Excel 14.0 Object Library. Then Click OK. Follow this image below. design 4. Put the following code in your code view:
  1. using Microsoft.Office.Interop.Excel;
  2. using System.Windows.Forms;
  3.  
  4.  
  5.  
  6.  
  7. public class Form1
  8. {
  9. private void Button1_Click(object sender, EventArgs e)
  10. {
  11. Application appXL = default(Application);
  12. Workbook wbXl = default(Workbook);
  13. Worksheet shXL = default(Worksheet);
  14. Range raXL = default(Range);
  15. // Start Excel and get Application object.
  16. appXL = CreateObject("Excel.Application");
  17. appXL.Visible = true;
  18. // Add a new workbook.
  19. wbXl = appXL.Workbooks.Add;
  20. shXL = wbXl.ActiveSheet;
  21. // Add table headers going cell by cell.
  22. shXL.Cells(1, 1).Value = "First Name";
  23. shXL.Cells(1, 2).Value = "Last Name";
  24. shXL.Cells(1, 3).Value = "Full Name";
  25. shXL.Cells(1, 4).Value = "Specialization";
  26. // Format A1:D1 as bold, vertical alignment = center.
  27. object with_1 = shXL.Range("A1", "D1");
  28. with_1.Font.Bold = true;
  29. with_1.VerticalAlignment = XlVAlign.xlVAlignCenter;
  30. // Create an array to set multiple values at once.
  31. string[,] students = new string[6, 3];
  32. students[0, 0] = "Lyndon";
  33. students[0, 1] = "Bermoy";
  34. students[1, 0] = "Novee";
  35. students[1, 1] = "Dumanig";
  36. students[2, 0] = "Aga";
  37. students[2, 1] = "Bermoy";
  38. students[3, 0] = "Don";
  39. students[3, 1] = "Bermzkiee";
  40. students[4, 0] = "Sourcecodester";
  41. students[4, 1] = "TheBest";
  42. // Fill A2:B6 with an array of values (First and Last Names).
  43. shXL.Range("A2", "B6").Value = students;
  44. // Fill C2:C6 with a relative formula (=A2 & " " & B2).
  45. raXL = shXL.Range("C2", "C6");
  46. raXL.Formula = "=A2 & \" \" & B2";
  47. // Fill D2:D6 values.
  48. shXL.Cells(2, 4).Value = "Programming";
  49. shXL.Cells(3, 4).Value = "Mechatronics";
  50. shXL.Cells(4, 4).Value = "Robotics";
  51. shXL.Cells(5, 4).Value = "Mathmematics";
  52. shXL.Cells(6, 4).Value = "Best Website";
  53. // AutoFit columns A:D.
  54. raXL = shXL.Range("A1", "D1");
  55. raXL.EntireColumn.AutoFit();
  56. // Make sure Excel is visible and give the user control
  57. // of Excel's lifetime.
  58. appXL.Visible = true;
  59. appXL.UserControl = true;
  60. // Release object references.
  61. raXL = null;
  62. shXL = null;
  63. wbXl = null;
  64. appXL.Quit();
  65. appXL = null;
  66. return;
  67. Err_Handler:
  68. MsgBox(Err.Description, vbCritical, "Error: " + Err.Number);
  69. }
  70. }
Output: output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment