C# 获取单元格的单元格格式

C# 获取单元格的单元格格式,c#,openxml,C#,Openxml,我试图链接一个单元格对象和另一个单元格格式,以确定单元格是否被锁定 CellFormat对象,它是从spreadSheetDocument.workbookPart.WorkbookStylesPart.Stylesheet.CellFormats中获取的 收藏 单元格对象,它是从sheetData.subjections()集合获取的。每行中都有一个单元格对象 但问题是:我从哪里得到Cell对象和CellFormat对象之间的关系 据我所知,CellFormat对象中存在ApplyProtec

我试图链接一个单元格对象和另一个单元格格式,以确定单元格是否被锁定

CellFormat对象,它是从spreadSheetDocument.workbookPart.WorkbookStylesPart.Stylesheet.CellFormats中获取的 收藏

单元格对象,它是从
sheetData.subjections()集合获取的。每行中都有一个单元格对象

但问题是:我从哪里得到Cell对象和CellFormat对象之间的关系

据我所知,CellFormat对象中存在ApplyProtection属性和保护对象,但我不知道如何获得Cell和CellFormat对象之间的关系


问候。

单元格
样式索引
单元格格式
集合的索引。正如您在问题中指出的,
CellFormats
,包含
ApplyProtection
Protection
属性

请注意,在Excel中,默认情况下单元格受保护。这意味着如果
AppyProtection
true
Protection
属性为空,则单元格被锁定

假设您有一个
单元格
对象,则以下信息将显示单元格是否已锁定:

//check the styleindex isn't null
if (cell.StyleIndex != null)
{
    //get the CellFormat related to this styleindex
    CellFormat cellFormat = (CellFormat)spreadSheetDocument.workbookPart
                            .WorkbookStylesPart.Stylesheet
                            .CellFormats.ChildElements[(int)cell.StyleIndex.Value];

    /* the cell is locked if ApplyProtection is true 
    * and either the Protection object is null OR the the Locked property of the Protection object is true
    */
    bool isLocked = cellFormat.ApplyProtection && (cellFormat.Protection == null || cellFormat.Protection.Locked);
}

太好了,它对我有用!!!但是我不知道为什么有时候保护属性不是null或Protection.Locked可以是null或not null,非常感谢你,皮特!!!!非常好,很乐意帮忙。:)