// Move worksheet to the beginningworkBook.SetSheetPosition("ImportantSheet", 0);// Move worksheet to the endint lastPosition = workBook.WorkSheets.Count - 1;workBook.SetSheetPosition("ArchiveSheet", lastPosition);
// Move worksheet to the beginning
workBook.SetSheetPosition("ImportantSheet", 0);
// Move worksheet to the end
int lastPosition = workBook.WorkSheets.Count - 1;
workBook.SetSheetPosition("ArchiveSheet", lastPosition);
' Move worksheet to the beginningworkBook.SetSheetPosition("ImportantSheet", 0)' Move worksheet to the endDim lastPosition AsInteger = workBook.WorkSheets.Count - 1workBook.SetSheetPosition("ArchiveSheet", lastPosition)
' Move worksheet to the beginning
workBook.SetSheetPosition("ImportantSheet", 0)
' Move worksheet to the end
Dim lastPosition As Integer = workBook.WorkSheets.Count - 1
workBook.SetSheetPosition("ArchiveSheet", lastPosition)
using IronXL;WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx");// Set worksheet positionworkBook.SetSheetPosition("workSheet2", 0);workBook.SaveAs("setWorksheetPosition.xlsx");
using IronXL;
WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx");
// Set worksheet position
workBook.SetSheetPosition("workSheet2", 0);
workBook.SaveAs("setWorksheetPosition.xlsx");
ImportsIronXLPrivate workBook AsWorkBook = WorkBook.Load("createNewWorkSheets.xlsx")' Set worksheet positionworkBook.SetSheetPosition("workSheet2", 0)workBook.SaveAs("setWorksheetPosition.xlsx")
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("createNewWorkSheets.xlsx")
' Set worksheet position
workBook.SetSheetPosition("workSheet2", 0)
workBook.SaveAs("setWorksheetPosition.xlsx")
// Get the default (active) worksheet objectWorkSheet activeSheet = workBook.DefaultWorkSheet;Console.WriteLine($"Active worksheet: {activeSheet.Name}");
// Get the default (active) worksheet object
WorkSheet activeSheet = workBook.DefaultWorkSheet;
Console.WriteLine($"Active worksheet: {activeSheet.Name}");
C#
using IronXL;WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx");// Set active for workSheet3workBook.SetActiveTab(2);workBook.SaveAs("setActiveTab.xlsx");
using IronXL;
WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx");
// Set active for workSheet3
workBook.SetActiveTab(2);
workBook.SaveAs("setActiveTab.xlsx");
ImportsIronXLPrivate workBook AsWorkBook = WorkBook.Load("createNewWorkSheets.xlsx")' Set active for workSheet3workBook.SetActiveTab(2)workBook.SaveAs("setActiveTab.xlsx")
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("createNewWorkSheets.xlsx")
' Set active for workSheet3
workBook.SetActiveTab(2)
workBook.SaveAs("setActiveTab.xlsx")
// Safe worksheet removal with validationif (workBook.WorkSheets.Count > 1){ workBook.RemoveWorkSheet("TempSheet");}else{Console.WriteLine("Cannot remove the last worksheet");}
// Safe worksheet removal with validation
if (workBook.WorkSheets.Count > 1)
{
workBook.RemoveWorkSheet("TempSheet");
}
else
{
Console.WriteLine("Cannot remove the last worksheet");
}
' Safe worksheet removal with validationIf workBook.WorkSheets.Count > 1 Then workBook.RemoveWorkSheet("TempSheet")ElseConsole.WriteLine("Cannot remove the last worksheet")End If
' Safe worksheet removal with validation
If workBook.WorkSheets.Count > 1 Then
workBook.RemoveWorkSheet("TempSheet")
Else
Console.WriteLine("Cannot remove the last worksheet")
End If
// Remove multiple worksheets by collecting names first
var sheetsToRemove = workBook.WorkSheets
.Where(ws => ws.Name.StartsWith("Temp_"))
.Select(ws => ws.Name)
.ToList();
foreach (var sheetName in sheetsToRemove)
{
workBook.RemoveWorkSheet(sheetName);
}
ImportsSystem.Linq' Remove multiple worksheets by collecting names firstDim sheetsToRemove = workBook.WorkSheets _ .Where(Function(ws) ws.Name.StartsWith("Temp_")) _ .Select(Function(ws) ws.Name) _ .ToList()For Each sheetName In sheetsToRemove workBook.RemoveWorkSheet(sheetName)Next
Imports System.Linq
' Remove multiple worksheets by collecting names first
Dim sheetsToRemove = workBook.WorkSheets _
.Where(Function(ws) ws.Name.StartsWith("Temp_")) _
.Select(Function(ws) ws.Name) _
.ToList()
For Each sheetName In sheetsToRemove
workBook.RemoveWorkSheet(sheetName)
Next
// Example: Copying a worksheet and updating formula referencesWorkSheet original = workBook.GetWorkSheet("Original");WorkSheet copied = original.CopySheet("Duplicate");// Update formulas that need to reference the new sheetforeach (var cell in copied["A1:Z100"]){ if (cell.IsFormula) { // Replace references as needed string formula = cell.Formula; // Update formula logic here based on your needs }}
// Example: Copying a worksheet and updating formula references
WorkSheet original = workBook.GetWorkSheet("Original");
WorkSheet copied = original.CopySheet("Duplicate");
// Update formulas that need to reference the new sheet
foreach (var cell in copied["A1:Z100"])
{
if (cell.IsFormula)
{
// Replace references as needed
string formula = cell.Formula;
// Update formula logic here based on your needs
}
}
ImportsSystem' Example: Copying a worksheet and updating formula referencesDim original AsWorkSheet = workBook.GetWorkSheet("Original")Dim copied AsWorkSheet = original.CopySheet("Duplicate")' Update formulas that need to reference the new sheetFor Each cell In copied("A1:Z100") If cell.IsFormulaThen ' Replace references as needed Dim formula AsString = cell.Formula ' Update formula logic here based on your needs End IfNext
Imports System
' Example: Copying a worksheet and updating formula references
Dim original As WorkSheet = workBook.GetWorkSheet("Original")
Dim copied As WorkSheet = original.CopySheet("Duplicate")
' Update formulas that need to reference the new sheet
For Each cell In copied("A1:Z100")
If cell.IsFormula Then
' Replace references as needed
Dim formula As String = cell.Formula
' Update formula logic here based on your needs
End If
Next
using IronXL;WorkBook firstBook = WorkBook.Create(ExcelFileFormat.XLSX);WorkBook secondBook = WorkBook.Create();// Select first worksheet in the workbookWorkSheet workSheet = firstBook.DefaultWorkSheet;// Duplicate the worksheet to the same workbookworkSheet.CopySheet("Copied Sheet");// Duplicate the worksheet to another workbook with the specified nameworkSheet.CopyTo(secondBook, "Copied Sheet");firstBook.SaveAs("firstWorksheet.xlsx");secondBook.SaveAs("secondWorksheet.xlsx");
using IronXL;
WorkBook firstBook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkBook secondBook = WorkBook.Create();
// Select first worksheet in the workbook
WorkSheet workSheet = firstBook.DefaultWorkSheet;
// Duplicate the worksheet to the same workbook
workSheet.CopySheet("Copied Sheet");
// Duplicate the worksheet to another workbook with the specified name
workSheet.CopyTo(secondBook, "Copied Sheet");
firstBook.SaveAs("firstWorksheet.xlsx");
secondBook.SaveAs("secondWorksheet.xlsx");
ImportsIronXLDim firstBook AsWorkBook = WorkBook.Create(ExcelFileFormat.XLSX)Dim secondBook AsWorkBook = WorkBook.Create()' Select first worksheet in the workbookDim workSheet AsWorkSheet = firstBook.DefaultWorkSheet' Duplicate the worksheet to the same workbookworkSheet.CopySheet("Copied Sheet")' Duplicate the worksheet to another workbook with the specified nameworkSheet.CopyTo(secondBook, "Copied Sheet")firstBook.SaveAs("firstWorksheet.xlsx")secondBook.SaveAs("secondWorksheet.xlsx")
Imports IronXL
Dim firstBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
Dim secondBook As WorkBook = WorkBook.Create()
' Select first worksheet in the workbook
Dim workSheet As WorkSheet = firstBook.DefaultWorkSheet
' Duplicate the worksheet to the same workbook
workSheet.CopySheet("Copied Sheet")
' Duplicate the worksheet to another workbook with the specified name
workSheet.CopyTo(secondBook, "Copied Sheet")
firstBook.SaveAs("firstWorksheet.xlsx")
secondBook.SaveAs("secondWorksheet.xlsx")
What does IronXL do when a worksheet copy is created within the same workbook?
When a worksheet is copied within the same workbook using IronXL, the `CopySheet` method duplicates cell values, formulas, formatting, and dimensions, ensuring the new sheet maintains the original's fidelity.
How does IronXL handle duplicate worksheet names?
IronXL automatically appends a number to the name when a duplicate worksheet is created, such as renaming 'Sheet1' to 'Sheet1_1,' to prevent conflicts and ensure code execution without errors.
What does zero-based indexing mean in IronXL worksheet management?
In IronXL, zero-based indexing means that the first worksheet is at position 0, aligning with C# array conventions, helping avoid off-by-one errors during worksheet manipulation.
Why is it important to handle formula references when copying worksheets with IronXL?
When copying worksheets using IronXL, it's crucial to review and adjust formula references to ensure they still point to the correct data, especially in cases involving absolute or cross-sheet references.