Take the 2-minute tour ×

I have an excel spreadsheet that is password-protected. I need to open this spreadsheet and read the data from it. I've been attempting to use the POI API to no avail. A Java solution would be preferred but any ideas would be helpful.

Edit: Yes, I have the password. The file is password protected in excel; a password must be entered to view the spreadsheet.

Edit2: I am unable to open it with POI with the password, I am looking for an alternate solution.

share|improve this question
It's not clear from your question whether you actually have the password and just having problems transmitting it to POI. It also seems like a duplicate of stackoverflow.com/questions/1204382/… –  Uri Apr 9 '10 at 16:59
Excel has several protection mechanisms where a password is used. Are you referring to the file-protection, allowing the file to be opened only with the supplied password? –  M.A. Hanin Apr 9 '10 at 17:06
See edits regarding questions. –  dhorn Apr 9 '10 at 17:14
add comment

4 Answers

up vote 1 down vote accepted

You can use JExcelApi.

It has been a while since I have done this, so I may not be telling you how to do it correctly, but there is definitely a way to do this using JExcelApi. Try the source below:

Workbook workbook = Workbook.getWorkbook(new File("/path/to/protected.xls"));
WritableWorkbook copy = Workbook.createWorkbook(new File("/path/to/unprotected.xls"), workbook);
WritableSheet[] sheets = copy.getSheets();

for (WritableSheet sheet : sheets){


Of course, you will need to import necessary classes and catch necessary exceptions.

share|improve this answer
Awesome, I'll give this a shot. –  dhorn Apr 9 '10 at 17:28
Is there any method to set password on excel?? –  happy Jun 27 '12 at 9:23
@happy Yes, check the SheetSettings class: jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/… –  Kevin Crowell Jun 27 '12 at 15:07
I tried this but I am not able to set the password.Please see stackoverflow.com/questions/11256259/… –  happy Jun 29 '12 at 4:58
add comment

POI should be able to open both protected xls files (using org.apache.poi.hssf.record.crypt) and protected xlsx files (using org.apache.poi.poifs.crypt). Have you tried these?

If you're using HSSF (for a xls file), you need to set the password before opening the file. You do this with a call to:


After that, HSSF should be able to open your file.

For XSSF, you want something like:

    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("protect.xlsx"));
    EncryptionInfo info = new EncryptionInfo(fs);
    Decryptor d = new Decryptor(info);
    XSSFWorkbook wb = new XSSFWorkbook(d.getDataStream(fs));
share|improve this answer
This looks most correct for POI. EncryptionInfo was introduced in POI in v3.7 or v3.8 –  Jeromy Evans Aug 16 '11 at 9:09
how to set password on the unprotected excel file? Is there any method in this org.apache.poi.hssf.record.crypto.Biff8EncryptionKey package –  happy Jun 26 '12 at 8:21
add comment

addthe excel file in ODBC Sources (from control panel->Administrative Tools) and then execute the code:

// program to extract data from excel file

import java.sql.Connection ;
import java.sql.Statement  ;
import java.sql.ResultSet  ;
import java.sql.ResultSetMetaData ;
import java.sql.DriverManager ;
import java.sql.SQLException ;

public class ExtractExcelData {

    public static void main (String[] args) {
        try {
            connection = DriverManager.getConnection(URL,userName,password);
        catch (ClassNotFoundException cnfe) {
            System.err.println("unable to load excel  driver");
            return  ;
        catch (SQLException se) {
            System.err.println("cannot connect to excel file");
            return  ;

        try {
            statement = connection.createStatement();
            String select = "SELECT * FROM [Sheet1$]";
            resultSet = statement.executeQuery(select);
            metaData = resultSet.getMetaData();

            int count = metaData.getColumnCount();
            while ( resultSet.next() ) {

                String col1 =  resultSet.getString(1) ; 
                String col2 =  resultSet.getString(2) ; 
                String col3 =  resultSet.getString(3) ; 

                System.out.println( col1 ) ;
                System.out.println( col2 ) ;
                System.out.println( col3 ) ;

        catch (SQLException se) {
            System.err.println("cannot execute query");
            return ;

        try {
        catch (SQLException se ) {
            System.err.println("unable to close excel file");
            return  ;

    private static final String userName = "" ;
    private static final String password = "" ;
    private static final String URL = "jdbc:odbc:testexcel" ;
    private static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver" ;

    private static Connection connection ;
    private static Statement statement ;
    private static ResultSet resultSet ;
    private static ResultSetMetaData metaData ;
share|improve this answer
This Windows only solution can't be recommended. –  Jeromy Evans Aug 16 '11 at 9:11
add comment

I tried to set password for excel file from java script, this script will only work on IE and Excel get should installed in the client system.

function setPasswordToExcel(password,excelFileName,newFileName)
   var Excel;
    Excel = new ActiveXObject("Excel.Application"); 
    Excel.Visible = false;
    var obj = Excel.Workbooks.Open(excelFileName);
    obj.Password =password;
    return 1;
share|improve this answer
add comment

Your Answer


