• We just launched and are currently in beta. Join us as we build and grow the community.

Creating an Excel Application in VB.NET

LunaticX

Keyboard Warrior
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
75
Likes
121
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, we will create an Excel Application.

Excel is a spreadsheet application developed by Microsoft. Excel features calculation, graphing tools, pivot tables, and a macro programming language. Thus, excel is really important in offices for making reports, request letter, and for some miscellaneous applications.

This tutorial uses Microsoft.

Office

.

Interop

.

Excel

in our Windows Form Application.

Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New, and choose Windows Form Application.

Finally, select OK, then it creates your project and displays following Form1.

Insert a Button control named Button1 in the form.

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.

add_rederence.png


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.

addreference_-_com_0.png


Your design must be look like this:
design_1.png


Double click the code window and choose the Click event of Button1 and write the following code below.

  1. Imports

    Microsoft.

    Office

    .

    Interop

    .

    Excel

  2. Public

    Class

    Form1
  3. Private

    Sub

    Button1_Click(

    ByVal

    sender As

    Object

    , ByVal

    e As

    EventArgs)

    Handles

    Button1.

    Click
  4. Dim

    appXL As

    Application
  5. Dim

    wbXl As

    Workbook
  6. Dim

    shXL As

    Worksheet
  7. Dim

    raXL As

    Range
  8. ' Start Excel and get Application object.
  9. appXL =

    CreateObject

    (

    "Excel.Application"

    )
  10. appXL.

    Visible

    =

    True
  11. ' Add a new workbook.
  12. wbXl =

    appXL.

    Workbooks

    .

    Add
  13. shXL =

    wbXl.

    ActiveSheet
  14. ' Add table headers going cell by cell.
  15. shXL.

    Cells

    (

    1

    , 1

    )

    .

    Value

    =

    "First Name"
  16. shXL.

    Cells

    (

    1

    , 2

    )

    .

    Value

    =

    "Last Name"
  17. shXL.

    Cells

    (

    1

    , 3

    )

    .

    Value

    =

    "Full Name"
  18. shXL.

    Cells

    (

    1

    , 4

    )

    .

    Value

    =

    "Specialization"
  19. ' Format A1:D1 as bold, vertical alignment = center.
  20. With

    shXL.

    Range

    (

    "A1"

    , "D1"

    )
  21. .

    Font

    .

    Bold

    =

    True
  22. .

    VerticalAlignment

    =

    XlVAlign.

    xlVAlignCenter
  23. End

    With
  24. ' Create an array to set multiple values at once.
  25. Dim

    students(

    5

    , 2

    )

    As

    String
  26. students(

    0

    , 0

    )

    =

    "Lyndon"
  27. students(

    0

    , 1

    )

    =

    "Bermoy"
  28. students(

    1

    , 0

    )

    =

    "Novee"
  29. students(

    1

    , 1

    )

    =

    "Dumanig"
  30. students(

    2

    , 0

    )

    =

    "Aga"
  31. students(

    2

    , 1

    )

    =

    "Bermoy"
  32. students(

    3

    , 0

    )

    =

    "Don"
  33. students(

    3

    , 1

    )

    =

    "Bermzkiee"
  34. students(

    4

    , 0

    )

    =

    "Sourcecodester"
  35. students(

    4

    , 1

    )

    =

    "TheBest"
  36. ' Fill A2:B6 with an array of values (First and Last Names).
  37. shXL.

    Range

    (

    "A2"

    , "B6"

    )

    .

    Value

    =

    students
  38. ' Fill C2:C6 with a relative formula (=A2 & " " & B2).
  39. raXL =

    shXL.

    Range

    (

    "C2"

    , "C6"

    )
  40. raXL.

    Formula

    =

    "=A2 & "

    " "

    " & B2"
  41. ' Fill D2:D6 values.
  42. With

    shXL
  43. .

    Cells

    (

    2

    , 4

    )

    .

    Value

    =

    "Programming"
  44. .

    Cells

    (

    3

    , 4

    )

    .

    Value

    =

    "Mechatronics"
  45. .

    Cells

    (

    4

    , 4

    )

    .

    Value

    =

    "Robotics"
  46. .

    Cells

    (

    5

    , 4

    )

    .

    Value

    =

    "Mathmematics"
  47. .

    Cells

    (

    6

    , 4

    )

    .

    Value

    =

    "Best Website"
  48. End

    With
  49. ' AutoFit columns A:D.
  50. raXL =

    shXL.

    Range

    (

    "A1"

    , "D1"

    )
  51. raXL.

    EntireColumn

    .

    AutoFit

    (

    )
  52. ' Make sure Excel is visible and give the user control
  53. ' of Excel's lifetime.
  54. appXL.

    Visible

    =

    True
  55. appXL.

    UserControl

    =

    True
  56. ' Release object references.
  57. raXL =

    Nothing
  58. shXL =

    Nothing
  59. wbXl =

    Nothing
  60. appXL.

    Quit

    (

    )
  61. appXL =

    Nothing
  62. Exit

    Sub
  63. Err_Handler:
  64. MsgBox

    (

    Err.

    Description

    , vbCritical, "Error: "

    &

    Err.

    Number

    )
  65. End

    Sub
  66. End

    Class

Then run the application and click Button1. The excel will display based on the code on your Button1. It will look like this:

excel.png


Then click Save, to save the Excel File.

Download the source code below and try it! :)

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 R. Bermoy

IT Instructor/System Developer/Android Developer

Mobile: 09079373999

Telephone: 826-9296

E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: SerBermz


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top