刚开始学习VBA的时候,保存自定义数据用的隐藏工作表;后来学了VSTO,把自定义数据保存到
除了以上几种保存方式,还可以保存为JSON格式,或者直接在xlsx文件中写入
直接存储到文档使用的是CustomDocumentProperties属性,Excel参考文档链接:https://docs.microsoft.com/zh-cn/office/vba/api/excel.workbook.customdocumentproperties
以下是C# 代码
/// <summary>/// 写入自定义字符串/// </summary>/// <param name="WBK">工作簿</param>/// <param name="ValueStr">对应值</param>/// <param name="NameStr">名称</param>public void WriteDocumentProperties(Excel.Workbook WBK,string ValueStr,string NameStr){ Microsoft.Office.Core.DocumentProperties properties; properties = (Microsoft.Office.Core.DocumentProperties)WBK.CustomDocumentProperties; if (ReadDocumentProperty(WBK,NameStr) != null) { properties[NameStr].Delete(); } properties.Add(NameStr, false,Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString,ValueStr);}/// <summary>/// 读取自定义字符串/// </summary>/// <param name="WBK">工作簿</param>/// <param name="NameStr">名称</param>/// <returns>读取不到时返回null</returns>public string ReadDocumentProperty(Excel.Workbook WBK,string NameStr){ Microsoft.Office.Core.DocumentProperties properties; properties = (Microsoft.Office.Core.DocumentProperties)WBK.CustomDocumentProperties; foreach (Microsoft.Office.Core.DocumentProperty prop in properties) { if (prop.Name == NameStr) { return prop.Value.ToString(); } } return null;}
VSTO保存自定义数据到工作簿
没有评论:
发表评论