Friday, January 15, 2010

How Java get data from Excell?

ava Excel API既可以从本地文件系统的一个文件(.xls),也可以从输入流中读取Excel数据表。读取Excel数据表的第一步是创建Workbook(术 语:工作薄),下面的代码片段举例说明了应该如何操作:

import java.io.*;

import jxl.*;

… … … …

try

{

//从输入流创建Workbook

InputStream is = new FileInputStream(sourcefile);

jxl.Workbook workbook = Workbook.getWorkbook(is);

}

catch (Exception e)

{

e.printStackTrace();

}




上面是从输入流中读取Excel数据表,下面则是从本地文件中读取:

////直接从本地文件创建Workbook

Workbook workbook = Workbook.getWorkbook(new File(excelfile));


一旦创建了Workbook,我们就可以通过它来访问Excel Sheet(术语:工作表)。参考下面的代码片段:

//获取第一张Sheet表

Sheet rs = workbook.getSheet(0);




我们既可能通过Sheet的名称来访问它,也可以通过下标来访问它。如果通过下标来访问的话,要注意的一点是下标从0开始,就像数组一样。

一旦得到了Sheet,我们就可以通过它来访问Excel Cell(术语:单元格)。参考下面的代码片段:

//获取第一行,第一列的值

Cell c00 = rs.getCell(0, 0);

String strc00 = c00.getContents();

//获取第一行,第二列的值

Cell c10 = rs.getCell(1, 0);

String strc10 = c10.getContents();



//获取第二行,第二列的值

Cell c11 = rs.getCell(1, 1);

String strc11 = c11.getContents();



System.out.println("Cell(0, 0)" + " value : " + strc00 + "; type : " + c00.getType());

System.out.println("Cell(1, 0)" + " value : " + strc10 + "; type : " + c10.getType());

System.out.println("Cell(1, 1)" + " value : " + strc11 + "; type : " + c11.getType());




如果仅仅是取得Cell的 值,我们可以方便地通过getContents()方法,它可以将任何类型的Cell值都作为一个字符串返回。示例代码中Cell(0, 0)是文本型,Cell(1, 0)是数字型,Cell(1,1)是日期型,通过getContents(),三种类型的返回值都是字符型。

如果有需要知道Cell内容的确切类型,API也提供了一系列的方法。参考下面的代码片段:

String strc00 = null;

double strc10 = 0.00;

Date strc11 = null;



Cell c00 = rs.getCell(0, 0);

Cell c10 = rs.getCell(1, 0);

Cell c11 = rs.getCell(1, 1);



if(c00.getType() == CellType.LABEL)

{

LabelCell labelc00 = (LabelCell)c00;

strc00 = labelc00.getString();

}

if(c10.getType() == CellType.NUMBER)

{

NmberCell numc10 = (NumberCell)c10;

strc10 = numc10.getValue();

}

if(c11.getType() == CellType.DATE)

{

DateCell datec11 = (DateCell)c11;

strc11 = datec11.getDate();

}



System.out.println("Cell(0, 0)" + " value : " + strc00 + "; type : " + c00.getType());

System.out.println("Cell(1, 0)" + " value : " + strc10 + "; type : " + c10.getType());

System.out.println("Cell(1, 1)" + " value : " + strc11 + "; type : " + c11.getType());




在得到Cell对象后,通过 getType()方法可以获得该单元格的类型,然后与API提供的基本类型相匹配,强制转换成相应的类型,最后调用相应的取值方法getXXX(),就可以得到确定类型的值。API提供了以下基本类型,与Excel的数据格式相对应,如下图所示:



每种类型的具体意义,请参见Java Excel API Document。

我是循环取出全部数据,并转化为相应格式:

int rows = sheet.getRows();

for (int i = 1; i < rows; i++) {

Cell cb1 = sheet.getCell(0, i);

Cell cb2 = sheet.getCell(1, i);

Cell num3 = sheet.getCell(2, i);

Cell num4 = sheet.getCell(3, i);



String user = "";

String rule = "";

int numNew = 0;

int numEdit = 0;

if (cb1.getType() == CellType.LABEL) {

LabelCell lc = (LabelCell) cb1;

user = lc.getString();

}

if (cb2.getType() == CellType.LABEL) {

LabelCell lc = (LabelCell) cb2;

rule = lc.getString();

}



if (num3.getType() == CellType.NUMBER_FORMULA) {

NumberFormulaCell nc = (NumberFormulaCell) num3;

try {

numNew = Double.valueOf(nc.getFormula()).intValue();

} catch (FormulaException e) {

e.printStackTrace();

}

}

if (num4.getType() == CellType.NUMBER_FORMULA) {

NumberFormulaCell nc = (NumberFormulaCell) num4;

try {

numEdit = Double.valueOf(nc.getFormula()).intValue();

} catch (FormulaException e) {

e.printStackTrace();

}

}

}


当你完成对Excel电子表格数据的处理后,一定要使用close()方法来关闭先前创建的对象,以释放读取数据表的过程中所占用的内存空间,在读取大量数据时显得尤为重要。参考如下代码片段:

//操作完成时,关闭对象,释放占用的内存空间

workbook.close();




Java Excel API提供了许多访问Excel数据表的方法,在这里我只用到了几个,其它的,以后可以慢慢看API吧.我也会慢慢看的.以后看看写Excel的操作再写上来吧.

No comments:

Post a Comment