b.在用户DSN选项卡中点击添加,选择Driver do Microsoft Excel(*.xls)
注册好数据源后就可以写代码了,一个示例如下:
public class ExcelReader { private String entry;//ODBC数据源名称 public ExcelReader(String entry) { this.entry = entry; } //sheetName为工作表名称 public String read(String sheetName) throws Exception { StringBuilder builder = new StringBuilder(); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); Connection connection = DriverManager.getConnection("jdbc:odbc:" + entry); Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery("select * from [" + sheetName + "$]"); while(rs.next()) { builder.append(rs.getObject(1));//示意性只读取一列 } rs.close(); statement.close(); connection.close(); return builder.toString(); } public static void main(String[] args) throws Exception { ExcelReader reader = new ExcelReader("etl"); System.out.println(reader.read("test")); } }
原文地址:http://blog.csdn.net/xtayfjpk/article/details/40476697