Abarna Abi
Abarna Abi

Reputation: 27

Excel to HTML using VB.Net

In a Windows form, I need to export data (Columns A to C) from Excel to HTML.

I am trying as below but getting "Public member 'NamedaRanges' on type 'Worksheet' not found" in namedRanges. Not able to verify after that whether it will save HTML successfully. Please help to solve this.

Private Sub Button_click() 
    xlSheet=xlWB.Worksheets("Sheet3")  
    xlSheet.NamedRanges.SetPrintArea
          (xlSheet.Cells.GetSubrange
                          ("A1",C1")) 
   xlSheet.SaveAs(FileName:="C:\Users\
      Sample.html",FileFormat:=xlHtml)
End Sub

Upvotes: 0

Views: 389

Answers (1)

CDP1802
CDP1802

Reputation: 16189

For non-contiguous ranges you may have to copy to a temporary sheet as a contiguous range and export that

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

        Const folder = "C:\tmp\"
        Const xlFile = "test1.xlsx"
        Const htmlfile = "export.html"

        Dim xls As New Excel.Application
        Dim wb As Workbook
        Dim ws As Worksheet, wsHtml As Worksheet

        wb = xls.Workbooks.Open(folder & xlFile, False, True) ' no link update , radonly
        ws = wb.Worksheets("Sheet3")

        ' create temp sheet, copy range to it, export and then delete it
        wsHtml = wb.Sheets.Add()
        ws.Range("A:A,C:C").Copy(wsHtml.Range("A1"))
        wsHtml.PageSetup.PrintArea = wsHtml.Columns("A:B").Address
        wsHtml.SaveAs(Filename:=folder & htmlfile, FileFormat:=44) ' html

        xls.DisplayAlerts = False
        wsHtml.Delete()
        xls.DisplayAlerts = True

        wb.Close(False)
        xls.Quit()
        MsgBox("Exported to " & folder & htmlfile)

    End Sub

Upvotes: 1

Related Questions