如何将 ac# 对象附加到 Excel 工作表并在保存工作簿时保存它
我正在用 C# 创建一个 Excel 插件。在加载项中,用户创建“查询”对象,然后执行查询并在 Excel 中显示数据。 我想保存“查询”对象,并能够在给定工作表的情况下获取它,以便对其进行编辑并重新执行查询。
我发现了以下可能性:
public static void SetDocumentProperty(string propertyName, String str)
{
DeleteDocumentProperty(propertyName);
var workbook = Globals.ThisAddIn.GetActiveWorkBook();
workbook.CustomDocumentProperties.Add(propertyName, false, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString, str);
}
它将查询保存为字符串(在序列化对象之后)。 我仍然需要一种将查询连接到工作表的方法,我尝试使用工作表名称 - 问题是工作表名称可能会改变。所以我的问题是:
- 有没有办法获得独特的 工作表的标识符?
- 有没有更好的方法来实现我想要做的事情?
谢谢
I am creating an excel add-in in c#. In the add-in the user creates 'query' objects then the query is performed and data is displayed in excel.
I want to save the 'query' object and to be able to fetch it given a worksheet to enable editing of it and re performing the query.
I have found the following possibility:
public static void SetDocumentProperty(string propertyName, String str)
{
DeleteDocumentProperty(propertyName);
var workbook = Globals.ThisAddIn.GetActiveWorkBook();
workbook.CustomDocumentProperties.Add(propertyName, false, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString, str);
}
which saves the query as a string (after serializing the object).
I still need a way to connect the query to the worksheet, I have tried using the sheet name - the trouble with this is the sheet name might change. so my question is:
- Is there any way of getting a unique
identifier of the worksheet? - Is there a better way of achieving what i am trying to do?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终使用了这个:
这在序列化后将对象附加到工作表的自定义属性。
稍后可以使用以下方式获取它(属性名称为
"query"
):可以使用以下方式删除它:
I ended up using this:
This attaches the object to a custom property of a worksheet after serializing it.
It can later be fetched by using (the name of the property being
"query"
):It can be deleted by using: