Excel worksheet protection

The following example shows how you can protect a worksheet.

Note that specifying advanced settings like sheet password is supported only in XLSX and XLS and 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 the 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);

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

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

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

See also


Next steps

GemBox.Spreadsheet for Java is a component that enables you to read, write, edit and convert spreadsheet files from your applications using one simple API.

Download Buy