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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从excel读取(utf-8)字符串与odbc相关的知识,希望对你有一定的参考价值。

我想使用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();
答案
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)+"
";
                 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

以上是关于从excel读取(utf-8)字符串与odbc的主要内容,如果未能解决你的问题,请参考以下文章

Qt从excel导入数据-ODBC

从代码更改/创建 ODBC 连接

使用 UCanAccess 代替 JDBC-ODBC Bridge 来读取 excel 表

刷新每个查询所用的 Excel ODBC 数据连接查询时间

来自 Access ODBC 驱动程序的无效日期时间格式异常

使用 ODBC 从 Java 读取 Visual Foxpro 数据