VSTO: Attach meta-data to a cell in Excel?

前端 未结 5 787
刺人心
刺人心 2021-02-14 05:11

I\'m using VSTO to create an Excel Add-on. This add-on retrieves and display alot of data from a sql-server. This works great, but later on I plan to access some of the data ins

相关标签:
5条回答
  • 2021-02-14 05:41

    You can embed your metadata at the workbook level inside a CustomXMLPart. The customXMLpart could store a serialized dictionary. The dictionary could store cell addresses as keys, and the corresponding metadata as the value.

    Adding a custom xml part: http://msdn.microsoft.com/en-us/library/bb608612.aspx

    Note that this only works for xls and xlsx file formats.

    0 讨论(0)
  • 2021-02-14 05:48

    You might want to look at XML mapping: http://msdn.microsoft.com/en-us/library/aa203737(office.11).aspx

    0 讨论(0)
  • 2021-02-14 05:51

    AMissico listed some very good solutions. I'd like to add one that worked for me:

    In C# (ExcelDNA/VSTO) or VBA you can do something like:

    var app = (Application) ExcelDnaUtil.Application;
    app.ActiveCell.AddComment("This is an editable cell");
    

    The obvious drawback would be that the user can see this comment - in my case it worked out very well because I could provide useful diagnostics to the user, and also parse the same text to get the meta-data I wanted.

    You can also hide the comment using:

    app.DisplayCommentIndicator = XlCommentDisplayMode.xlNoIndicator;
    

    However, note that this hides all comments, not just the one you added. To iterate over comments in a sheet, you can use the following:

    var comments = ((Worksheet)app.ActiveSheet).Comments;
    foreach(Comment comment in comments)
    {
        var text = comment.Text();
        // do something...
    }
    
    0 讨论(0)
  • 2021-02-14 05:52

    There are several way to do this. I do not know your specific requirements, so I will briefly outline some solutions.

    • Create a Named Range, but adding/removing data can affect the defined Named Range if you don't do it right. Sometimes, it is better to define a single cell named range to act as a bookmark then "select range" will get you all the data.

    • Create a Style. Apply this style to each data cell you wish to "find". Define a method that returns a Range base on which cells have the specified style.

    • Create a PivotCache Object. This object has the ability to update itself, and reduces the file size, especially if the cache is used repeatedly in a workbook. It is also one way to work around the limitation in the number of rows in a worksheet.

    • Create a List. This has many advantages. You can add/remove data at will. Add/remove columns. Think of a list as a table.

    • Use XML Mapping (http://msdn.microsoft.com/en-us/library/aa203737(office.11).aspx) as "code4life" mentions.

    • If the workbook is XMLSS, then define a new namespace and adorn the cells with an attribute from the namespace. You can then "query" using XPath. This is a very powerful because you can embed whatever is needed into a workbook.

    Each has its advantages/disadvantages. I have used each solution multiple times.

    0 讨论(0)
  • 2021-02-14 06:07

    I don't recall a way to do exactly what you are asking. What I've seen done in the past was setting range names based on what you might want to look up. Another option is to hide a cell next to it or some other predetermined offset (e.g. always 3 cells to the right, or same position but on a hidden page). The hidden cell/page would have the data you would be looking for.

    One thing that seems to have accidentally emerged as a best practice at the accounting firm I used to work for was that you should push all your data into an "ugly" page that is hidden and use formulas/lookups to refer to your data. That way you can update the "ugly" page and know where data is while having a "pretty page" that the users can monkey up to their hearts' content.

    0 讨论(0)
提交回复
热议问题