需要导入poi-3.9-20121203.jar
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;
public class Poi3 {
public static void main(String[] args) throws Exception {
//创建读取流
InputStream fs = new FileInputStream("d:\\简单的工作簿.xls");
//包装
POIFSFileSystem pfs = new POIFSFileSystem(fs);
//创建一个工作簿
HSSFWorkbook wb = new HSSFWorkbook(pfs);
//获取第一个sheet
HSSFSheet hssfSheet = wb.getSheetAt(0);
if(hssfSheet == null){
return;
}
//遍历所有行
for(int r = 0;r <= hssfSheet.getLastRowNum();r++){
HSSFRow hssfRow = hssfSheet.getRow(r);
if(hssfRow == null){
continue;
}
//遍历列
for(int c = 0;c <= hssfRow.getLastCellNum();c++){
HSSFCell hssfCell = hssfRow.getCell(c);
if(hssfCell == null){
continue;
}
System.out.print(" "+getValue(hssfCell));
}
//换行
System.out.println();
}
}
//value转换
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());
}
}
读取的文件
}
读取后