java操作Excel的poi 遍历一个工作簿
遍历一个工作簿
package com.java.poi; 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; import java.io.FileInputStream; import java.io.InputStream; /** * 遍历一个工作簿 * @author nidegui * @create 2019-06-17 9:46 */ public class Test5 { public static void main(String[] args) throws Exception { InputStream is=new FileInputStream("E:\\2.xls"); POIFSFileSystem fs=new POIFSFileSystem(is); //接受一个输入流 HSSFWorkbook wb=new HSSFWorkbook(fs);//创建了一个工作簿 HSSFSheet hssfSheet=wb.getSheetAt(0); //获取第一个sheet页 if(hssfSheet==null){ return; } //遍历行row for (int rownum=0;rownum<=hssfSheet.getLastRowNum();rownum++){ HSSFRow sheetRow = hssfSheet.getRow(rownum); if(sheetRow==null){ continue; } //遍历列cell for (int cellnum=0;cellnum<=sheetRow.getLastCellNum();cellnum++){ HSSFCell cell = sheetRow.getCell(cellnum); if(cell==null){ continue; } System.out.print( " "+getValue(cell)); } System.out.println(); } } /** * 静态 * @param hssfCell * @return */ 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()); } } }