tags:

views:

346

answers:

3

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.

+1  A: 

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"));
workbook.setProtected(false);
WritableWorkbook copy = Workbook.createWorkbook(new File("/path/to/unprotected.xls"), workbook);
WritableSheet[] sheets = copy.getSheets();

for (WritableSheet sheet : sheets){
    sheet.getSettings().setProtected(false);
}

copy.write();
copy.close();

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

Kevin Crowell
Awesome, I'll give this a shot.
dhorn
+1  A: 

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 {
            Class.forName(DRIVER);
            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 ) ;

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

        try {
            statement.close();
            resultSet.close();
        }
        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 ;
}
Wajdy Essam
A: 

You can try SmartXLS for Java,it can encrypt/decrypt xls file.

WorkBook workbook = new WorkBook();
workbook.read("file.xls","yourpassword");
liya