|
1 | 1 | using DocumentFormat.OpenXml;
|
| 2 | +using DocumentFormat.OpenXml.Office2016.Excel; |
2 | 3 | using DocumentFormat.OpenXml.Packaging;
|
3 | 4 | using DocumentFormat.OpenXml.Spreadsheet;
|
4 | 5 | using System;
|
|
9 | 10 |
|
10 | 11 | namespace FileFormat.Cells
|
11 | 12 | {
|
| 13 | + |
| 14 | + public enum SheetVisibility |
| 15 | + { |
| 16 | + Visible, |
| 17 | + Hidden, |
| 18 | + VeryHidden |
| 19 | + } |
| 20 | + |
12 | 21 | /// <summary>
|
13 | 22 | /// Represents an Excel workbook with methods for creating, modifying, and saving content.
|
14 | 23 | /// </summary>
|
@@ -272,7 +281,129 @@ public bool RemoveSheet(string sheetName)
|
272 | 281 |
|
273 | 282 | // Return true to indicate success
|
274 | 283 | return true;
|
275 |
| - } |
| 284 | + } |
| 285 | + |
| 286 | + /// <summary> |
| 287 | + /// Renames an existing sheet within the workbook. |
| 288 | + /// </summary> |
| 289 | + /// <param name="existingSheetName">The current name of the sheet to be renamed.</param> |
| 290 | + /// <param name="newSheetName">The new name to be assigned to the sheet.</param> |
| 291 | + public void RenameSheet(string existingSheetName, string newSheetName) |
| 292 | + { |
| 293 | + // Find the sheet by its existing name |
| 294 | + var sheet = workbookpart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == existingSheetName); |
| 295 | + |
| 296 | + // If the sheet is found, rename it |
| 297 | + if (sheet != null) |
| 298 | + { |
| 299 | + sheet.Name = newSheetName; // Set the new name |
| 300 | + workbookpart.Workbook.Save(); // Save changes to the workbook |
| 301 | + |
| 302 | + // Synchronize the Worksheets property with the Sheets of the workbook |
| 303 | + SyncWorksheets(); |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + /// <summary> |
| 308 | + /// Copies an existing sheet within the workbook to a new sheet. |
| 309 | + /// </summary> |
| 310 | + /// <param name="sourceSheetName">The name of the sheet to be copied.</param> |
| 311 | + /// <param name="newSheetName">The name of the new sheet to be created.</param> |
| 312 | + public void CopySheet(string sourceSheetName, string newSheetName) |
| 313 | + { |
| 314 | + // Find the source sheet by its name |
| 315 | + Sheet sourceSheet = workbookpart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == sourceSheetName); |
| 316 | + |
| 317 | + if (sourceSheet != null) |
| 318 | + { |
| 319 | + // Clone the WorksheetPart of the source sheet |
| 320 | + WorksheetPart sourcePart = (WorksheetPart)(workbookpart.GetPartById(sourceSheet.Id)); |
| 321 | + WorksheetPart newPart = workbookpart.AddNewPart<WorksheetPart>(); |
| 322 | + |
| 323 | + // Clone the worksheet content |
| 324 | + newPart.Worksheet = (DocumentFormat.OpenXml.Spreadsheet.Worksheet)sourcePart.Worksheet.CloneNode(true); |
| 325 | + newPart.Worksheet.Save(); |
| 326 | + |
| 327 | + // Create a new sheet and assign it the cloned WorksheetPart |
| 328 | + Sheet newSheet = new Sheet() |
| 329 | + { |
| 330 | + Id = workbookpart.GetIdOfPart(newPart), |
| 331 | + SheetId = (uint)(workbookpart.Workbook.Sheets.Count() + 1), |
| 332 | + Name = newSheetName |
| 333 | + }; |
| 334 | + |
| 335 | + // Append the new sheet to the workbook |
| 336 | + workbookpart.Workbook.Sheets.Append(newSheet); |
| 337 | + workbookpart.Workbook.Save(); // Save changes to the workbook |
| 338 | + } |
| 339 | + } |
| 340 | + |
| 341 | + |
| 342 | + |
| 343 | + /// <summary> |
| 344 | + /// Reorders a sheet within the workbook to a new position. |
| 345 | + /// </summary> |
| 346 | + /// <param name="sheetName">The name of the sheet to be reordered.</param> |
| 347 | + /// <param name="newPosition">The new position (index) where the sheet should be moved.</param> |
| 348 | + public void ReorderSheets(string sheetName, int newPosition) |
| 349 | + { |
| 350 | + // Get the Sheets collection from the workbook |
| 351 | + Sheets sheets = workbookpart.Workbook.Sheets; |
| 352 | + |
| 353 | + // Find the sheet by its name |
| 354 | + Sheet sourceSheet = workbookpart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == sheetName); |
| 355 | + |
| 356 | + if (sourceSheet != null) |
| 357 | + { |
| 358 | + sourceSheet.Remove(); // Remove the sheet from its current position |
| 359 | + sheets.InsertAt(sourceSheet, newPosition); // Insert the sheet at the new position |
| 360 | + workbookpart.Workbook.Save(); // Save changes to the workbook |
| 361 | + } |
| 362 | + } |
| 363 | + |
| 364 | + |
| 365 | + |
| 366 | + /// <summary> |
| 367 | + /// Sets the visibility of a sheet within the workbook. |
| 368 | + /// </summary> |
| 369 | + /// <param name="sheetName">The name of the sheet whose visibility is to be set.</param> |
| 370 | + /// <param name="visibility">The visibility state to be applied to the sheet (Visible, Hidden, or VeryHidden).</param> |
| 371 | + /// <exception cref="ArgumentException">Thrown when the sheet is not found in the workbook.</exception> |
| 372 | + public void SetSheetVisibility(string sheetName, SheetVisibility visibility) |
| 373 | + { |
| 374 | + // Retrieve the Sheets collection from the workbook |
| 375 | + var sheets = workbookpart.Workbook.Sheets.Elements<Sheet>(); |
| 376 | + |
| 377 | + // Find the sheet by its name |
| 378 | + var sheet = sheets.FirstOrDefault(s => s.Name == sheetName); |
| 379 | + |
| 380 | + if (sheet != null) |
| 381 | + { |
| 382 | + // Adjust the state based on the visibility parameter |
| 383 | + switch (visibility) |
| 384 | + { |
| 385 | + case SheetVisibility.Visible: |
| 386 | + sheet.State = SheetStateValues.Visible; |
| 387 | + break; |
| 388 | + case SheetVisibility.Hidden: |
| 389 | + sheet.State = SheetStateValues.Hidden; |
| 390 | + break; |
| 391 | + case SheetVisibility.VeryHidden: |
| 392 | + sheet.State = SheetStateValues.VeryHidden; |
| 393 | + break; |
| 394 | + default: |
| 395 | + throw new ArgumentOutOfRangeException(nameof(visibility), "Invalid visibility value."); |
| 396 | + } |
| 397 | + |
| 398 | + // Save the changes to the workbook |
| 399 | + workbookpart.Workbook.Save(); |
| 400 | + } |
| 401 | + else |
| 402 | + { |
| 403 | + // Throw an exception if the sheet is not found |
| 404 | + throw new ArgumentException($"Sheet '{sheetName}' not found in the workbook."); |
| 405 | + } |
| 406 | + } |
276 | 407 |
|
277 | 408 |
|
278 | 409 | /// <summary>
|
|
0 commit comments