@@ -117,6 +117,42 @@ public string Name
117
117
sheet . Name = value ;
118
118
}
119
119
}
120
+
121
+ /// <summary>
122
+ /// Gets the row index where the pane is frozen in the worksheet.
123
+ /// Returns 0 if no rows are frozen.
124
+ /// </summary>
125
+ /// <remarks>
126
+ /// This property retrieves the value of <see cref="VerticalSplit"/> from the Pane element in the SheetView.
127
+ /// </remarks>
128
+ public int FreezePanesRow
129
+ {
130
+ get
131
+ {
132
+ var pane = _worksheetPart . Worksheet . GetFirstChild < SheetViews > ( )
133
+ ? . Elements < SheetView > ( ) . FirstOrDefault ( )
134
+ ? . Elements < Pane > ( ) . FirstOrDefault ( ) ;
135
+ return pane != null ? ( int ) pane . VerticalSplit . Value : 0 ;
136
+ }
137
+ }
138
+
139
+ /// <summary>
140
+ /// Gets the column index where the pane is frozen in the worksheet.
141
+ /// Returns 0 if no columns are frozen.
142
+ /// </summary>
143
+ /// <remarks>
144
+ /// This property retrieves the value of <see cref="HorizontalSplit"/> from the Pane element in the SheetView.
145
+ /// </remarks>
146
+ public int FreezePanesColumn
147
+ {
148
+ get
149
+ {
150
+ var pane = _worksheetPart . Worksheet . GetFirstChild < SheetViews > ( )
151
+ ? . Elements < SheetView > ( ) . FirstOrDefault ( )
152
+ ? . Elements < Pane > ( ) . FirstOrDefault ( ) ;
153
+ return pane != null ? ( int ) pane . HorizontalSplit . Value : 0 ;
154
+ }
155
+ }
120
156
121
157
122
158
/// <summary>
@@ -1049,12 +1085,19 @@ public List<uint> GetHiddenColumns()
1049
1085
return itemList ;
1050
1086
}
1051
1087
1052
-
1053
1088
/// <summary>
1054
- /// Freezes the top row of the worksheet.
1089
+ /// Freezes the specified rows and/or columns of the worksheet.
1055
1090
/// </summary>
1056
- public void FreezeFirstRow ( )
1091
+ /// <param name="rowsToFreeze">The number of rows to freeze. Set to 0 if no row freezing is needed.</param>
1092
+ /// <param name="columnsToFreeze">The number of columns to freeze. Set to 0 if no column freezing is needed.</param>
1093
+ public void FreezePane ( int rowsToFreeze , int columnsToFreeze )
1057
1094
{
1095
+ // Ensure we are freezing at least one row or column
1096
+ if ( rowsToFreeze == 0 && columnsToFreeze == 0 )
1097
+ {
1098
+ return ; // No freeze needed, exit the method.
1099
+ }
1100
+
1058
1101
// Retrieve or create the SheetViews element
1059
1102
SheetViews sheetViews = _worksheetPart . Worksheet . GetFirstChild < SheetViews > ( ) ;
1060
1103
@@ -1080,24 +1123,42 @@ public void FreezeFirstRow()
1080
1123
existingPane . Remove ( ) ;
1081
1124
}
1082
1125
1083
- // Define freeze pane settings for freezing the top row
1126
+ // Calculate the top left cell after the freeze
1127
+ string topLeftCell = GetTopLeftCell ( rowsToFreeze , columnsToFreeze ) ;
1128
+
1129
+ // Define freeze pane settings dynamically based on the rows and columns to freeze
1084
1130
Pane pane = new Pane
1085
1131
{
1086
- VerticalSplit = 1D , // Split below the first row
1087
- TopLeftCell = "A2" , // Top left cell after freeze
1088
- ActivePane = PaneValues . BottomLeft ,
1132
+ VerticalSplit = rowsToFreeze > 0 ? ( double ) rowsToFreeze : 0D ,
1133
+ HorizontalSplit = columnsToFreeze > 0 ? ( double ) columnsToFreeze : 0D ,
1134
+ TopLeftCell = topLeftCell ,
1135
+ ActivePane = PaneValues . BottomRight ,
1089
1136
State = PaneStateValues . Frozen
1090
1137
} ;
1091
1138
1139
+ // Adjust active pane based on what is being frozen
1140
+ if ( rowsToFreeze > 0 && columnsToFreeze > 0 )
1141
+ {
1142
+ pane . ActivePane = PaneValues . BottomRight ; // Both rows and columns
1143
+ }
1144
+ else if ( rowsToFreeze > 0 )
1145
+ {
1146
+ pane . ActivePane = PaneValues . BottomLeft ; // Only rows
1147
+ }
1148
+ else if ( columnsToFreeze > 0 )
1149
+ {
1150
+ pane . ActivePane = PaneValues . TopRight ; // Only columns
1151
+ }
1152
+
1092
1153
// Insert the Pane as the first child of SheetView
1093
1154
sheetView . InsertAt ( pane , 0 ) ;
1094
1155
1095
1156
// Add the selection for the frozen pane
1096
1157
Selection selection = new Selection ( )
1097
1158
{
1098
- Pane = PaneValues . BottomLeft ,
1099
- ActiveCell = "A2" ,
1100
- SequenceOfReferences = new ListValue < StringValue > ( ) { InnerText = "A2" }
1159
+ Pane = pane . ActivePane ,
1160
+ ActiveCell = topLeftCell ,
1161
+ SequenceOfReferences = new ListValue < StringValue > ( ) { InnerText = topLeftCell }
1101
1162
} ;
1102
1163
1103
1164
// Ensure selection comes after the pane
@@ -1107,6 +1168,46 @@ public void FreezeFirstRow()
1107
1168
_worksheetPart . Worksheet . Save ( ) ;
1108
1169
}
1109
1170
1171
+ /// <summary>
1172
+ /// Determines the top left cell after the freeze pane based on rows and columns to freeze.
1173
+ /// </summary>
1174
+ /// <param name="rowsToFreeze">The number of rows to freeze.</param>
1175
+ /// <param name="columnsToFreeze">The number of columns to freeze.</param>
1176
+ /// <returns>The top left cell reference as a string (e.g., "B2").</returns>
1177
+ private string GetTopLeftCell ( int rowsToFreeze , int columnsToFreeze )
1178
+ {
1179
+ // Default top left cell is A1
1180
+ if ( rowsToFreeze == 0 && columnsToFreeze == 0 )
1181
+ {
1182
+ return "A1" ;
1183
+ }
1184
+
1185
+ // Calculate column part (A, B, C, etc.) based on columns to freeze
1186
+ string columnLetter = columnsToFreeze > 0 ? GetColumnLetter ( columnsToFreeze + 1 ) : "A" ;
1187
+
1188
+ // Calculate row number based on rows to freeze
1189
+ int rowNumber = rowsToFreeze > 0 ? rowsToFreeze + 1 : 1 ;
1190
+
1191
+ return $ "{ columnLetter } { rowNumber } ";
1192
+ }
1193
+
1194
+ /// <summary>
1195
+ /// Converts a column index (1-based) to an Excel column letter (A, B, C, ..., Z, AA, AB, etc.).
1196
+ /// </summary>
1197
+ /// <param name="columnIndex">The 1-based index of the column.</param>
1198
+ /// <returns>The corresponding column letter as a string.</returns>
1199
+ private string GetColumnLetter ( int columnIndex )
1200
+ {
1201
+ string columnLetter = string . Empty ;
1202
+ while ( columnIndex > 0 )
1203
+ {
1204
+ columnIndex -- ;
1205
+ columnLetter = ( char ) ( 'A' + ( columnIndex % 26 ) ) + columnLetter ;
1206
+ columnIndex /= 26 ;
1207
+ }
1208
+ return columnLetter ;
1209
+ }
1210
+
1110
1211
/// <summary>
1111
1212
/// Hides a specific row in the worksheet.
1112
1213
/// </summary>
0 commit comments