[]
        
(Showing Draft Content)

Clone Workbook

Cloning a workbook allows you to create an exact duplicate of your workbook at any time. The cloned workbook will serve as a backup, ensuring that if you make any unintended changes or need to revert to a previous version, you will have the correct version available. DsExcel provides clone method of Workbook class that enables a user to clone the current workbook. The method returns a new instance, which is an exact copy of the current workbook.

!type=note

Note: The clone method works with user-defined types, which are custom types created by the user. If any of these user-defined types implement this method, the workbook cloning process will use this method to create a complete, independent copy of the object. This means the new workbook will be a true clone, with all the original workbook's data and properties copied.

  • Data Source (Workbook.addDataSource)

  • Tag (IRange.setTag)

  • Default Value (IRange.setDefaultValue)

  • Cell Type (selected items of check box list/radio button list cell type can be custom object)

  • Custom Object (IRange.setValue)

However, if the user-defined types do not implement the method, the workbook cloning process will fall back to a shallow copy. The cloned workbook will share references to the same instances as the original, rather than creating independent copies. This means changes to the cloned workbook may affect the original, as they will be pointing to the same underlying objects.

type=note

Note: The events in the Workbook/Worksheet instances will not be cloned. If you need to retain the original events, you must re-register them in the cloned workbook.

Refer to the following example code to clone a workbook and delete the first sheet from the cloned workbook:

// Create new workbook.
Workbook workbook = new Workbook();

// Open Excel file.
workbook.open("BreakEven.xlsx");

// Clone workbook.
IWorkbook clone = workbook.clone();

// Delete first Worksheet. Any changes in the cloned workbook will not affect the original workbook.
clone.getWorksheets().get(0).delete();

// Save Excel file.
workbook.save("CloneWorkbook.xlsx");