package
com.xiaoye.demo;
import
java.io.FileInputStream;
import
java.io.InputStream;
import
org.apache.poi.hssf.usermodel.HSSFCell;
import
org.apache.poi.hssf.usermodel.HSSFRow;
import
org.apache.poi.hssf.usermodel.HSSFSheet;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook;
import
org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* 遍历工作薄的行和列取值
* @author 小夜的传说
* 2014-2-21 上午11:07:41
*/
public
class
ThrRowCell {
public
static
void
main(String[] args)
throws
Exception {
InputStream in=
new
FileInputStream(
"d://测试文件.xls"
);
POIFSFileSystem fs=
new
POIFSFileSystem(in);
HSSFWorkbook wb=
new
HSSFWorkbook(fs);
HSSFSheet hssfSheet=wb.getSheetAt(
0
);
if
(hssfSheet==
null
){
return
;
}
for
(
int
rowNum=
0
;rowNum<=hssfSheet.getLastRowNum();rowNum++){
HSSFRow hssRow=hssfSheet.getRow(rowNum);
if
(hssRow==
null
){
continue
;
}
for
(
int
cellNum=
0
;cellNum<=hssRow.getLastCellNum();cellNum++){
HSSFCell hssfCell=hssRow.getCell(cellNum);
if
(hssfCell==
null
){
continue
;
}
System.out.print(
"--"
+getValue(hssfCell));
}
System.out.println();
}
}
private
static
String getValue(HSSFCell hssfCell){
if
(hssfCell.getCellType()==HSSFCell.CELL_TYPE_BOOLEAN){
return
String.valueOf(hssfCell.getBooleanCellValue());
}
else
if
(hssfCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
return
String.valueOf(hssfCell.getNumericCellValue());
}
else
{
return
String.valueOf(hssfCell.getStringCellValue());
}
}
}