Knowledgebase (2330)
Children categories
It is not an easy task for WPF developers moving worksheet to another location in a workbook, as calculations or charts that are based on worksheet data might become inaccurate if you move the worksheet in WPF application. Here presents a solution that saves you from these worries. Apply Spire.Xls for WPF in your application, and you easily can move worksheet in your WPF application.
Spire.XLS for WPF is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document in WPF applications. Spire.XLS for .NET embed a method - Spire.Xls.WorkShee.MoveWorksheet(int destIndex) in its class design used to move a worksheet to another location in the spreadsheet. The method takes the target worksheet index as a parameter and lead no inaccuracy on these calculations or charts.
Now Feel free to download Spire.XLS for WPF and use the code samples below to move worksheet in WPF application.
using Spire.Xls;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
//open Excel
Workbook mywbk = new Workbook();
mywbk.LoadFromFile(@"..\test.xls");
// Locate the Worksheet
Worksheet mysht = mywbk.Worksheets[0];
//Move Worksheet
mysht.MoveWorksheet(2);
//Save and Launch
mywbk.SaveToFile("result.xls");
System.Diagnostics.Process.Start("result.xls");
}
}
}
Imports Spire.Xls
Imports System.Windows
Namespace WpfApplication1
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub button2_Click(sender As Object, e As RoutedEventArgs)
'open Excel
Dim mywbk As New Workbook()
mywbk.LoadFromFile("..\test.xls")
' Locate the Worksheet
Dim mysht As Worksheet = mywbk.Worksheets(0)
'Move Worksheet
mysht.MoveWorksheet(2)
'Save and Launch
mywbk.SaveToFile("result.xls")
System.Diagnostics.Process.Start("result.xls")
End Sub
End Class
End Namespace
When working with Excel files that contain multiple worksheets, you may sometimes find that some of the worksheets are no longer necessary or need to be organized in a different way. In these cases, you have the option of moving or deleting the worksheets to better manage your spreadsheet. In this article, you will learn how to move or delete worksheets in Excel in C# using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Move a Worksheet in Excel in C#
Spire.XLS for .NET provides the Worksheet.MoveWorksheet(int destIndex) method to move an Excel worksheet to a desired position by specifying the index. The following are the detailed steps:
- Create a Workbook instance.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet using Workbook.Worksheet[] property.
- Move the worksheet to another position using Worksheet.MoveWorksheet() method.
- Save the result file using Workbook.SaveToFile() method.
- C#
using Spire.Xls;
namespace MoveSheet
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile("Budget.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Move the worksheet to the 3rd position
sheet.MoveWorksheet(2);
//Save the result file
workbook.SaveToFile("MoveWorksheet.xlsx", ExcelVersion.Version2016);
}
}
}

Delete a Worksheet in Excel in C#
You can remove a specific worksheet from Excel by sheet index or name using the Workbook.Worksheets.RemoveAt(int index) or Workbook.Worksheets.Remove(string sheetName) method. The following are the detailed steps:
- Create a Workbook instance.
- Load an Excel file using Workbook.LoadFromFile() method.
- Delete a specific worksheet from the file using Workbook.Worksheets.RemoveAt() or Workbook.Worksheets.Remove() method.
- Save the result file using Workbook.SaveToFile() method.
- C#
using Spire.Xls;
namespace RemoveSheet
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile("Budget.xlsx");
//Remove a specific worksheet by index
workbook.Worksheets.RemoveAt(0);
//Remove a specific worksheet by name
//workbook.Worksheets.Remove("Summary");
//Save the result file
workbook.SaveToFile("DeleteWorksheet.xlsx", ExcelVersion.Version2016);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Before we start our topic, let us first play a game. At the top of this page, there are nine categories. When we move our mouse on .NET, a dropdown list which shows all e-iceblue .NET products is displayed as the picture below:

Imagine, if we demonstrate every product on .NET out of the dropdown list on the top, what will this page be? The webpage will be rambling and disordered. As a result, visitors will hardly search what they want and finally have to leave disappointedly.
By the same token, if we do not merge PDF files of similar content to one document, all the information of various classes will mix together. We have no choice but to spend much time on finding the right files. While our work turns more efficient when we use PDF merge function. Furthermore, opening more PDF files at the same time always annoys people in that the windows are mistakenly switched. Finally, merge PDF does not mean gathering tens of PDF files into one. More reasonable choice is to amalgamate PDF files of few pages into single one. Those of numerous pages should be combined with merged PDF files in one folder for the convenience of reading.
Spire.PDF for WPF, as a professional PDF component, is a best hand for users to merge multiple PDF files on WPF application with C#, VB.NET. The whole procedure can be performed by below three simple steps.
Step 1. Create a C#/VB.NET Project
- Free download and correctly install Spire.PDF for WPF on system.
- Create “PDF merge” project in WPF Application with C#/VB.NET.
- Add a button in MainWindow.
- Add Spire.Pdf.Wpf.dll and System.Drawing as references.
Step 2 Merge PDF Files into One
//pdf document list
String[] files = new String[]
{
@"D:\e-iceblue\Spire.PDF\Demos\Data\DigitalSignature.pdf",
@"D:\e-iceblue\Spire.PDF\Demos\Data\Sample2.pdf",
@"D:\e-iceblue\Spire.PDF\Demos\Data\ImageWaterMark.pdf"
};
'pdf document list
Dim files As [String]() = New [String]() {"D:\e-iceblue\Spire.PDF\Demos\Data\DigitalSignature.pdf", "D:\e-iceblue\Spire.PDF\Demos\Data\Sample2.pdf", "D:\e-iceblue\Spire.PDF\Demos\Data\ImageWaterMark.pdf"}
Step 2. Open the three PDF files in a new document and import all pages into a new one
//open pdf documents
PdfDocument[] docs = new PdfDocument[files.Length];
for (int i = 0; i < files.Length; i++)
{
docs[i] = new PdfDocument(files[i]);
}
//append document
docs[0].AppendPage(docs[1]);
//import pages
for (int i = 0; i < docs[2].Pages.Count; i = i + 2)
{
docs[0].InsertPage(docs[2], i);
}
'open pdf documents
Dim docs As PdfDocument() = New PdfDocument(files.Length - 1) {}
For i As Integer = 0 To files.Length - 1
docs(i) = New PdfDocument(files(i))
Next
'append document
docs(0).AppendPage(docs(1))
'import pages
Dim i As Integer = 0
While i < docs(2).Pages.Count
docs(0).InsertPage(docs(2), i)
i = i + 2
End While
Step 3. Save and Preview the Merged PDF File
docs[0].SaveToFile("MergeDocuments.pdf");
foreach (PdfDocument doc in docs)
{
doc.Close();
}
PDFDocumentViewer("MergeDocuments.pdf");
docs(0).SaveToFile("MergeDocuments.pdf")
For Each doc As PdfDocument In docs
doc.Close()
Next
PDFDocumentViewer("MergeDocuments.pdf")
Effective Screeshot

Spire.PDF is a PDF document creation component that enables your .NET/Silverlight/WPF applications to read, write and manipulate PDF documents without using Adobe Acrobat.