简体   繁体   English

在Excel工作簿中保存自定义数据

[英]Save custom data in Excel workbook

Is it possible to save large amount of data (about 1-2 mb) in Excel workbook? 是否可以在Excel工作簿中保存大量数据(约1-2 MB)?

Ideally, this data should be tied with a worksheet in the workbook. 理想情况下,此数据应与工作簿中的工作表绑定。
CustomProperties are unlikely to support large data. CustomProperties不太可能支持大数据。

My data can be presented in following forms: binary, xml, string. 我的数据可以以下列形式呈现:binary,xml,string。

Thanks... 谢谢...

If your information comes in files, you can read the file in binary mode and put it in a cell. 如果您的信息来自文件,您可以以二进制模式读取文件并将其放在单元格中。 Then in the end or in the beginning you would save your filename so you could easily reconstruct the file. 然后在最后或开头,您将保存文件名,以便您可以轻松地重建文件。 Here is some code for you to begin with: 以下是一些代码供您开头:

Sub readBin()
    Dim iFreeFile As Integer, bTemporary As Byte
    iFreeFile = FreeFile
    Open "\temp.bin" For Binary Access Read As iFreeFile
    Do While Not EOF(intFileNum)
        Get iFreeFile, , bTemporary //read byte from file
        Cells(1, 1) = bTemporary //save in cell
    Loop
    Close iFreeFile
End Sub 

Yes, you could store string and XML in Excel cells. 是的,您可以在Excel单元格中存储字符串和XML。 For binary you'd be better off not saving it inside Excel, but if you had to then OLE (object linking and embedding) could be an option. 对于二进制文件,最好不要将其保存在Excel中,但如果必须,则可以选择OLE(对象链接和嵌入)。 You could do so by saving the binary as a file outside of Excel and then inserting it as a OLE object: 您可以通过将二进制文件保存为Excel外部的文件,然后将其作为OLE对象插入来执行此操作:

Dim mySht As Worksheet
Dim oleFileName as String

oleFile = "MyXmlDoc.xml" 

Set mySht = ActiveWorkbook.ActiveSheet

mySht.Shapes.AddOLEObject Filename:=Environ$("Appdata") & _
     "\MyFolder\" & oleFile, _
         Link:=False, DisplayAsIcon:=True

It worked fine for us for certain types of common filetype. 对于某些类型的常见文件类型,它对我们来说很好。 But we never did it for raw binary data. 但我们从来没有为原始二进制数据做过。 Usually we put a Word Document or PDF in the spreadsheet. 通常我们在电子表格中放置Word文档或PDF。 You also run the risk of possibly corrupting the workbook or losing the binary data. 您还可能存在可能损坏工作簿或丢失二进制数据的风险。 In our case the OLE object would be clicked on by a user that had Wordperfect instead of Word or they ran Linux / Mac and the embedded document wouldn't open. 在我们的例子中,OLE对象将由具有Wordperfect而不是Word的用户单击,或者他们运行Linux / Mac并且嵌入式文档无法打开。

It does make the Excel File get rather large with every embedded object you add. 它确实使您添加的每个嵌入对象的Excel文件变得相当大。 It's an old technology with its quirks. 这是一项古怪的技术。

You could add a VBA module to the workbook for your data and encode your data in normal ASCII strings (for example, using Base64 encoding). 您可以将VBA模块添加到工作簿中以获取数据,并使用常规ASCII字符串对数据进行编码(例如,使用Base64编码)。 Resulting code would look like this: 结果代码如下所示:

Dim x(1000) As String
Sub InitData()
    x(0) = "abcdefghijk...."
    x(1) = "123456789......"
    '...'


End Sub

You can also store these strings in a sheet instead of a VBA module, line-by-line, if you prefer this. 如果您愿意,也可以逐行将这些字符串存储在工作表而不是VBA模块中。

To accomplish the encoding / decoding, look here: 要完成编码/解码,请在此处查看:

How do I base64 encode a string efficiently using Excel VBA? 如何使用Excel VBA有效地对字符串进行base64编码?

Base64 Encode String in VBScript Base64在VBScript中编码字符串

http://www.source-code.biz/snippets/vbasic/12.htm http://www.source-code.biz/snippets/vbasic/12.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM