Can Excel interpret a cell as HTML?

前端 未结 4 946
小鲜肉
小鲜肉 2020-12-05 14:11

I\'m using Aspose.Cells to build an Excel document programmatically. This works great. One of the cells, though, is a block of raw HTML. I\'m wondering if it is possible to

4条回答
  •  情话喂你
    2020-12-05 14:46

    This code worked for me on one cell (inspired by @Rick's answer, but with few changes because Clipboard.SetDataObject(objData) caused error and also objRange.PasteSpecial() didn't work):

    Private Sub Worksheet_Change2(ByVal Target As Range, ByVal sht As Worksheet)
         Dim objData As DataObject 'Set a reference to MS Forms 2.0'
         Dim sHTML As String
         Dim sSelAdd As String
         Application.EnableEvents = False     
         objData = New DataObject
         sHTML = Target.Text
         objData.SetText sHTML
         objData.PutInClipboard
         sht.PasteSpecial Format:="Unicode Text"
         Application.EnableEvents = True
    End Sub
    
    Sub test()
         Dim rng As Range
         Set rng = ActiveSheet.Range("F15") 'cell to change'
         Worksheet_Change2 rng, ActiveSheet 
    End Sub
    

    see this post for some more details

    I guess it shouldn't be too difficult to tweak it a bit that it would work for entire worksheet and not only one specific cell, you should probably add some if condition to wrap this code in order to prevent errors, see this post for some more info

提交回复
热议问题