Split a Workbook into Separate Excel Files in Java

This article demonstrates how to split a workbook into multiple Excel files (each containing one worksheet) by using Spire.XLS for Java.

import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;

public class SplitWorkbook {

    public static void main(String[] args) {

        //Create a Workbook object
        Workbook wb = new Workbook();

        //Load an Excel document
        wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx");

        //Declare a Workbook variable
        Workbook newWb;

        //Declare a String variable
        String sheetName;

        //Specify the folder path, which is used to store the generated Excel files
        String folderPath = "C:\\Users\\Administrator\\Desktop\\Output\\";

        //Loop through the worksheets in the source file
        for (int i = 0; i < wb.getWorksheets().getCount(); i++) {

            //Initialize the Workbook object
            newWb = new Workbook();

            //Remove the default sheets
            newWb.getWorksheets().clear();

            //Add the the specific worksheet of the source document to the new workbook
            newWb.getWorksheets().addCopy(wb.getWorksheets().get(i));

            //Get the worksheet name
            sheetName = wb.getWorksheets().get(i).getName();

            //Save the new workbook to the specified folder 
            newWb.saveToFile(folderPath + sheetName + ".xlsx", FileFormat.Version2013);
        }
    }
}

Split a Workbook into Separate Excel Files in Java