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

问题描述 投票:1回答:1

我想使用ExcelODBC文件中读取字符串,当我用下面的代码读取它们时,显示为

?? ??? ???????? ??? ?????? ?? ? ? ??? ? ?

我的代码:

    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();
excel odbc
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.