我们在C#中有Excel in。为了支持一些功能,我们正在使用Worksheet.CustomProperties。我们发现,在添加了一些自定义属性之后,我们正在更改它的值。例如,我们通过Add方法设置"1“。在某些情况下,我们将相同的值更改为"2“。然后我们保存工作簿并再次打开它。由于某种原因,我们看到的是"1“而不是"2”。为什么?看起来我漏掉了什么,但不知道是什么。更新:
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没有被保存。
发布于 2016-09-30 11:09:08
也许我过度简化了您的任务,但我按如下方式设置了自定义属性(例如,将文件保存到Sharepoint文档库时的属性):
Excel.Workbook wb;
wb.ContentTypeProperties["Region"].Value = "Northeast";这些属性可能与您正在讨论的属性不同……这将更改属性,例如,当您单击“文件”选项卡并在最右侧的面板中看到“属性”列表时。
https://stackoverflow.com/questions/39741768
复制相似问题