Excel worksheet protection

The following example shows how you can protect a worksheet.

Note that specifying advanced settings like sheet password or allowing some objects to be editable is supported only for XLSX file format.

Excel worksheet protected with GemBox.Spreadsheet for Java
Screenshot of Excel worksheet protected with GemBox.Spreadsheet for Java
import com.gembox.spreadsheet.*;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");

        ExcelFile workbook = new ExcelFile();
        ExcelWorksheet worksheet = workbook.addWorksheet("Sheet Protection");

        worksheet.getCell(0, 2).setValue("Only cells from A1 to A10 are editable.");

        for (int i = 0; i < 10; i++) {
            ExcelCell cell = worksheet.getCell(i, 0);
            cell.setValue(i);
            cell.getStyle().setLocked(false);
        }

        worksheet.setProtected(true);

        // ProtectionSettings class is supported only for XLSX file format.
        worksheet.getCell(2, 2).setValue("Inserting columns is allowed (only supported for XLSX file format).");
        WorksheetProtection protectionSettings = worksheet.getProtectionSettings();
        protectionSettings.setAllowInsertingColumns(true);

        worksheet.getCell(3, 2).setValue("Sheet password is 123 (only supported for XLSX file format).");
        protectionSettings.setPassword("123");

        workbook.save("Sheet Protection.%OutputFileType%");
    }
}

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet for Java yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.