如何将大(最大10Mb)文本文件嵌入到Excel文件中

时间:2015-07-08 17:56:30

标签: excel vba excel-vba

在Excel文件中存储大文本文件(最大10Mb)的最佳方法是什么?

我有几个要求:

  1. 必须嵌入它才能移动excel文件并将其发送到另一台计算机,所有文本文件都会跟随。

  2. 需要从宏开始。

  3. 宏嵌入后需要能够读取文件内容。

  4. 我已经尝试通过将文本分成足够小的几个块以适合单元格(~32 000个字符)来存储它,但它不起作用。在我的宏插入前150,000个字符后,它给了我一个“Out of Memory”错误。

    我记得看到一个网页上有几个选项,但我找不到它了。任何建议都是最受欢迎的。如果你不确定它是否有效,我会尝试一下。

3 个答案:

答案 0 :(得分:3)

最好只将.txt文件与Excel文件一起保存,然后让宏根据需要从该文件夹中提取文本。要阅读有关导入文件的更多信息,请参阅:

http://answers.microsoft.com/en-us/office/forum/office_2010-customize/vba-code-to-import-multiple-text-files-from/525bd388-0f7d-4b4a-89f9-310c67227458

将.txt保存在Excel文件本身并不是必需的,并且可能会使长期传输文件变得更加困难。例如,如果您无法通过电子邮件发送大于10MB的文件,那么您可以简单地将.txt文件分成两半并单独发送电子邮件 - 使用宏将文本加载到本地Excel中。

答案 1 :(得分:2)

非常简单的CustomXMLPart示例:

Sub CustomTextTester()

    Dim cxp1 As CustomXMLPart, cxp2 As CustomXMLPart
    Dim txt As String

    'read file content
    txt = CreateObject("scripting.filesystemobject").opentextfile( _
                                      "C:\_Stuff\test.txt").readall()

    'Add a custom XML part with that content
    Set cxp1 = ThisWorkbook.CustomXMLParts.Add("<myXMLPart><content><![CDATA[" & txt _
                                               & "]]></content></myXMLPart>")

    Debug.Print cxp1.SelectSingleNode("myXMLPart/content").FirstChild.NodeValue

End Sub

答案 2 :(得分:1)

考虑下面显示的方法。它使用位于工作表上的Caption对象的Label属性进行数据存储。因此,您可以创建许多具有不同名称的此类容器。

Sub Test()
    Dim sText
    ' create special hidden sheet for data storage
    If Not IsSheetExists("storage") Then
        With ThisWorkbook.Worksheets.Add()
            .Name = "storage"
            .Visible = xlVeryHidden
        End With
    End If
    ' create new OLE object TypeForms.Label type as container
    AddContainer "test_container_"
    ' read text from file
    sText = ReadTextFile("C:\Users\DELL\Desktop\tmp\tmp.txt", 0)
    ' put text into container
    PutContent "test_container_", sText
    ' retrieve text from container
    sText = GetContent("test_container_")
    ' show length
    MsgBox Len(sText)
    ' remove container
    RemoveContainer "test_container_"
End Sub

Function IsSheetExists(sSheetName)
    Dim oSheet
    For Each oSheet In ThisWorkbook.Sheets
        If oSheet.Name = sSheetName Then
            IsSheetExists = True
            Exit Function
        End If
    Next
    IsSheetExists = False
End Function

Sub AddContainer(sName)
    With ThisWorkbook.Sheets("storage").OLEObjects.Add(ClassType:="Forms.Label.1")
        .Visible = False
        .Name = sName
    End With
End Sub

Sub RemoveContainer(sName)
    ThisWorkbook.Sheets("storage").OLEObjects.Item(sName).Delete
End Sub

Sub PutContent(sName, sContent)
    ThisWorkbook.Sheets("storage").OLEObjects.Item(sName).Object.Caption = sContent
End Sub

Function GetContent(sName)
    GetContent = ThisWorkbook.Sheets("storage").OLEObjects.Item(sName).Object.Caption
End Function

Function ReadTextFile(sPath, iFormat)
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 1, False, iFormat)
        ReadTextFile = ""
        If Not .AtEndOfStream Then ReadTextFile = .ReadAll
        .Close
    End With
End Function