How to Control Microsoft Excel 2007 workbook and write contents from Visual Basic.Net


How to Open an Excel workbook and write contents from Visual Basic.Net. We need to add two references to our project. First one is Microsoft Excel 12.0 Object library and second one is Microsoft.Office.interop.

In this article I am going to show you how you can control Microsoft Excel 2007 from Visual BAsic.Net.
How to Open an Excel workbook and write contents from Visual Basic.Net. We need to add two references to our project. First one is Microsoft Excel 12.0 Object library and second one is Microsoft.Office.interop.
Now, Add two Buttons on the form and name them btnshow and btnclose as shown in the image.
Now, Add the following code on the click events of the btnshow button.


Private Sub btnshow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnshow.Click
Dim MyExcel As New Excel.Application
MyExcel.Workbooks.Add()


Dim x As Integer = 0
MyExcel.Range("B1").Activate()

Do
MyExcel.ActiveCell.Value = x
MyExcel.ActiveCell.Offset(1, 0).Activate()
x += 1
Loop Until x = 10
MyExcel.Visible = True
MyExcel.Visible = True
End Sub


And Add the following code on the click events of the btnclose button.This is to close the Excel Process otherwise the process will be executing even if we close the excel.

Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
Dim MyExcel As New Excel.Application
MyExcel.Workbooks.Close()
MyExcel = Nothing
End Sub


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: