C#/VB.NET :Split an Excel Worksheet into Separate Excel Files
With the continuous expansion of data, the scale of some data tables will increase geometrically. When the data reaches a certain scale, the query and reading performance of the data will become slow. At this time, the data table needs to be split. Next In this article, you will learn in detail how to split an Excel worksheet into separate Excel files through C#/VB.NET code. The following are the specific steps and codes I have compiled for your reference, I hope it will be helpful to you.
Programming Environment
In this test, Free Spire.XLS for .NET is introduced into the program. The Spire.XLS.dll file can be referenced by:
Method 1: Download Free Spire.XLS for .NET locally, unzip it, and install it. After the installation is complete, find Spire.XLS.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.
Method 2: Install via NuGet. It can be installed by the following 2 methods:
(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.XLS", and click "Install". Wait for the program installation to complete.
(2) Copy the following content to the PM console installation.
Install-Package FreeSpire.XLS -Version 12.7.0
Specific Steps:
- Create a Workbook object
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Declare a new Workbook variable, which is used to create new Excel workbooks.
- Loop through the worksheets in the source document.
- Initialize the Workbook object, and add the copy of a specific worksheet of source document into it.
- Save the workbook to an Excel file using Workbook.SaveToFile() method.
- using Spire.Xls;using System;namespace SplitWorksheets{class Program{static void Main(string[] args){//Create a Workbook objectWorkbook wb = new Workbook();//Load an Excel documentwb.LoadFromFile("data.xlsx");//Declare a new Workbook variableWorkbook newWb;//Declare a String variableString sheetName;//Specify the folder path which is used to store the generated Excel filesString folderPath = "C:\\Users\\ Administrator \\Desktop\\Output\\";//Loop through the worksheets in the source filefor (int i = 0; i < wb.Worksheets.Count; i++){//Initialize the Workbook objectnewWb = new Workbook();//Remove the default sheetsnewWb.Worksheets.Clear();//Add the specific worksheet of the source document to the new workbooknewWb.Worksheets.AddCopy(wb.Worksheets[i]);//Get the worksheet namesheetName = wb.Worksheets[i].Name;//Save the new workbook to the specified foldernewWb.SaveToFile(folderPath + sheetName + ".xlsx", ExcelVersion.Version2013);}}}}
- Imports Spire.XlsNamespace SplitWorksheetsFriend Class ProgramPrivate Shared Sub Main(ByVal args As String())'Create a Workbook objectDim wb As Workbook = New Workbook()'Load an Excel documentwb.LoadFromFile("data.xlsx")'Declare a new Workbook variableDim newWb As Workbook'Declare a String variableDim sheetName As String'Specify the folder path which is used to store the generated Excel filesDim folderPath = "C:\Users\ Administrator \Desktop\Output\"'Loop through the worksheets in the source fileFor i As Integer = 0 To wb.Worksheets.Count - 1'Initialize the Workbook objectnewWb = New Workbook()'Remove the default sheetsnewWb.Worksheets.Clear()'Add the specific worksheet of the source document to the new workbooknewWb.Worksheets.AddCopy(wb.Worksheets(i))'Get the worksheet namesheetName = wb.Worksheets(i).Name'Save the new workbook to the specified foldernewWb.SaveToFile(folderPath & sheetName & ".xlsx", ExcelVersion.Version2013)NextEnd SubEnd ClassEnd Namespace
- Effective Shot
Conclusion:
In this post, you have learned how to split an Excel worksheet into separate Excel files in C#/VB.NET. Not only that, we also have other functions, such as, Merge Excel Files into One , Split One Excel Worksheet to Several Excel Documents and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.XLS for .NET.
Comments
Post a Comment