簡體   English   中英

在 Excel 工作表中保存 CustomProperty

[英]Saving CustomProperty in Excel Worksheet

我們在 C# 中有 Excel 加載項。為了支持我們使用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 沒有保存。

也許我簡化了您的任務,但是我將自定義屬性(例如,將文件保存到Sharepoint文檔庫時的屬性)設置如下:

Excel.Workbook wb;

wb.ContentTypeProperties["Region"].Value = "Northeast";

這些屬性可能與您所討論的屬性不同。例如,當您單擊“文件”選項卡並在最右側面板中看到“屬性”列表時,這將更改屬性。

這個答案可能晚了六年,但這里有一些示例代碼,用於處理 WorkSheet 對象的CustomPropertiesCustomProperty ,包括 function 以通過.Name屬性獲取它們。

我使用的是 Excel 2019,其中的CustomProperties集合和任何CustomProperty對象確實與工作簿一起保存。 我只是通過保存、關閉和重新打開來對此進行測試,並且CustomProperty值被保留。

Public Sub CustomPropertyTest()
    
    Dim xl          As Excel.Application
    Dim ws          As Excel.Worksheet
    Dim wb          As Excel.Workbook
    Dim cps         As Excel.CustomProperties
    Dim cp          As Excel.CustomProperty
    
    Set xl = Excel.Application
    Set wb = xl.ActiveWorkbook
    Set ws = wb.ActiveSheet
    Set cps = ws.CustomProperties
    
    If CustomPropertyByName("Test1") Is Nothing Then
        Set cp = cps.Add("Test1", "Hello!")
    End If
    If CustomPropertyByName("Test2", ws) Is Nothing Then
        Set cp = cps.Add("Test2", "Hello again!")
    End If
    
    Set cp = CustomPropertyByName("Test1", ws)
    'This can also be used if you're certain of the item index.
    'Set cp = cps.Item(1)
        
    Stop
    
    'Change the value.
    cp.Value = "2"
    
    Stop
    
    'Change the value again.
    cp.Value = "3"
   
    Stop
    
End Sub

Public Function CustomPropertyByName( _
                    ByVal cpName As String, _
                    Optional ByRef wsObj As Excel.Worksheet _
                    ) As CustomProperty
                    
    Dim cps         As Excel.CustomProperties
    Dim cp          As Excel.CustomProperty
    
    If wsObj Is Nothing Then
        Set wsObj = Excel.ActiveSheet
    End If
    
    Set cps = wsObj.CustomProperties
    For Each cp In cps
        If cp.Name = cpName Then
            Set CustomPropertyByName = cp
            Exit Function
        End If
    Next
    
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM