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

前端 未结 5 773
刺人心
刺人心 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: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...
    }
    

提交回复
热议问题