2016-02-13 64 views
1

欲讀取使用ODBC,從Excel文件字符串時我與下面的代碼讀取它們,像讀數(UTF-8)從Excel字符串ODBC

所示?? ??? ???????? ??? ?????? ?? ? ? ??? ? ?

我的代碼:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

    Connection conn = DriverManager.getConnection("jdbc:odbc:excelD"); 
    Statement stm = conn.createStatement(); 
    ResultSet rs = stm.executeQuery("select * from [sheet3$]"); 
    BufferedWriter out = new BufferedWriter(new FileWriter(new File("Resullt.txt"),true)); 

    while (rs.next()) { 
     out.write((rs.getString(1))); 
    } 
    rs.close(); 
    stm.close(); 

回答

0
String DATABASE_URL = "jdbc :o dbc :D river={Microsoft Excel Driver (*.xls)}; DriverID=22; READONLY=false;"; 
String DRIVER_NAME = "sun.jdbc.odbc.JdbcOdbcDriver"; 
String infile="ChinJap.xls"; 
String tabSheet = "multibyte"; 

    try 
    { 
     Properties info = new Properties(); 
     info.put("encoding", "utf-8"); 
     //info.put("charset", "ISO-8859-1"); 
     Class.forName(DRIVER_NAME); 
     conn = DriverManager.getConnection(DATABASE_URL + "DBQ=" + infile,info); 
     Statement stmt = conn.createStatement(); 
     ResultSet rs = stmt.executeQuery("select English,Japanese,Chinese from [" + tabSheet+ "$]"); 
     FileOutputStream fos=new FileOutputStream("temp33.txt"); 
     Writer writer = new OutputStreamWriter(fos,"utf-8"); 
     if(rs.next()){ 
      do 
      { 
       try 
       { 
       String temp = rs.getString(2)+"\n"; 
       System.out.println(temp); 
       writer.write(temp); 
       }catch(Exception ex){ 
       System.out.println(ex.getMessage()); 
       } 
      }while(rs.next()); 
      writer.close(); 
     }//end of if 
    }catch(Exception ex){ 
     System.out.println(ex.getMessage()); 
    }//end of try 
+1

到上你的解決方案的描述將是一件好事。 –