18, Feb, 2019

DISPLAY EXCEL SHEET INSIDE VB FORM

DISPLAY EXCEL SHEET 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

Save and Run

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>