VB.Net will have an excellent feature that is, interoperability which is used to take an external application into the current project.Here we have taken an example excel sheet , following are the steps to import the external items to the projects.
Step-1
Follow the below steps to create an excel sheet.
Open Visual Studio 2013 ultimate.
Create a Windows Application.
Go to File->New->Project as shown in the below figure.
Step-2
Then a new window will be as shown in the following figure. Select the Visual Basic, Windows Application, Give the Name and then click on the OK as shown in the below figure.
Step-3
Then, the project will be created. In the design form drag and drop the Button control as shown in the below figure.
Step-4
Double click on the button control and then automatically event will be raised in the code section.
Note
We have to add the following library in order to work with the Excel Sheets.
Microsoft.Office.Interop.Excel
Step-5
Now, we have to add the reference namespace to our project. Go to Project->Add Reference as shown in the below figure.
Step-6
Then, the following window will be open. Choose the Type Library under the COM and then the select the Class Library as shown in the below figure and click on Ok.
Step-7
Then you will get a reference to excel library.Now write the following code.
[vbnet]
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO
Public Class Form1
Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
Dim FileTest As String = "C:\Windows\Temp\Excel Test\Test.xlsx"
If File.Exists(FileTest) Then
File.Delete(FileTest)
End If
Dim ExcelObj As Object
ExcelObj = CreateObject("Excel.Application")
Dim BookObj As Excel.Workbook
Dim SheetObj As Excel.Worksheet
BookObj = ExcelObj.Workbook.Add
SheetObj = ExcelObj.WorkSheet(1)
SheetObj.Name = "Sample"
SheetObj.Range("A1").Value = "Some Value"
BookObj.SaveAs("FileTest")
BookObj.Close()
BookObj = Nothing
ExcelObj.Quit()
ExcelObj = Nothing
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
[/vbnet]
Step-8
Press f5 to run the application. Then the following output will be displayed. Click on the button.
Step-9
Click on the button. Then the Excel Sheet will be created as shown below.
Programming
Tips
Interoperability will be happening in the component object model and selected external application.
The selected application package needs to be imported into the code to hire the properties.