jxl读取excel文件

来源:互联网 发布:淘宝假货可以不退货吗 编辑:程序博客网 时间:2024/05/16 09:19

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 pathxls文件路径 * @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 pathxls文件路径 * @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) {@Overridepublic 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
原创粉丝点击