2
我们在C#中有Excel加载项。为了支持我们使用的一些功能Worksheet.CustomProperties。我们发现在添加一些自定义属性后,我们正在改变它的值。例如,我们通过Add方法设置“1”。在某个时候,我们将相同的值更改为“2”。然后我们保存工作簿并再次打开它。出于某种原因,我们看到“1”而不是“2”。为什么?看起来我错过了一些东西,但不知道是什么。 更新时间:在Excel工作表中保存CustomProperty
public class CustomPropertyUtil
{
protected readonly Worksheet Sheet;
public WorkSheetCustomPropertyUtil(Worksheet sheet)
{
this.Sheet = sheet;
}
protected bool Exists(string propertyName)
{
try
{
return Sheet.CustomProperties.Cast<CustomProperty>().Any(c => c.Name == propertyName);
}
catch (System.Runtime.InteropServices.COMException)
{
return false;
}
}
protected object GetValue(string propertyName)
{
CustomProperty property = GetProperty(propertyName);
return property != null ? property.Value : null;
}
protected void SetValue(string propertyName, object value)
{
CustomProperty customProperty = GetProperty(propertyName);
if (customProperty == null)
{
AddCustomProperty(propertyName, value);
}
else
{
customProperty.Value = value;
}
}
private void AddCustomProperty(string propertyName, object value)
{
Sheet.CustomProperties.Add(propertyName, value);
}
protected CustomProperty GetProperty(string propertyName)
{
return Exists(propertyName)
? Sheet.CustomProperties.Cast<CustomProperty>().FirstOrDefault(c => c.Name == propertyName)
: null;
}
}
这就是我们如何管理自定义属性。当我们保存我们做以下事情:
Workbook.SaveAs(filePath, AccessMode: XlSaveAsAccessMode.xlNoChange);
当我在VBA中打开它时,看到我的CustomProperties没有保存。
没有任何代码我们也没有。如果您寻求任何建议,请张贴更多信息。 – Pav
@Pav你是对的。我添加了代码,但可能还不够。这是同时使用Aspose互操作的传统项目。现在我将尝试在VBA上创建POC来研究这种情况。 – Alezis
我在VBA上的POC没有显示出问题。在我们找到解决方法的地方,但是这个代码必须被重构。如果我有任何结果会在这里分享。 – Alezis