ALTERNATE WAY TO DISPLAY EXCEL SHEET INSIDE VB FORM

Submitted by techrev on Sat, 01/20/2018 - 06:28
excel inside vb form

Step 1 :- CONVERT EXCEL FILE TO HTML FILE

Step 2 :- DISPLAY INSIDE WEB-BROWSER

 

Required Libraries

Imports System
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Core
Imports System.IO
Imports Excel = Microsoft.Office.Interop.Excel
 

Create an menu item/ button, and paste below code

 

        Dim curFile3 As String = "E:\Excel-in-VB\copy\excel-vb.xlsx"
        Dim curFile4 As String = "E:\Excel-in-VB\excel-vb.html"
        If (File.Exists(curFile4)) Then

        My.Computer.FileSystem.DeleteFile("E:\Excel-in-VB\excel-vb.html")

        End If
        If (File.Exists(curFile3)) Then

        My.Computer.FileSystem.DeleteFile("E:\Excel-in-VB\copy\excel-vb.xlsx")

       End If
       My.Computer.FileSystem.CopyFile("E:\Excel-in-VB\excel-vb.xlsx", "E:\Excel-in-VB\copy\excel-vb.xlsx")

        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet

        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Open("E:\Excel-in-VB\copy\excel-vb.xlsx")
        xlWorkSheet = xlWorkBook.Worksheets("Sheet1")

        xlWorkBook.Saved = True
        xlWorkBook.SaveAs(Filename:="E:\Excel-in-VB\excel-vb.html", FileFormat:=Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml)
        xlWorkBook.Saved = True
        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        WebBrowser1.Navigate("E:\Excel-in-VB\excel-vb.html")

 

add

Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
End Sub

 

Create an excel file excel-vb.xlsx inside E drive (change according to your PC, or insert a fileopencontrolbox)

create a 'copy' folder inside E drive

Save and Run

Tags