Java控制excel,word文档只读模式

excel

  <!--poi依赖-->
        <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
        <!--<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.9</version>
        </dependency>-->


import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @description:
 * @author: Jhang
 * @time: 2020/12/4 14:51
 */

public class ee {

    public static void main(String[] args) {

            String str = "F:\\exce\\test\\test01.xlsx";
            String str1 = "F:\\exce\\test\\test02.xlsx";

          encryptionExcel(str,str1);
        }


        /**
         * encryptionExcel 加密excel
         * @param
         * @param
         */
        public static boolean encryptionExcel(String sourceFilePath, String destPath) {
            try {
                FileInputStream fis = new FileInputStream(new File(sourceFilePath)); // 这里换成你本地的excel相对路径或绝对路径

                FileOutputStream fos = new FileOutputStream(new File(destPath));// 这里换成你本地的excel相对路径或绝对路径

                if(sourceFilePath.substring(sourceFilePath.length()-4).equals(".xls")){
                    HSSFWorkbook workbook = new HSSFWorkbook(fis);
                    int sheetNumbers = workbook.getNumberOfSheets();
                    for (int i = 0; i < sheetNumbers; i++) {
                        HSSFSheet sheet =  workbook.getSheetAt(i);
                        sheet.protectSheet("111");
                    }

                    workbook.write(fos);
                    fis.close();
                    fos.close();

                }else if(sourceFilePath.substring(sourceFilePath.length()-5).equals(".xlsx")){
                    XSSFWorkbook workbook = new XSSFWorkbook(sourceFilePath);
                    int sheetNumbers = workbook.getNumberOfSheets();
                    for (int i = 0; i < sheetNumbers; i++) {
                        XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(i);
                        sheet.protectSheet("1111");
                    }
                    workbook.write(fos);
                    fis.close();
                    fos.close();

                }


            } catch (Exception e) {

            }

            return true;
        }

}

word

 		<dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>2.7.3</version>
        </dependency>

 			<repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
        <repository>
            <id>com.e-iceblue</id>
            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
 public void wordTest(){

            //加载测试文档  world 只读
            String input = "F:\\exce\\test\\test01.docx";
            String output= "F:\\exce\\test\\test02.docx";
            Document doc = new Document(input);

          //  doc.encrypt("123");//设置文档打开密码
            doc.protect(ProtectionType.Allow_Only_Reading,"123456");//设置文档只读密码123456/n
            //doc.protect(ProtectionType.Allow_Only_Comments,"123");//设置文档只允许添加批注
            //doc.protect(ProtectionType.Allow_Only_Form_Fields,"123");//只允许表单域
            //doc.protect(ProtectionType.Allow_Only_Revisions,"123");//只允许修订

            //保存加密后的文档
            doc.saveToFile(output);
            doc.dispose();
        }


注: 加密的文档用代码中设置的密码解不开 ,不知道是不是加密了

你可能感兴趣的:(Java控制excel,word文档只读模式)