从excel读取(utf-8)字符串与odbc

时间:2016-02-13 08:55:36

标签: excel odbc

我想使用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();

1 个答案:

答案 0 :(得分: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