Monday, 12 July 2021 03:23

Copy Worksheets from one Workbook to another in Java

Written by  support iceblue
Rate this item
(0 votes)

This article shows how to copy a worksheet from an Excel document to anther by using Spire.XLS for Java.

import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class CopySheetBetweenWorkbooks {
    public static void main(String[] args) {

        //Create a Workbook object to load the source document
        Workbook srcWorkbook = new Workbook();
        srcWorkbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\source.xlsx");

        //Get the specific worksheet to copy
        Worksheet originalSheet = srcWorkbook.getWorksheets().get(0);

        //Create another Workbook object to load the destination document
        Workbook destWorkbook = new Workbook();
        destWorkbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\destination.xlsx");

        //Add the copy of selected sheet to destination document
        Worksheet newSheet = destWorkbook.getWorksheets().addCopy(originalSheet);
        newSheet.setName(originalSheet.getName());

        //Copy the theme of source document to destination document
        destWorkbook.copyTheme(srcWorkbook);

        //Save to another file
        destWorkbook.saveToFile("CopySheetBetweenWorkbooks.xlsx");
    }
}

Copy Worksheets from one Workbook to another in Java

Additional Info

  • tutorial_title:
Last modified on Wednesday, 01 September 2021 02:29