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 Ja
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 ;
}