Security

Security (3)

This article demonstrates how to detect whether an Excel document is password protected or not using Spire.XLS for Java.

import com.spire.xls.Workbook;

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

        //Get the file path
        String filePath= "C:\\Users\\Administrator\\Desktop\\sample.xlsx";

        //Detect whether the workbook is password protected or not
        Boolean isProtected = Workbook.bookIsPasswordProtected(filePath);

        //Print results
        if (isProtected) {
            System.out.print("The document is password protected.");
        }
        else {
            System.out.print("The document is not protected.");
        }
    }
}

Detect if an Excel Document is Password Protected in Java

Wednesday, 19 February 2020 06:28

Java unprotect the Excel file

Written by support iceblue

We have already demonstrated how to protect the Excel file in Java; this article will show you how to unprotect the Excel workbook or a single worksheet in Java applications.

Unprotect the Excel workbook:

import com.spire.xls.*;

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

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

        //Use the password to open the sample document
        workbook.setOpenPassword("E-iceblue");
        workbook.loadFromFile("ProtectWorkbook.xlsx");

        //Unprotect the whole workbook
        workbook.unProtect();

        //Save the document to file
        workbook.saveToFile("UnprotectWb.xlsx");
        workbook.dispose();
    }
}

Unprotect a single Excel worksheet:

import com.spire.xls.*;

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

        //Create a workbook and load a sample file with protected worksheet
        Workbook workbook = new Workbook();
        workbook.loadFromFile("ProtectWS.xlsx");
        
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Unprotect the first worksheet
        sheet.unprotect("iceblue");

        //Save the document to file
        workbook.saveToFile("Unprotectworksheet.xlsx");
        workbook.dispose();
    }
}

This article demonstrates how to use Spire.XLS for Java to encrypt the Excel files from the following three parts:

  • Encrypt the whole excel workbook with password in Java
  • Protect a certain worksheet with password in Java
  • Lock some certain cells on the worksheet in Java

Encrypt the whole Excel workbook:

import com.spire.xls.*;

public class EncryptWorkbook {
    public static void main(String[] args) {
        //Create a workbook and load a file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Protect the workbook with the password you want
        workbook.protect("eiceblue");

        //Save the Excel file
        workbook.saveToFile("output/EncryptWorkbook.xlsx", ExcelVersion.Version2010);

    }
}

Effective screenshot after protected workbook:

How to protect Excel files in Java applications

Only protect the first worksheet on the excel workbook.

import com.spire.xls.*;
import java.util.EnumSet;

public class ProtectWorksheet {
    public static void main(String[] args) {
        //Create a workbook and load a file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Protect this sheet with a password.
        sheet.protect("TestPassword", EnumSet.of(SheetProtectionType.All));

        //Save the Excel file
        workbook.saveToFile("output/ProtectWorksheet.xlsx", ExcelVersion.Version2010);

    }
}

The screenshot of the protected worksheet:

How to protect Excel files in Java applications

Lock some certain cells on the Excel Worksheet.

import com.spire.xls.*;
import java.util.EnumSet;

public class ProtectCell {
    public static void main(String[] args) {
        //Create a workbook and load a file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Protect cell
        sheet.getCellRange("B3").getCellStyle().setLocked(true);
        sheet.getCellRange("C3").getCellStyle().setLocked(false);

        //Protect sheet
        sheet.protect("TestPassword", EnumSet.of(SheetProtectionType.All));

        //Save the Excel file
        workbook.saveToFile("output/ProtectCell.xlsx", ExcelVersion.Version2010);

    }
}

Here is the protected Excel cells:

How to protect Excel files in Java applications