How to Create an Excel File Using Visual Basic.Net

0
431

Today I will teach you about How to Create an Excel File Using Visual Basic.Net. Create an Excel File Using Visual Basic.Net provides support for interoperability between the COM object model of Microsoft Excel and your application. So let’s get started:

  • First is open the Visual Basic, Select File on the menu, then click New and create a new project.

  • Then a New Project Dialog will appear. You can rename your project, depending on what you like to name it. After that click OK

  • Then design your form like this just like what I’ve shown you below
    Add a Button from the toolbox.

  • After that, Add a Reference to Microsoft Excel Object Library to your project.

To do this:

*Select add reference from the project menu.

*On the COM tab, Locate Microsoft Excel Object Library and then click select.

*After that, Click OK

  • Double click the code window and add this following code to the top of Public Class Form1
  1. Imports Excel = Microsoft.Office.Interop.Excel
  • Then, Go back to Design View and Double click the Button and add this following code.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Dim appXL As Excel.Application
  3. Dim wbXl As Excel.Workbook
  4. Dim shXL As Excel.Worksheet
  5. Dim raXL As Excel.Range
  6. appXL = CreateObject("Excel.Application")
  7. appXL.Visible = True
  8. wbXl = appXL.Workbooks.Add
  9. shXL = wbXl.ActiveSheet
  10.  
  11. shXL.Cells(1, 1).Value = "FIRST NAME"
  12. shXL.Cells(1, 2).Value = "LAST NAME"
  13. shXL.Cells(1, 3).Value = "FULL NAME"
  14. shXL.Cells(1, 4).Value = "SUBJECTS"
  15.  
  16. With shXL.Range("A1", "D1")
  17. .Font.Bold = True
  18. .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
  19. End With
  20.  
  21. Dim pupils(5, 2) As String
  22. pupils(0, 0) = "Markfil"
  23. pupils(0, 1) = "Baldomero"
  24. pupils(1, 0) = "Juvy"
  25. pupils(1, 1) = "Aral"
  26. pupils(2, 0) = "Andrew"
  27. pupils(2, 1) = "Braza"
  28. pupils(3, 0) = "Carlie"
  29. pupils(3, 1) = "Golez"
  30. pupils(4, 0) = "Jesriel"
  31. pupils(4, 1) = "Agita"
  32.  
  33. shXL.Range("A2", "B6").Value = pupils
  34.  
  35. raXL = shXL.Range("C2", "C6")
  36. raXL.Formula = "=A2 & "" "" & B2"
  37.  
  38. With shXL
  39. .Cells(2, 4).Value = "English"
  40. .Cells(3, 4).Value = "Algebra"
  41. .Cells(4, 4).Value = "Physics"
  42. .Cells(5, 4).Value = "Trigonometry"
  43. .Cells(6, 4).Value = "Theology"
  44. End With
  45.  
  46. raXL = shXL.Range("A1", "D1")
  47. raXL.EntireColumn.AutoFit()
  48.  
  49. appXL.Visible = True
  50. appXL.UserControl = True
  51.  
  52. raXL = Nothing
  53. shXL = Nothing
  54. wbXl = Nothing
  55. appXL.Quit()
  56. appXL = Nothing
  57. Exit Sub
  58. Err_Handler:
  59. MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
  60. End Sub
  61.  
  62.  
  • Finally, Click F5 to run the program.

If you have any questions or suggestions about How to Create an Excel File Using Visual Basic.Net please contact me through our contact page.

Our Previous topics: How to Read an Excel File using Visual basic.Net

 

Download How to Create an Excel File Using Visual Basic.Net Source code Here

 

Readers might read also:

 

 

 

 

Facebook Comments
(Visited 472 times, 2 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here