【问题标题】:Import to excel from text file, with styling从文本文件导入到excel,带有样式
【发布时间】:2016-11-16 19:54:08
【问题描述】:

我有一个保存文本文件的程序,我想做的是在这些文本中添加一些基本样式,然后在 excel 中打开它们。

我的文件通常如下所示:

Title of text

lorem ipsum dolor sit amet

我可以在这个文本文件中写些什么,在 excel 中打开时使“文本标题粗体

就像 html 标记一样

<b>Title of text</b>

lorem ipsum dolor sit amet

excel有什么类似的吗?

【问题讨论】:

  • 您可以在 excel 中添加样式是的,但您是尝试将格式存储在保存的文本文件中,还是在打开文本的 excel 文件中?
  • @TimWilkinson 我想将格式存储在文本文件中,然后在 excel 中打开它,所以第一次在 excel 中打开时,标题将是粗体
  • 如果您将工作表保存为.txt 或任何其他原始文本格式,您将无法实现这一点。例如,您必须导出为 HTML 或 word doc 以保留您添加的任何样式。
  • @TimWilkinson 我也很怀疑,如果你回答我会接受(在一两天内,必须保持希望)。因为如果这是真的,这是我唯一能接受的答案

标签: excel text


【解决方案1】:

您可以使用以下代码。此代码将格式化A1:A13 范围内的文本。您可以根据需要选择要格式化的范围。只需将此代码复制到 Visual Basic 编辑器,然后使用 Run Sub 按钮运行 sub。

Sub Macro1()

Dim str As String
Dim nBold As Long
Dim nEndBold As Long
Dim nChars As Long
Set Rng = Range("A1:A13")

For Each cell In Rng
    str = cell.Text
    nBold = InStr(str, "<b>")
    If nBold > 0 Then
        nEndBold = InStr(str, "</b>")
    If nEndBold = 0 Then nEndBold = 32767
        nChars = nEndBold - nBold - 3
        str = Replace(Replace(str, "<b>", ""), "</b>", "")
        cell.Value = str
        cell.Characters(nBold, nChars).Font.Bold = True
    End If
Next cell

End Sub

查看this link了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    相关资源
    最近更新 更多