利用odbc连接excel,解决中文乱码问题。

1.Excel

名称:test.xls

内容如下:

专业名称 申请学位 姓名 毕业学校 毕业专业
图形学 硕士 曾宏波 中国科学技术大学 数学
线性代数 硕士 樊昊霏 北京大学 基础数学
离散数学 硕士 傅二娟 华东师范大学 数学与应用数学
概率学 硕士 姜清元 北京大学 基础数学
拓扑学 硕士 吕健成 北京大学 数学
基础数学 硕士 谭新文 北京大学 基础
基础数学 硕士 王式柔 辽宁大学 数学与应用数学
基础数学 硕士 熊欢 北京大学 数学系
基础数学 硕士 余孟洋 北京大学 基础数学
基础数学 硕士 张蓉蓉 兰州大学 基础理论
基础数学 硕士 赵沨 北京大学 数学
基础数学 硕士 邹德阳 山东大学 数学与应用数学

2.程序

由于是从别的地方改的,设计模式规划的有点乱,先记录下来:

package com.maggie.odbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class ExcelConnection {
	protected static String driverName = "sun.jdbc.odbc.JdbcOdbcDriver"; 
	private static String root = ExcelConnection.class.getResource("").getPath().substring(1);
	private static Connection conn = null;
	private Statement stmt = null;
	
	public ExcelConnection(String filename) { 
		String  urlString = "";
		try {
			if (filename != null && filename.indexOf(".xls") != -1) {
				urlString = "jdbc:odbc:driver={Microsoft Excel Driver (*.xls)};DBQ="
						+ root + filename;
				System.out.println(urlString);
			} else {
				throw new Exception("");
			}
			if (conn == null) {
				Class.forName(driverName).newInstance();
				System.out.println("Success loading postgresql Driver!");
				conn = DriverManager.getConnection(urlString, "", "");
			} else
				return;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Statement createStatement() {
		try {
			if(stmt == null || stmt.isClosed()){
				stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_UPDATABLE);
			}			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stmt;
	}

	public ResultSet executeQuery(String sql) {
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {

		}
		return rs;
	}

	public void closeStmt() {
		try {
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			stmt = null;
		}
	}

	public void closeConn() { 
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			conn = null;
		}
	}

	public void test_sql1() {
		//excel的第一行作为列名,因此查询的结果是从第二行开始。
		String sql = "select *  from [sheet1$] where 专业名称='基础数学'";
		this.createStatement();//创建Statement。
		ResultSet rs = this.executeQuery(sql);
		try {
			while (rs.next()) {
				//rs的起始索引为1,对应excel中第一列的值
				System.out.println(rs.getString(3));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.closeStmt();
		this.closeConn();
	}

	public static void main(String args[]) {
		ExcelConnection test = new ExcelConnection("test.xls");
		test.test_sql1();
	}
}


3.中文乱码

我的系统是windows_xp中文系统,excel是默认安装的office2003.

【问题描述】:

程序本身以UTF-8编码时:

运行程序后,得到的结果无论怎么转码都得不到中文。形式如:???&。

当尝试用new String(s.getBytes("UTF-8"),"GBK")时,有些仅能得到???学

【问题解决】:

将程序本身的编码定义为GBK,

则可以得到正确的中文值,而无需进行任何转码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值