现在的位置: 首页 > 综合 > 正文

JAVA读取Excel中的数据

2013年12月11日 ⁄ 综合 ⁄ 共 929字 ⁄ 字号 评论关闭

先去下载个JXL的JAR包。以下是一个小例子。

package test.hello;

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

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

public class JXLTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			//得到工作薄
			Workbook workbook = Workbook.getWorkbook(new File("D:/mydata.xls"));
			//得到工作区
			Sheet sheet = workbook.getSheet(0);
			//得到单元格
			Cell cell00 = sheet.getCell(0,0);
			Cell cell01 = sheet.getCell(0,1);
			Cell cell02 = sheet.getCell(0,2);
			
			//得到列数
			int columCount = sheet.getColumns();
			//得到行数
			int rowCount = sheet.getRows();
			
			System.out.println("行数"+columCount);
			System.out.println("列数"+rowCount);
			
			for(int i=0;i<rowCount;i++){
				System.out.print("第"+(i+1)+"行的内容是:");
				for(int j=0;j<columCount;j++){
					Cell cell = sheet.getCell(j,i);				
					System.out.print(cell.getContents()+"\t");
				}
				System.out.println("");
			}	
			
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

抱歉!评论已关闭.