开发者

Performance issue with accessing Microsoft.Office.Core.DocumentProperties

I have a Excel COM addin which reads the CustomDocumentProperties section of a workbook.

This is how I access a particular entry from the CustomDocumentProperties section

DocumentProperties docProperties = (DocumentProperties)
                    xlWorkbook.CustomDocumentProperties;

            docProperty = docProperties[propName];

The problem is when the CustomDocumentProperties contain more than 8000 entries, the performance of this code is really bad. I have ran CPU profiler and it showed that the following line takes more than a minute.

docProperty = docProperties[propName];

Does anyone know how to improve the performance of accessi开发者_StackOverflow中文版ng DocumentProperties?

Thanks!


I doubt that there is anything that you could do to improve the performance of the document properties. I believe that it is implemented as a simple list -- not as a dictionary or hash table. In fact, I don't believe that the list is sorted, so with 8000 entries, on average half of them, or 4000, would have to be accessed in order to find the property that you are looking for.

You might consider not using the CustomDocumentProperties as a dictionary. Instead, you might try putting all 8000 of your entries into a custom dictionary, serializing it, and then adding the entire serialized dictionary to the CustomDocumentProperties as a single entry. So to use it, you would access the CustomDocumentProperties, deserialize the dictionary, and then use it repeatedly. When done, if there were any changes to the dictionary, you would have to re-serialize it and save it back to the CustomDocumentProperties, which you would probably only want to do once -- for example, just before saving your workbook. (You might want to put code to re-serialize and save your custom dictionary to the CustomDocumentProperties within the Workbook.BeforeSave event.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜