java读取excel 文件 ->jxl读取excel中文乱码有关问题

java读取excel 文件 ---------->jxl读取excel中文乱码问题

java读取excel文件有两个比较好用的插件poi和jxl


目前poi最新版本是:poi-bin-3.7      jxl:jexcelapi_2_6_11


今天用jxl写了一个读取excel 的例子

 

 

package com.util;

 

import java.io.FileInputStream;

import java.io.InputStream;

 

import jxl.Cell;

import jxl.Sheet;

import jxl.Workbook;

import jxl.WorkbookSettings;

 

public class Excel {

 

/**

 * @param args

 */

public static void main(String[] args) {

 

try {

WorkbookSettings ws = new WorkbookSettings();

// ws.setEncoding("UTF-8");

InputStream is = new FileInputStream("d:/aaa.xls"); // savePath是文件的绝对路径如c:/aa.xls

 

jxl.Workbook wb = Workbook.getWorkbook(is); // 得到工作薄

 

jxl.Sheet[] sts = wb.getSheets(); // 获得所有的工作表

 

for (jxl.Sheet st : sts) { // 得到工作薄中的第一个工作表 (有多个表的时候遍历sts)

int rsRows = st.getRows(); // 得到excel的总行数

int columncount = st.getColumns();// 获得excel的总列数

System.out.println("共" + rsRows + "行 " + columncount + "列");

for (int i = 0; i < rsRows; i++) {

Cell cell0 = st.getCell(0, i);// 得到工作表的第一个单元格,即A1

Cell cell1 = st.getCell(1, i);// 得到工作表的第二个单元格,即B1

String str1 = cell0.getContents(); // 获得单元格内的内容

String str2 = cell1.getContents(); // 获得单元格内的内容

System.out.println(str1);

System.out.println(str2);

System.out.println(new String(str2.getBytes("GBK"),"UTF-8"));

System.out.println(new String(str2.getBytes("GB2312")));

}

}

 

Sheet st1 = wb.getSheet(2);// 获得第一个工作薄

 

Sheet stname = wb.getSheet("user");// 获得指定工作簿

 

} catch (Exception e) {

e.printStackTrace();

}

}

 

}


在这期间出现了一个让人头疼的问题,那就是中文乱码!!! 好生郁闷

各种编码格式ISO-8859-1,UTF-8,GB2312,GBK 都试过来完还是不能用

最后我换了一下JDK 竟然任何过滤方式都不要就出来了

折腾死我了  唉  分享一下

下面是poi 和jxl的jar包  供大家下载使用相互学习