【问题标题】:Performance issue with accessing Microsoft.Office.Core.DocumentProperties访问 Microsoft.Office.Core.DocumentProperties 的性能问题
【发布时间】:2010-01-21 15:00:58
【问题描述】:

我有一个 Excel COM 插件,它读取工作簿的 CustomDocumentProperties 部分。

这是我从 CustomDocumentProperties 部分访问特定条目的方式

DocumentProperties docProperties = (DocumentProperties)
                    xlWorkbook.CustomDocumentProperties;

            docProperty = docProperties[propName];

问题是当 CustomDocumentProperties 包含超过 8000 个条目时,这 代码真的很糟糕。我运行了 CPU 分析器,它显示以下行需要一分钟以上。

docProperty = docProperties[propName];

有谁知道如何提高访问 DocumentProperties 的性能?

谢谢!

【问题讨论】:

    标签: c# performance excel office-interop


    【解决方案1】:

    我怀疑您可以做些什么来提高文档属性的性能。我相信它是作为一个简单的列表实现的——而不是作为字典或哈希表。事实上,我不认为该列表已排序,因此如果有 8000 个条目,则平均需要访问其中的一半或 4000 个条目才能找到您要查找的属性。

    您可能会考虑不将 CustomDocumentProperties 用作字典。相反,您可以尝试将所有 8000 个条目放入自定义字典中,对其进行序列化,然后将整个序列化字典作为单个条目添加到 CustomDocumentProperties。因此,要使用它,您将访问 CustomDocumentProperties,反序列化字典,然后重复使用它。完成后,如果字典有任何更改,您将不得不重新序列化它并将其保存回 CustomDocumentProperties,您可能只想执行一次 - 例如,就在保存工作簿之前。 (您可能希望将代码重新序列化并将自定义字典保存到 Workbook.BeforeSave 事件中的 CustomDocumentProperties。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-28
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      相关资源
      最近更新 更多