2014-09-12 113 views
1

我想鏈接一個Cell對象和另一個CellFormat以確定單元格是否被鎖定。獲取CellFormat的單元格

CellFormat對象,它從spreadSheetDocument.workbookPart.WorkbookStylesPart.Stylesheet.CellFormats 集合中獲得。

Cell對象,它從sheetData.Descendants<Row>()集合中獲得。在每一行中都有一個Cell對象。

但問題是:我在哪裏可以得到Cell對象和CellFormat對象之間的關係?

據我所知,CellFormat對象內存在ApplyProtection屬性和Protection對象,但我不知道如何獲取Cell和CellFormat對象之間的關係。

問候。

回答

1

CellStyleIndexCellFormats集合的索引。如您在您的問題中指出的那樣,CellFormats包含ApplyProtectionProtection屬性。

請注意,在Excel中默認情況下,單元格受到保護。這意味着如果AppyProtectiontrue並且Protection屬性爲空,則單元格被鎖定爲

假設你有一個Cell對象,下面應該給你一個細胞是否被鎖定:

//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); 
} 
+0

大,這對我的作品!但我不知道爲什麼有時候Protection Property不爲null或者Protection.Locked可能爲null或者不爲null,非常感謝你的petelids !!!! – ceph 2014-09-15 20:13:36

+0

非常好,很樂意幫忙。 :) – petelids 2014-09-15 20:16:18