Java set excel worksheet to read only
mb5f8e5f84eff49 2021-07-22 10:20:01

Excel

Excel Document protection can be set by document encryption in the worksheet , You can set the password to open the document 、 Worksheets restrict editing passwords . When you need to set the content of a document to be viewed only and cannot be edited , For sheet Table to set the content to read-only . below , adopt Java The program code shows how to set up Excel The worksheet is read-only .

Java  The program environment is as follows :

  • Code compilation tools :IntelliJ IDEA

  • Excel Test documentation :.xlsx 2013

  • Excel Class library tools :free spire.xls.jar 3.9.1

  • Jdk edition :1.8.0

Jar Import game The result is shown in Fig. :

Program code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  import  com.spire.xls.*;
  
  public  class  ReadOnly {
      public  static  void  main(String[] args) {
          // load Excel Workbook document
          Workbook wb =  new  Workbook();
          wb.loadFromFile( "sample.xlsx" );
  
          // Traverse worksheet
          for ( int  i =  0 ; i<wb.getWorksheets().getCount();i++)
          {
              Worksheet sheet = wb.getWorksheets().get(i); // Get all worksheets
              sheet.protect( "123456" ); // Set the password that allows editing
              sheet.setProtectContents( true ); // Setting up content protection ( read-only )
          }
  
          // Save the document
          wb.saveToFile( "ReadOnly.xlsx" );
          wb.dispose();
      }
  }

Execution procedure , Generating documentation , As shown in the picture :

The path of test file and result file in the code is IDEA Project folder path ,www.sangpi.com The file path of can be customized separately .

Please bring the original link to reprint ,thank
Similar articles

2021-06-04

2021-06-05

2021-06-13