/** * */ package com.user.util; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * @author Li Shuchao * 2013-8-9 下午02:48:54 2013 */ public class TestExcelConnection { private static String driverName = "sun.jdbc.odbc.JdbcOdbcDriver"; private static String dbURL = "jdbc:odbc:driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=D:\\importDemo.xlsx"; // 不设置数据源 // private static String dbURL="jdbc:odbc:ExcelTest"; //数据源连接方式 DSN:ExcelTest private static Connection dbConn = null; /** * @param args importDemo.xlsx * @throws UnsupportedEncodingException */ public static void main(String[] args) throws UnsupportedEncodingException { try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, "", ""); Statement smt = dbConn.createStatement(); ResultSet set = smt.executeQuery("select * from [sheet1$]"); int i =0; while (set.next()) { System.out.print(i + "\t") ; String str1 = new String(set.getBytes(1),"gbk"); //set.getString(1); String str2 = new String(set.getBytes(2),"gbk"); String str3 = new String(set.getBytes(3),"gbk"); if (str1 != null){ System.out.print( str1 + "\t");//new String(str1.getBytes("ISO-8859-1") ,"gbk") + "\t") ; } if (str2 != null){ System.out.print( str2 + "\t") ; } if (str3 != null){ System.out.println( str3 + "\t") ; } i ++; } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { dbConn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
?