Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

     

How to protect Excel files in Java applications

The data security becomes more and more important since the internet spreads the data quickly than we can imagine. We always use Excel files to store a large amount of data which is very important to us in our daily life. So in this article, we are going to give you a detailed introduction about how to protect the excel files with password in Java.

This article will show you 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

Part 1: Encrypt the whole excel workbook with password

importcom.spire.xls.*;publicclassEncryptWorkbook{publicstaticvoidmain(String[]args){//Create a workbook and load a fileWorkbookworkbook=newWorkbook();workbook.loadFromFile("Sample.xlsx");//Protect the workbook with the password you wantworkbook.protect("eiceblue");//Save the Excel fileworkbook.saveToFile("output/EncryptWorkbook.xlsx",ExcelVersion.Version2010);}}

Here is the effective screenshot when we open the protected workbook:
Encrypted Excel workbook

Part 2: Only protect the first worksheet on the excel workbook.

importcom.spire.xls.*;importjava.util.EnumSet;publicclassProtectWorksheet{publicstaticvoidmain(String[]args){//Create a workbook and load a fileWorkbookworkbook=newWorkbook();workbook.loadFromFile("Sample.xlsx");//Get the first worksheetWorksheetsheet=workbook.getWorksheets().get(0);//Protect this sheet with a password.sheet.protect("TestPassword",EnumSet.of(SheetProtectionType.All));//Save the Excel fileworkbook.saveToFile("output/ProtectWorksheet.xlsx",ExcelVersion.Version2010);}}

Here is the screenshot of the protected worksheet:

Protect Excel worksheet

Part 3: Lock some certain cells on the Excel Worksheet.

importcom.spire.xls.*;importjava.util.EnumSet;publicclassProtectCell{publicstaticvoidmain(String[]args){//Create a workbook and load a fileWorkbookworkbook=newWorkbook();workbook.loadFromFile("Sample.xlsx");//Get the first worksheetWorksheetsheet=workbook.getWorksheets().get(0);//Protect cellsheet.getCellRange("B3").getCellStyle().setLocked(true);sheet.getCellRange("C3").getCellStyle().setLocked(false);//Protect sheetsheet.protect("TestPassword",EnumSet.of(SheetProtectionType.All));//Save the Excel fileworkbook.saveToFile("output/ProtectCell.xlsx",ExcelVersion.Version2010);}}

Here is the protected Excel cells:
Lock Cells

Conclusion:
Thanks to Free Spire.XLS, I could show you how to protect/encrypted the Excel workbook, Excel worksheets and certain Excel cells easily in java. Besides encrypted the Excel file formats with password, it also supports to unlock and decrypted the Excel files. Thanks for your reading.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Your Office Development Master
  • Location
    Chengdu, SiChuan, China
  • Work
    Dev at E-iceblue
  • Joined

More fromE-iceblue Product Family

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp