如何在 Excel 工作簿中存储信息
我正在使用 VS2008 和 VSTO(C#) 在 Office 2007 上编写 Excel 插件。 此插件需要存储嵌入或工作簿内部的信息,以便当用户再次打开包含此类信息的工作簿时恢复某些状态。 在保存工作簿之前,插件会在 XML 中序列化所有信息,打开工作簿后,插件会尝试反序列化此信息(如果找到)。
我尝试使用 Office.DocumentProperties,但每个字符串元素都被截断(最多 256 个字符)
在事件 Excel.AppEvents_WorkbookOpenEventHandler 和 Excel.AppEvents_WorkbookBeforeCloseEventHandler :
Office.DocumentProperties properties = (Office.DocumentProperties)this.Application.ActiveWorkbook.CustomDocumentProperties;
properties.Add(Constants.ADDIN_PERSISTENCE, false, Office.MsoDocProperties.msoPropertyTypeString, serialize(configurationInstance), null);
在事件 AppEvents_WorkbookOpenEventHandler 处:
Office.DocumentProperties properties = (Office.DocumentProperties)Application.ActiveWorkbook.CustomDocumentProperties;
Configuration configurationInstance = deserialize((string)properties[Constants.ADDIN_PERSISTENCE]);
但这不起作用,因为序列化返回的字符串长度超过 256 个字符。
在 Excel 2003 上的其他插件中,我将信息存储在具有奇怪名称的“非常隐藏”工作表的第一个单元格中。 不知道有没有更好的解决方案。
I'm programing an Excel Addin with VS2008 and VSTO(C#) over Office 2007.
This addin needs to store information embedded or inside the Workbook to recover some status when the user opens again the Workbook with this kind of information. Before save the Workbook, the Addin serialize in XML all the information, and after open a WorkBook the Addin try to deserialize this information if it found it.
I tried to use the Office.DocumentProperties but each string element is truncated (max.256 chars)
At events Excel.AppEvents_WorkbookOpenEventHandler and Excel.AppEvents_WorkbookBeforeCloseEventHandler :
Office.DocumentProperties properties = (Office.DocumentProperties)this.Application.ActiveWorkbook.CustomDocumentProperties;
properties.Add(Constants.ADDIN_PERSISTENCE, false, Office.MsoDocProperties.msoPropertyTypeString, serialize(configurationInstance), null);
At event AppEvents_WorkbookOpenEventHandler:
Office.DocumentProperties properties = (Office.DocumentProperties)Application.ActiveWorkbook.CustomDocumentProperties;
Configuration configurationInstance = deserialize((string)properties[Constants.ADDIN_PERSISTENCE]);
But this doesn't work because the serialization returns a string longer than 256 chars.
In other Addin, over Excel 2003, I store the information in the first cell on a "veryhidden" sheet with a strange name. I don't know if there are better solutions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我找到了“好方法”。 Office.CustomXMLParts 是存储 XML 数据的集合。
您可以直接在 Msdn1
MSDN 示例中查找信息:
OK, I found "the good method". Office.CustomXMLParts is a collection to store XML data.
You can find information directly at Msdn1
MSDN example: