News Category

Security

Security (6)

In Microsoft Excel, you can lock specific cells so that other users cannot make changes to the data or formulas within them. In this article, we will introduce how to lock cells in Excel programmatically in Java using Spire.XLS for Java library.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>14.3.2</version>
    </dependency>
</dependencies>
    

Lock Specific Cells in Excel in Java

Normally, the locked option is enabled for all cells in a worksheet. Therefore, before locking a cell or range of cells, all cells must be unlocked. Keep in mind that locking cells doesn’t take effect until the worksheet is protected.

The following are the steps to lock specific cells in Excel:

  • Create an instance of Workbook class.
  • Load the Excel file using Workbook.loadFromFile() method.
  • Get the desired worksheet using Workbook.getWorksheets().get(sheetIndex) method.
  • Access the used range in the worksheet and then unlock all the cells in the range using XlsRange.getStyle().setLocked() method.
  • Access specific cells and then lock them using XlsRange.getStyle().setLocked() method.
  • Protect the worksheet using XlsWorksheetBase.protect() method.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

import java.util.EnumSet;

public class LockCells {
    public static void main(String []args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("Input.xlsx");

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

        //Unlock all cells in the used range of the worksheet
        CellRange usedRange = sheet.getRange();
        usedRange.getStyle().setLocked(false);

        //Lock specific cells
        CellRange cells = sheet.getRange().get("A1:C3");
        cells.getStyle().setLocked(true);

        //Protect the worksheet with password
        sheet.protect("123456", EnumSet.of(SheetProtectionType.All));

        //Save the result file
        workbook.saveToFile("LockCells.xlsx", ExcelVersion.Version2016);
    }
}

Java: Lock Specific Cells in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Spire.XLS for Java offers the Workbook.addDigitalSignature() method and the Workbook.removeAllDigitalSignatures() method to add and remove the Excel digital signatures. This article will show you how to detect if an Excel document has been signed or not, and retrieve information about digital signatures in Java applications.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>14.3.2</version>
    </dependency>
</dependencies>
    

Detect if an Excel is Digitally Signed or Not

Spire.XLS for Java offers the Workbook.isDigitallySigned() method to check whether an Excel document is digitally signed or not.

  • Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
  • Determine if the workbook is digitally signed by Workbook.isDigitallySigned() method.
  • Print out the results.
  • Java
import com.spire.xls.*;

public class Test {
    public static void main(String[] args) throws Exception {

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

        //Load an Excel document
        workbook.loadFromFile("Sample.xlsx");

        //Detect if an Excel Document is digitally signed or not
        Boolean signature = workbook.isDigitallySigned();

       //Print the results
       if (signature) {
            System.out.println("Document has been signed");
        }
         else
        {
            System.out.println("Document has not been signed");
        }

    }
}

Java: Get Digital Signatures in Excel in Java

Get the Digital Signature Details in Excel

The following are the steps to get the details of the digital signatures in an Excel document.

  • Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
  • Get the collection of digital signatures using Workbook.getDigitalSignatures() method.
  • Get the details of a specific digital signature using the methods under IDigitalSignature object.
  • Java
import com.spire.xls.*;
import com.spire.xls.core.interfaces.IDigitalSignature;
import com.spire.xls.core.interfaces.IDigitalSignatures;

import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Date;

public class Test {
    public static void main(String[] args) throws Exception {

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

        //Load an Excel document
        workbook.loadFromFile("Sample01.xlsx");

        //Get the collection of digital signatures
        IDigitalSignatures signatures = workbook.getDigitalSignatures();

        //Get the details of digital signatures
        for (IDigitalSignature digitalSignature : (Iterable) signatures) {
            X509Certificate info = digitalSignature.getX509Certificate();
            PrivateKey privateKey = digitalSignature.getPrivateKey();
            String comment = digitalSignature.getComments();
            Date date = digitalSignature.getSignTime();

            //Print out the results of the Excel digital signature
            System.out.println("Signatature Certificate:" + info + "\n" +
                    "Signature Comment:" + comment + "\n" +
                    "Sign Date:" + date + "\n" +
                    "PrivateKey:" + privateKey + "\n");
        }
    }
}

Java: Get Digital Signatures in Excel in Java

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

A digital signature is an electronic signature with encrypted information that helps verify the authenticity of messages, software and digital documents. They are commonly used in software distribution, financial transactions, contract management software, and other situations that require forgery or tampering detection. When generating an Excel report, you may need to add a digital signature to make it look more authentic and official. In this article, you will learn how to add or delete digital signatures in Excel in Java using Spire.XLS for Java.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>14.3.2</version>
    </dependency>
</dependencies>
    

Add a Digital Signature to Excel in Java

You can add a digital signature to protect the integrity of an Excel file. Once the digital signature is added, the file becomes read-only to discourage further editing. If someone makes changes to the file, the digital signature will become invalid immediately.

Spire.XLS for Java provides the addDigitalSignature method of Workbook class to add digital signatures to an Excel file. The detailed steps are as follows:

  • Instantiate a Workbook instance.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Instantiate a CertificateAndPrivateKey instance with the specified certificate (.pfx) file path and the password of the .pfx file.
  • Add a digital signature to the file using Workbook.addDigitalSignature(CertificateAndPrivateKey, String, Date) method.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.digital.CertificateAndPrivateKey;

import java.util.Date;

public class AddDigitalSignature {
    public static void main(String []args) throws Exception {
        //Create a Workbook instance
        Workbook workbook=new Workbook();
        //Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        //Add a digital signature to the file
        CertificateAndPrivateKey cap = new CertificateAndPrivateKey("Test.pfx","e-iceblue");
        workbook.addDigitalSignature(cap, "e-iceblue",new Date());

        //Save the result file
        workbook.saveToFile("AddDigitalSignature.xlsx", ExcelVersion.Version2013);
    }
}

Java: Add or Delete Digital Signatures in Excel

Delete Digital Signature from Excel in Java

Spire.XLS for Java provides the removeAllDigitalSignatures method of Workbook class for developers to remove digital signatures from an Excel file. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Remove all digital signatures from the file using Workbook.removeAllDigitalSignatures() method.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

public class DeleteDigitalSignature {
    public static void main(String []args) throws Exception {
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("AddDigitalSignature.xlsx");

        //Remove all digital signatures in the file
        workbook.removeAllDigitalSignatures();

        //Save the result file
        workbook.saveToFile("RemoveDigitalSignature.xlsx", ExcelVersion.Version2013);
    }
}

Java: Add or Delete Digital Signatures in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

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

Java unprotect the Excel file

2020-02-19 06:28:02 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();
    }
}

While sharing your spreadsheets with others, you may do not want the receiver to alter the content or may want them to change only specific content and leave the rest unchanged. To protect your worksheet from being edited by other people, Excel offers a protection feature. In this article, you will learn how to programmatically protect and unprotect a workbook or a worksheet in Java by using Spire.XLS for Java.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>14.3.2</version>
    </dependency>
</dependencies>
    

Password Protect an Entire Workbook in Java

By encrypting an Excel document with a password, you ensure that only you and authorized individuals can read or edit it. The following are the steps to password protect a workbook using Spire.XLS for Java.

  • Create a Workbook object.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Protect the workbook using a password using Workbook.protect() method.
  • Save the workbook to another Excel file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

public class PasswordProtectWorkbook {

    public static void main(String[] args) {

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

        //Load an Excel file
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");

        //Protect workbook with a password
        workbook.protect("psd-123");

        //Save the workbook to another Excel file
        workbook.saveToFile("output/Encrypted.xlsx", ExcelVersion.Version2016);
    }
}

Java: Protect or Unprotect Excel Documents

Protect a Worksheet with a Specific Protection Type in Java

If you wish to grant people permission to read your Excel document but restrict the types of modifications they are allowed to make on a worksheet, you can protect the worksheet with a specific protection type. The table below lists a variety of pre-defined protection types under the SheetProtectionType enumeration.

Protection Type Allow users to
Content Modify or insert content.
DeletingColumns Delete columns.
DeletingRows Delete rows.
Filtering Set filters.
FormattingCells Format cells.
FormattingColumns Format columns.
FormattingRows Format rows.
InsertingColumns Insert columns.
InsertingRows Insert rows.
InsertingHyperlinks Insert hyperlinks .
LockedCells Select locked cells.
UnlockedCells Select unlocked cells.
Objects Modify drawing objects.
Scenarios Modify saved scenarios.
Sorting Sort data.
UsingPivotTables Use pivot table and pivot chart.
All Do any operations listed above on the protected worksheet.
None Do nothing on the protected worksheet.

The following are the steps to protect a worksheet with a specific protection type using Spire.XLS for Java.

  • Create a Workbook object.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Get a specific worksheet using Workbook.getWorksheets().get(index) method.
  • Protect the worksheet with a protection type using Worksheet.protect(String password, EnumSet.of <SheetProtectionType> options) method.
  • Save the workbook to another Excel file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.SheetProtectionType;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

import java.util.EnumSet;

public class ProtectWorksheet {

    public static void main(String[] args) {

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

        //Load an Excel file
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");

        //Get a specific worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);

        //Protect the worksheet with the permission password and the specific protect type
        worksheet.protect("psd-permission", EnumSet.of(SheetProtectionType.None));

        //Save the workbook to another Excel file
        workbook.saveToFile("output/ProtectWorksheet.xlsx", ExcelVersion.Version2016);
    }
}

Java: Protect or Unprotect Excel Documents

Allow Users to Edit Ranges in a Protected Worksheet in Java

In certain cases, you may need to allow users to be able to edit selected ranges in a protected worksheet. The following steps demonstrate how to.

  • Create a Workbook object.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Get a specific worksheet using Workbook.getWorksheets().get(index) method.
  • Specify editable cell ranges using Worksheet.addAllowEditRange() method.
  • Protect the worksheet with a protection type using Worksheet.protect(String password, EnumSet.of <SheetProtectionType> options) method.
  • Save the workbook to another Excel file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.SheetProtectionType;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

import java.util.EnumSet;

public class AllowEditRanges {

    public static void main(String[] args) {

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

        //Load an Excel file
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");

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

        //Add ranges that allow editing
        sheet.addAllowEditRange("Range One", sheet.getRange().get("A5:A6"));
        sheet.addAllowEditRange("Range Two", sheet.getRange().get("A8:B11"));

        //Protect the worksheet with a password and a protection type
        sheet.protect("psd-permission", EnumSet.of(SheetProtectionType.All));

        //Save the workbook to another Excel file
        workbook.saveToFile("output/AllowEditRange.xlsx", ExcelVersion.Version2016);
    }
}

Java: Protect or Unprotect Excel Documents

Unprotect a Password Protected Worksheet in Java

To remove the protection of a password-protected worksheet, invoke the Worksheet.unprotect() method and pass in the original password as a parameter. The detailed steps are as follows.

  • Create a Workbook object.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Get a specific worksheet using Workbook.getWorksheets().get(index) method.
  • Remove the protection using Worksheet.unprotect(String password) method.
  • Save the workbook to another Excel file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class UnprotectWorksheet {

    public static void main(String[] args) {

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

        //Load an Excel file containing protected worksheet
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\ProtectedWorksheet.xlsx");

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

        //Unprotect the worksheet using the specified password
        sheet.unprotect("psd-permission");

        //Save the workbook to another Excel file
        workbook.saveToFile("output/UnprotectWorksheet.xlsx", ExcelVersion.Version2016);
    }
}

Remove or Reset Password of an Encrypted Workbook in Java

To remove or reset password of an encrypted workbook, you can use the Workbook.unprotect() method and the Workbook.protect() method, respectively. The following steps show you how to load an encrypted Excel document and delete or change the password of it.

  • Create a Workbook object.
  • Specify the open password using Workbook.setOpenPassword() method.
  • Load the encrypted Excel file using Workbook.loadFromFile() method.
  • Remove the encryption using Workbook.unprotect() method. Or change the password using Workbook.protect() method.
  • Save the workbook to another Excel file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

public class RemoveOrResetPassword {

    public static void main(String[] args) {

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

        //Specify the open password
        workbook.setOpenPassword("psd-123");

        //Load an encrypted Excel file
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\Encrypted.xlsx");

        //Unprotect workbook
        workbook.unProtect();

        //Reset password
        //workbook.protect("newpassword");

        //Save the workbook to another Excel file
        workbook.saveToFile("output/Unprotect.xlsx", ExcelVersion.Version2016);
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.