jxl读取excel文件

package com.excel;

import java.io.File;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
/**
 * jxl对excel读取工具类
 * @author huweijun
 * @date 2015-01-29 22:22:22
 *
 */
public abstract class JxlExcelHandler {
	/**
	 * 文件路径
	 */
	private File file;
	/**
	 * 第几个工作表(从0开始,默认为0)
	 */
	private int sheetIndex = 0;
	/**
	 * 第几行(从0开始,默认为0)
	 */
	private int rowIndex = 0;
	
	public JxlExcelHandler(String path){
		this.file = new File(path);
	}
	
	/**
	 * @param path		xls文件路径
	 * @param rowIndex		从第几行开始(默认0)
	 * @author huweijun
	 * @date 2015-01-29 22:22:22
	 */
	public JxlExcelHandler(String path,int rowIndex){
		this.file = new File(path);
		this.rowIndex = rowIndex;
	}
	
	/**
	 * @param path		xls文件路径
	 * @param rowIndex	从第几行开始(默认0)
	 * @param sheetIndex	从第几个工作表开始(默认0)
	 * @author huweijun
	 * @date 2015-01-29 22:22:22
	 */
	public JxlExcelHandler(String path,int rowIndex,int sheetIndex){
		this.file = new File(path);
		this.rowIndex = rowIndex;
		this.sheetIndex = sheetIndex;
	}
	/**
	 * 执行
	 * @author huweijun
	 * @date 2015-01-29 22:22:22
	 */
	public void print(){
		Workbook book = null;
		try {
			book = Workbook.getWorkbook(file);
			Sheet sheet = book.getSheet(sheetIndex);
			int totalRow = sheet.getRows();
			for (int i=rowIndex; i<totalRow-1; i++) {
				handlerRow(sheet.getRow(i),i,totalRow-1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(book != null){
				book.close();
			}
		}
	}
	/**
	 * 从0遍历cell
	 * @param row 一行的所有cell
	 * @param index 第几行(从0开始)
	 * @param totalRow 总共多少行
	 * @author huweijun
	 * @date 2015-01-29 22:22:22
	 */
	public abstract void handlerRow(Cell[] row,int index,int totalRow);
	
}

package com.excel;


import jxl.Cell;
/**
 * 测试jxl对excel读取
 * @author huweijun
 * @date 2015-01-29 22:22:22
 *
 */
public class JxlExcelTest{


public static void main(String[] args) {
String file = "C:\\Users\\Administrator\\Desktop\\test.xls";
final int startRow = 1;
new JxlExcelHandler(file,startRow) {
@Override
public void handlerRow(Cell[] row, int index,int totalRow) {
String msg = row[0].getContents()+"  "+row[1].getContents()+"  "+row[2].getContents()+" 第 "+(index+1)+
" 行 总共:" +totalRow+" 行;  总共读取了:"+(totalRow-startRow)+"行.";
System.out.println(msg);
}
}.print();
}
}



jar包下载==>>>







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值