@@ -498,28 +498,26 @@ private String createSheetProtectionString(Worksheet sheet) {
498498 if (!sheet .isUseSheetProtection ()) {
499499 return "" ;
500500 }
501- HashMap <Worksheet .SheetProtectionValue , Integer > actualLockingValues = new HashMap <>();
502- if (sheet .getSheetProtectionValues ().isEmpty ()) {
503- actualLockingValues .put (Worksheet .SheetProtectionValue .selectLockedCells , 1 );
504- actualLockingValues .put (Worksheet .SheetProtectionValue .selectUnlockedCells , 1 );
505- }
501+ Map <Worksheet .SheetProtectionValue , Integer > actualLockingValues = new HashMap <>();
506502 if (!sheet .getSheetProtectionValues ().contains (Worksheet .SheetProtectionValue .objects )) {
507503 actualLockingValues .put (Worksheet .SheetProtectionValue .objects , 1 );
508504 }
509505 if (!sheet .getSheetProtectionValues ().contains (Worksheet .SheetProtectionValue .scenarios )) {
510506 actualLockingValues .put (Worksheet .SheetProtectionValue .scenarios , 1 );
511507 }
512- if (!sheet .getSheetProtectionValues ().contains (Worksheet .SheetProtectionValue .selectLockedCells )) {
513- if (!actualLockingValues .containsKey (Worksheet .SheetProtectionValue .selectLockedCells )) {
514- actualLockingValues .put (Worksheet .SheetProtectionValue .selectLockedCells , 1 );
515- }
508+ boolean allowSelectLocked = sheet .getSheetProtectionValues ().contains (Worksheet .SheetProtectionValue .selectLockedCells );
509+ boolean allowSelectUnlocked = sheet .getSheetProtectionValues ().contains (Worksheet .SheetProtectionValue .selectUnlockedCells );
510+ if (allowSelectLocked && !allowSelectUnlocked ) {
511+ // This shouldn't happen in Excel's UI, but handle it by allowing both
512+ allowSelectUnlocked = true ;
516513 }
517- if (!sheet . getSheetProtectionValues (). contains ( Worksheet . SheetProtectionValue . selectUnlockedCells ) ||
518- ! sheet . getSheetProtectionValues (). contains ( Worksheet .SheetProtectionValue .selectLockedCells )) {
519- if (! actualLockingValues . containsKey ( Worksheet . SheetProtectionValue . selectUnlockedCells )) {
520- actualLockingValues . put ( Worksheet . SheetProtectionValue . selectUnlockedCells , 1 );
521- }
514+ if (!allowSelectLocked ) {
515+ actualLockingValues . put ( Worksheet .SheetProtectionValue .selectLockedCells , 1 );
516+ }
517+ if (! allowSelectUnlocked ) {
518+ actualLockingValues . put ( Worksheet . SheetProtectionValue . selectUnlockedCells , 1 );
522519 }
520+ // Explicit permissions (set to 0 when allowed)
523521 if (sheet .getSheetProtectionValues ().contains (Worksheet .SheetProtectionValue .formatCells )) {
524522 actualLockingValues .put (Worksheet .SheetProtectionValue .formatCells , 0 );
525523 }
@@ -554,21 +552,17 @@ private String createSheetProtectionString(Worksheet sheet) {
554552 actualLockingValues .put (Worksheet .SheetProtectionValue .pivotTables , 0 );
555553 }
556554 StringBuilder sb = new StringBuilder ();
557- sb .append ("<sheetProtection" );
555+ sb .append ("<sheetProtection sheet=\" 1\" " );
556+
558557 String temp ;
559- Iterator <Map .Entry <Worksheet .SheetProtectionValue , Integer >> itr ;
560- Map .Entry <Worksheet .SheetProtectionValue , Integer > item ;
561- itr = actualLockingValues .entrySet ().iterator ();
562- while (itr .hasNext ()) {
563- item = itr .next ();
558+ for (Map .Entry <Worksheet .SheetProtectionValue , Integer > item : actualLockingValues .entrySet ()) {
564559 temp = item .getKey ().name ();// Note! If the enum names differs from the OOXML definitions, this method will
565- // cause invalid OOXML entries
566- sb .append (" " ).append (temp ).append ("=\" " ).append (item .getValue ()).append ("\" " );
560+ sb .append (" " ).append (temp ).append ("=\" " ).append (item .getValue ().toString ()).append ('"' );
567561 }
568562 if (!Helper .isNullOrEmpty (sheet .getSheetProtectionPasswordHash ())) {
569- sb .append (" password=\" " ).append (sheet .getSheetProtectionPasswordHash ()).append (" \" " );
563+ sb .append (" password=\" " ).append (sheet .getSheetProtectionPasswordHash ()).append ('"' );
570564 }
571- sb .append (" sheet= \" 1 \" />" );
565+ sb .append ("/>" );
572566 return sb .toString ();
573567 }
574568
@@ -939,16 +933,14 @@ else if (style.getCellXf().getVerticalAlign() == CellXf.VerticalAlignValue.top)
939933 sb2 .append ("/>" ); // </xf>
940934 alignmentString = sb2 .toString ();
941935 }
942- if (style .getCellXf ().isHidden () || style .getCellXf ().isLocked ()) {
943- if (style .getCellXf ().isHidden () && style .getCellXf ().isLocked ()) {
944- protectionString = "<protection locked=\" 1\" hidden=\" 1\" />" ;
945- }
946- else if (style .getCellXf ().isHidden () && !style .getCellXf ().isLocked ()) {
947- protectionString = "<protection hidden=\" 1\" locked=\" 0\" />" ;
948- }
949- else {
950- protectionString = "<protection hidden=\" 0\" locked=\" 1\" />" ;
951- }
936+ if (style .getCellXf ().isHidden () && style .getCellXf ().isLocked ()) {
937+ protectionString = "<protection hidden=\" 1\" />" ; // Locked is true by default (no need to define)
938+ }
939+ else if (style .getCellXf ().isHidden () && !style .getCellXf ().isLocked ()) {
940+ protectionString = "<protection hidden=\" 1\" locked=\" 0\" />" ;
941+ }
942+ else if (!style .getCellXf ().isHidden () && !style .getCellXf ().isLocked ()) {
943+ protectionString = "<protection locked=\" 0\" />" ;
952944 }
953945 sb .append ("<xf numFmtId=\" " );
954946 if (style .getNumberFormat ().isCustomFormat ()) {
0 commit comments