excel 解析 java_用Java解析Excel文件

packagecom.fyf.test;importjava.io.FileInputStream;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;public classPoiRead {public static voidmain(String[] args) {//TODO Auto-generated method stub

try{

FileInputStream fStream= new FileInputStream("D:/test.xls");

Workbook wb= newHSSFWorkbook(fStream);

Sheet sheet= wb.getSheetAt(0);

//因为Sheet接口继承了 java.lang.Iterable接口所以,遍历表中的行可以一用foreach很方便for(Row row : sheet) {

//跳过空行if (row==null) {continue;

}

System.out.print("row:"+row.getRowNum()+"\t");

//同理行中的单元格也可以用foreach遍历for(Cell cell : row) {if (cell==null) {continue;

}

//对cell进行判断后输出

System.out.print("|"+getStringCell(cell)+"\t|");

}

System.out.println();

}

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

}public staticString getStringCell(Cell cell) {

String result= "";int cellType =cell.getCellType();switch(cellType) {caseCell.CELL_TYPE_BOOLEAN:

result=String.valueOf(cell.getBooleanCellValue());break;caseCell.CELL_TYPE_NUMERIC:

//这里将数组作为日期返回

result=String.valueOf(cell.getDateCellValue());break;caseCell.CELL_TYPE_STRING:

result=cell.getStringCellValue();break;default:break;

}returnresult;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值