- package cn.CodeSnippet.utilit;
- import java.io.FileInputStream;
- import java.io.IOException;
- import org.apache.poi.hssf.usermodel.*;
- import org.apache.poi.poifs.filesystem.POIFSFileSystem;
- /**
- * @author la
- *POI工具读取Excel文件
- */
- public class ParseExcel {
- private void parseExcel(String excelFile)throws IOException {
- POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(excelFile));//打开Excel文件
- HSSFWorkbook wbHssfWorkbook=new HSSFWorkbook(fs);//打开工作薄
- HSSFSheet sheet=wbHssfWorkbook.getSheetAt(0);//打开工作表
- HSSFRow row=null;
- String data=null;
- for (int i = 0; i <=sheet.getLastRowNum(); i++) {//循环读取每一行
- row =sheet.getRow(i);
- for (int j = 0; j <= row.getLastCellNum(); j++) {//循环读取每一列
- switch (row.getCell((short)j).getCellType()) {//判断单元格的数据类型
- case HSSFCell.CELL_TYPE_BLANK:
- data="";
- break;
- case HSSFCell.CELL_TYPE_NUMERIC:
- data=(long)row.getCell((short)j).getNumericCellValue()+"";
- break;
- default:
- data=row.getCell((short)j).getStringCellValue();
- break;
- }
- System.out.print(data+"\\t");
- }
- System.out.println();
- }
- }
- /**
- * @param args
- *2012-10-23
- *void
- */
- public static void main(String[] args)throws IOException {
- new ParseExcel().parseExcel(args[0]);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/281120137579.html
来源: http://www.codesnippet.cn/detail/281120137579.html