HSSFWorkBook读取excel表格获取数据。

来源:互联网 发布:欧莱雅淘宝旗舰店 编辑:程序博客网 时间:2024/06/05 17:42
public static void main(String [] args){
try {
HSSFWorkbook work = new HSSFWorkbook(new FileInputStream("e:/aa.xls"));// 得到这个excel表格对象
HSSFSheet sheet = work.getSheetAt(0);//得到第一个sheet
int rowNo = sheet.getLastRowNum();//得到行数
for (int i = 1; i < rowNo; i++) {
HSSFRow row = sheet.getRow(i);
HSSFCell cell1 = row.getCell((short) 1);
HSSFCell cell2 = row.getCell((short) 2);
HSSFCell cell3 = row.getCell((short) 3);
String ce1 = cell1 == null?"空":cell1.getStringCellValue();
String ce2 = cell2 == null?"空":cell2.getStringCellValue();
String ce3 = cell3 == null?"空":cell3.getStringCellValue();


System.out.println(ce1 + "\t" + ce2 + "\t" + ce3);

}
} catch (Exception e) {
e.printStackTrace();

}


注:蓝色字体部分,只是获取String 类型的内容,如果多种类型的话,可以见下面 :

 SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");

int tCellType=cell.getCellType();
                String msg="";
                if(tCellType==0){ // 0时 为数字
                if(j==3||j==4||j==11||j==12){//假定这几列为日期型数字
            msg = df.format((cell.getDateCellValue()));
                }else{
                msg =String.valueOf(cell.getNumericCellValue()); //数字取值方式。
                }
                }else{
                msg = cell.getStringCellValue();String 取值方式
                }

原创粉丝点击