Java 导入EXCEL

导入Excel文件,可以导入每个Sheet里面的内容,用的是Apache POI,Jar包和详细讲解在上一篇到处里面有 http://jiaozhiguang-126-com.iteye.com/blog/1673965,附件是测试用的excel文件

package org.leno.importexcel;

import java.io.FileInputStream;
import java.io.IOException;

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 ImportExcel {
	public void readExcel(String sFilePath) {
		System.out.println(sFilePath) ;

		POIFSFileSystem fs = null;
		HSSFWorkbook wb = null;
		try {
			
			fs = new POIFSFileSystem(new FileInputStream(sFilePath));
			wb = new HSSFWorkbook(fs);

			// sheet数
			int iSheets = wb.getNumberOfSheets();
			for (int i = 0; i < iSheets; i++) {
				HSSFSheet sheet = wb.getSheetAt(i);
				String sSheetName = wb.getSheetName(i);

				HSSFRow row = null;
				HSSFCell cell = null;

				int rowNum = sheet.getLastRowNum();
//				if(i==0){//公司领导正职
					for (int j = 1; j <= rowNum; j++) {
						row = sheet.getRow(j);
						if (row != null) {
							
							cell = row.getCell( 1);//0是序号从1开始所属公司名称
							if (cell != null) {
								System.out.println(cell.getRichStringCellValue()
										.getString()) ;
							
							}
							cell = row.getCell( 2);//所属公司缩写
							if (cell != null) {
								System.out.println(cell.getRichStringCellValue()
										.getString()) ;
								
							}
							cell = row.getCell( 3);//姓名
							if (cell != null) {
								System.out.println(cell.getRichStringCellValue()
										.getString()) ;
								
							}
							cell = row.getCell( 4);//UID
							if (cell != null) {
								System.out.println(cell.getRichStringCellValue()
										.getString()) ;
								
							}
							cell = row.getCell( 5);//手机号
							if (cell != null) {
								System.out.println(cell.getRichStringCellValue()
										.getString()) ;
								
							}
							cell = row.getCell( 6);//职位名称
							if (cell != null) {
								System.out.println(cell.getRichStringCellValue()
										.getString()) ;
								
							}
							
						}
					}
//				}
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String args[]){
		ImportExcel ie = new ImportExcel() ;
		ie.readExcel("F:\\Project\\performance_leader\\WebRoot\\uploadfild\\leadertemplet.xls") ;
		
	}
}



Java 导入EXCEL_第1张图片

输出结果:

F:\Project\performance_leader\WebRoot\uploadfild\leadertemplet.xls
sSheetName is : 公司领导正职
中国实话
zgsh
张三
aaa
1213123123
总经理
中国实话
zgsh
里斯
aaa
1213123123
总经理
sSheetName is : 公司领导副职
中国银行
zgyh
张四
bbbb
1213123123234234
副总经理
sSheetName is : 资深经理
中国假话
zgjh
张五
ccc
1213123123
资深经理
sSheetName is : 公司中层及地市负责人
中国谎话
zgsh
张六
ddd
1213123123
中层
sSheetName is : 职工代表
中国实话
zgsh
张七
eee
1213123123
戴表

你可能感兴趣的:(java,Excel)