如何在 Excel 工作簿中存储信息

发布于 2024-07-26 01:31:32 字数 1095 浏览 1 评论 0原文

我正在使用 VS2008 和 VSTO(C#) 在 Office 2007 上编写 Excel 插件。 此插件需要存储嵌入或工作簿内部的信息,以便当用户再次打开包含此类信息的工作簿时恢复某些状态。 在保存工作簿之前,插件会在 XML 中序列化所有信息,打开工作簿后,插件会尝试反序列化此信息(如果找到)。

我尝试使用 Office.DocumentProperties,但每个字符串元素都被截断(最多 256 个字符)

在事件 Excel.AppEvents_WorkbookOpenEventHandlerExcel.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2024-08-02 01:31:32

好吧,我找到了“好方法”。 Office.CustomXMLParts 是存储 XML 数据的集合。

您可以直接在 Msdn1

MSDN 示例中查找信息:

private void AddCustomXmlPartToWorkbook(Excel.Workbook workbook){
string xmlString =
    "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
    "<employees xmlns=\"http://schemas.microsoft.com/vsto/samples\">" +
        "<employee>" +
            "<name>Karina Leal</name>" +
            "<hireDate>1999-04-01</hireDate>" +
            "<title>Manager</title>" +
        "</employee>" +
    "</employees>";
Office.CustomXMLPart employeeXMLPart = workbook.CustomXMLParts.Add(xmlString, missing);
}

OK, I found "the good method". Office.CustomXMLParts is a collection to store XML data.

You can find information directly at Msdn1

MSDN example:

private void AddCustomXmlPartToWorkbook(Excel.Workbook workbook){
string xmlString =
    "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
    "<employees xmlns=\"http://schemas.microsoft.com/vsto/samples\">" +
        "<employee>" +
            "<name>Karina Leal</name>" +
            "<hireDate>1999-04-01</hireDate>" +
            "<title>Manager</title>" +
        "</employee>" +
    "</employees>";
Office.CustomXMLPart employeeXMLPart = workbook.CustomXMLParts.Add(xmlString, missing);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文