Excel 可以将单元格解释为 HTML 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1842071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:28:26  来源:igfitidea点击:

Can Excel interpret a cell as HTML?

htmlexcelasposeaspose-cells

提问by Greg Smalter

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 tell Excel (in any fashion, including the GUI - you don't need to know the Aspose API) to parse and display a cell as HTML. Right now, it just shows up as the raw HTML in text format, tags and all.

我正在使用 Aspose.Cells 以编程方式构建 Excel 文档。这很好用。但是,其中一个单元格是原始 HTML 块。我想知道是否有可能告诉 Excel(以任何方式,包括 GUI - 您不需要知道 Aspose API)将单元格解析并显示为 HTML。现在,它只是以文本格式、标签等形式显示为原始 HTML。

I know Excel is capable of having HTML pasted into it, but it looks like it just parses it on its own and then Excel-ifies it for you, and it doesn't store the HTML, so it's not actually parsing it and displaying it as HTML. Plus, I can't figure out how to replicate this paste functionality anyway.

我知道 Excel 能够将 HTML 粘贴到其中,但它看起来只是自己解析它,然后 Excel 为您对其进行了处理,并且它不存储 HTML,因此它实际上并未对其进行解析和显示作为 HTML。另外,无论如何我无法弄清楚如何复制此粘贴功能。

Thanks.

谢谢。

采纳答案by ScottF

Unfortunately the answer is no.

不幸的是,答案是否定的。

Excel has two HTML options:

Excel 有两个 HTML 选项:

  • Open a HTML file, which will sort of render the HTML, sort of, but won't contain any actual HTML in cells
  • Store HTML in cells, but as unformatted text.
  • 打开一个 HTML 文件,该文件将呈现 HTML,有点,但不会在单元格中包含任何实际的 HTML
  • 将 HTML 存储在单元格中,但作为无格式文本。

You could, maybe possibly, come up with a macro that lets you enter HTML into a cell, then saves that HTML as a document, opens it up in another instance of Excel, then grabs that formatted HTML and places it in the original document; that way you would have two columns, one with the HTML, and one with the output. It would be veryunsightly though. Don't do it :0)

你可能,也许可以,想出一个宏,让你在一个单元格中输入 HTML,然后将该 HTML 保存为一个文档,在另一个 Excel 实例中打开它,然后获取格式化的 HTML 并将其放入原始文档中;这样你就会有两列,一列是 HTML,一列是输出。不过会非常难看。不要这样做:0)

回答by Rick

Pasting html data in excel will result in the html being properly displayed in excel. The one issue with this is that carriage returns and tabs will be pasted to the next cell.

将 html 数据粘贴到 excel 中会导致 html 在 excel 中正确显示。这样做的一个问题是回车和制表符将粘贴到下一个单元格。

Dim objData As New DataObject
objData.SetText(sHTML)
Clipboard.SetDataObject(objData)
objRange.PasteSpecial()

Will fill a cell with properly formated text

将使用格式正确的文本填充单元格

回答by BornToCode

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):

这段代码在一个单元格上对我有用(受到@Rick 的回答的启发,但由于Clipboard.SetDataObject(objData)导致错误并且也objRange.PasteSpecial()没有工作,所以几乎没有变化):

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 postfor 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 postfor some more info

我想稍微调整一下它应该不会太难,它适用于整个工作表,而不仅仅是一个特定的单元格,您可能应该添加一些 if 条件来包装此代码以防止错误,请参阅此帖子以了解一些信息更多信息

回答by StackOverflowFan

I found an interesting YouTube video which shows how to create a simple HTML Interpreter (VBA) in Microsoft Excel using the web browser control. Enter your HTML and CSS code into a text box, and the form will convert the HTML into a web preview.

我发现了一个有趣的 YouTube 视频,它展示了如何使用 Web 浏览器控件在 Microsoft Excel 中创建一个简单的 HTML 解释器 (VBA)。在文本框中输入您的 HTML 和 CSS 代码,表单会将 HTML 转换为 Web 预览。

HTML Interpreter in Microsoft Excel 2010/2007 - Write directly to Web Browser

Microsoft Excel 2010/2007 中的 HTML 解释器 - 直接写入 Web 浏览器