利用jxl读取excel文件里面的内容


excel里面的内容:





读取结果:





package com.test.read;

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

/**
 * 利用jxl读取excel表
 * @author leifengchuan
 * @date 2017-06-27
 */
public class readExcel {
	
	/**
	 * 新建一个读取的方法
	 * @param file
	 * @return
	 */
	public static Sheet ReadExcel(File file) {
		Workbook book = null;
		try {
			book = Workbook.getWorkbook(file);
		} catch (BiffException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
		// 获得第一个工作表对象
		Sheet sheet = book.getSheet(0);
		// 得到第一列第一行的单元格
		int columnum = sheet.getColumns(); // 得到列数
		int rownum = sheet.getRows(); // 得到行数
		System.out.println("列的数目是: " + columnum);
		System.out.println("行数:" + rownum);
		for (int i = 0; i < rownum; i++) // 循环进行读写
		{
			for (int j = 0; j < columnum; j++) {
				Cell cell1 = sheet.getCell(j, i);
				String result = cell1.getContents();
				System.out.print(result);
				System.out.print(" \t");
			}
			System.out.println("  ");

		}

		book.close();

		return sheet;

	}
    /**
     * 测试执行
     * @param args
     * 备注:执行excel,不支持最新版本的格式
     * 只支持 xls
     */
	public static void main(String[] args) {
		
		readExcel.ReadExcel(new File(
				 "D:\\Users\\测试文件.xls"
				 ));
		

	}
	
	

}





上面是主要的代码,源代码以及读取的excel模板地址

下载地址,直接导入eclipse 就可以运行,读取的excel文件在doc目录下,jxl.jar在lib目录
http://download.csdn.net/detail/leifengchuan/9882884




猜你喜欢

转载自lfc-jack.iteye.com/blog/2381862