Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Default Re: Read pasword protected Excel file

    Hi With Apache POI it is possible to password protect an excel file. PFB for the code. But it is working with XLSX extension [2010 Excel format], But not with XLS [2003 Excel format]
    Can you please give an idea of how to accomplish the same

    package com.excel.utility;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.security.GeneralSecurityException;

    import org.apache.poi.openxml4j.exceptions.InvalidFormatE xception;
    import org.apache.poi.openxml4j.opc.OPCPackage;
    import org.apache.poi.openxml4j.opc.PackageAccess;
    import org.apache.poi.poifs.crypt.EncryptionInfo;
    import org.apache.poi.poifs.crypt.EncryptionMode;
    import org.apache.poi.poifs.crypt.Encryptor;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;

    public class ReadPasswordProtectedXLSX {
    public static void main(String[] args) throws IOException,
    InvalidFormatException, GeneralSecurityException {

    // Add password protection and encrypt the file
    POIFSFileSystem fs = new POIFSFileSystem();
    EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);

    Encryptor enc = info.getEncryptor();
    enc.confirmPassword("Password1");

    OPCPackage opc = OPCPackage.open(new File(
    "C:\\Users\\Desktop\\Book1.xlsx"),
    PackageAccess.READ_WRITE);
    OutputStream os = enc.getDataStream(fs);
    opc.save(os);
    opc.close();

    FileOutputStream fos = new FileOutputStream(
    "C:\\Users\\Desktop\\Book1.xlsx");
    fs.writeFilesystem(fos);
    fos.close();

    System.out.println("File created!!");
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Read pasword protected Excel file

    Moved post from this 5 year old thread.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Read pasword protected Excel file

    Actually, it was copied over not moved (it is still there).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Read pasword protected Excel file

    I closed the other thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Read pasword protected Excel file

    Ah, well, close enough.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Read pasword protected Excel file
    By vijay7539 in forum Advanced Java
    Replies: 2
    Last Post: 08-01-2014, 03:46 PM
  2. Replies: 0
    Last Post: 02-21-2014, 11:04 AM
  3. Replies: 0
    Last Post: 01-15-2014, 07:27 AM
  4. Please help with Pasword protected page Java script..!
    By boyfrom75 in forum Forum Lobby
    Replies: 3
    Last Post: 11-15-2011, 08:13 PM
  5. How to Read Excel file??
    By spalax in forum New To Java
    Replies: 3
    Last Post: 08-15-2009, 05:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •