import java.io.FileInputStream; import java.io.IOException; import java.util.Iterator;
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; /**
* 简单读取Excel
* 需要poi-3.8-20120326.jar
* 下载地址http://dipoo.j1.fjjsp.net/ * @author Feww */
public class ReadExcel {
/** * 测试
* @param args
* @throws IOException */
public static void main(String[] args) throws IOException{ String filePath = \"E:\\\\xxx\\\\xxx.xls\";//文件路径 getExcel(filePath); } /**
* 读取Excel表 * @return */
public static void getExcel(String filePath) throws IOException{ //获取.xls文件输入流
FileInputStream fis = new FileInputStream(filePath); //获取POI文件系统对象
POIFSFileSystem poi = new POIFSFileSystem(fis); //获取Excel文件对象
HSSFWorkbook hwb = new HSSFWorkbook(poi); //获取第几个excel表
HSSFSheet sheet0 = hwb.getSheetAt(0); /*
* 遍历行 */
Iterator rows = sheet0.rowIterator(); while(rows.hasNext()){ //获取每行
HSSFRow hr = (HSSFRow)rows.next();
}
}
}
//System.打印行号out
.println(\"ROW\"+hr.getRowNum()+\":\"); /*
* 遍历列 */
Iterator cells = hr.cellIterator(); while(cells.hasNext()){ //获取列
HSSFCell hc = (HSSFCell)cells.next(); //System.打印列号out
.print(\"CELL\"+hc.getCellNum()+\":\"); /*
* 判断列类型 */switch
(hc.getCellType()){ //booleancase HSSFCell.类型
CELL_TYPE_BOOLEAN:
System.out.println(hc.getBooleanCellValue()); break; //numbercase HSSFCell.类型
CELL_TYPE_NUMERIC:
System.out.println(hc.getNumericCellValue()); break; //stringcase HSSFCell.类型
CELL_TYPE_STRING:
System.out.println(hc.getStringCellValue()); break; //formulacase HSSFCell.公式
CELL_TYPE_FORMULA:
System.out.println(hc.getCellFormula()); break; //.... //default否则 :
System.out.println(\"NoType for cell !\"); break; } }
因篇幅问题不能全部显示,请点此查看更多更全内容