Java读Excel

import java.io.File;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;

public class Test
{
	public static void main(String[] args) throws Exception
	{
		File target = new File("C:/testFile/demo.xls");

		Workbook rwb = Workbook.getWorkbook(target);
		
		//获取工作表
		Sheet[] sheets = rwb.getSheets();
		for (Sheet sheet : sheets)
		{
			System.out.println(sheet.getName());
		}
		
		// 获取第1列第2行的单元格; 列,行都是从0开始
		Cell cell = sheets[0].getCell(0, 1);
		String cont = cell.getContents();//内容
		
		//获取指定类型的值
		if(cell.getType() == CellType.LABEL)
		{
			LabelCell  lc =(LabelCell) cell;
			lc.getString();
		}
		else if (cell.getType() == CellType.NUMBER)
		{
			NumberCell nc = (NumberCell)cell;
			nc.getValue();
		}
		else if (cell.getType() == CellType.DATE)
		{
			DateCell dc =(DateCell) cell;
			dc.getDate();
		}
	}
}
 

你可能感兴趣的:(Excel)