简体   繁体   中英

Export Excel values to HTML using VB.Net

I have a Windows Forms with button click operation.

On button click the specified columns in excel should be exported to html.

I used "SaveAs" method. But not able to open the file format in any browser. Please help me how to export specific columns('B','D') to hmtl with cell color.

Dim xlApp As Excel.Application

Dim xlWB As Excel.WorkBook

Dim xlSH As Excel.WorkSheet

Dim str_File As String = "C:\Sample.xlsx"

xlApp = New Excel.Application

xlWB = xlApp.Workbooks.Open(str_File)

xlSH = xlWB.WorkSheets("merge")

xlSH.SaveAs("C:\Sample.html")

Just add the FileFormat paramater to the .SaveAs method and Bob's your uncle. I also closed and quit the Workbook and Application respectively. I added the GC code to the calling method to get rid of ghost Excel when debugging.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    OpCode()
    GC.Collect()
    GC.WaitForPendingFinalizers()
End Sub

Private Sub OpCode()
    Dim str_File As String = "C:\Users\xxx\Documents\Excel\Politics\2020 Election.xlsx"
    Dim xlApp As New Excel.Application
    Dim xlWB As Excel.Workbook = xlApp.Workbooks.Open(str_File)
    Dim xlSH As Excel.Worksheet = CType(xlWB.Worksheets(1), Excel.Worksheet)
    xlSH.SaveAs("C:\Users\xxx\Documents\Excel\Sample.html", Excel.XlFileFormat.xlHtml)
    xlWB.Close()
    xlApp.Quit()
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM