java利用poi读取excel中数据

所需的jar包:


代码:

/**
	 *
	 * @param cell
	 *            一个单元格的对象
	 * @return 返回该单元格相应的类型的值
	 */
	public static Object getRightTypeCell(Cell cell) {
		Object object = null;
		switch (cell.getCellType()) {
		case Cell.CELL_TYPE_STRING: {
			object = cell.getStringCellValue();
			break;
		}
		case Cell.CELL_TYPE_NUMERIC: {
			if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
				SimpleDateFormat sdf = null;
				if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
						.getBuiltinFormat("h:mm")) {
					sdf = new SimpleDateFormat("HH:mm");
				} else {// 日期
					sdf = new SimpleDateFormat("yyyy-MM-dd");
				}
				Date date = cell.getDateCellValue();
				object = sdf.format(date);
			} else {
				object = cell.getNumericCellValue();
			}
			break;
		}
		case Cell.CELL_TYPE_FORMULA: {
			cell.setCellType(Cell.CELL_TYPE_NUMERIC);
			object = cell.getNumericCellValue();
			break;
		}
		case Cell.CELL_TYPE_BLANK: {
			object = cell.getStringCellValue();
			break;
		}
		}
		return object;
	}

	/**
	 * 读取出filePath中的所有数据信息
	 * 
	 * @param filePath
	 *            excel文件的绝对路径
	 * 
	 */
	@SuppressWarnings("deprecation")
	public static void getDataFromExcel(String filePath) {
		// 判断是否为excel类型文件
		if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx")) {
			System.out.println("文件不是excel类型");
		}
		FileInputStream fis = null;
		Workbook wookbook = null;
		int flag = 0;
		try {
			// 获取一个绝对地址的流
			fis = new FileInputStream(filePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			// 2003版本的excel,用.xls结尾
			wookbook = new HSSFWorkbook(fis);// 得到工作簿
		} catch (Exception ex) {
			try {
				// 2007版本的excel,用.xlsx结尾
				wookbook = new XSSFWorkbook(filePath);// 得到工作簿
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 得到一个工作表
		Sheet sheet = wookbook.getSheetAt(0);
		// 获得表头
		Row rowHead = sheet.getRow(0);
		// 根据不同的data放置不同的表头
		Map<Object, Integer> headMap = new HashMap<Object, Integer>();
		try {
			// ----------------这里根据你的表格有多少列
			while (flag < rowHead.getPhysicalNumberOfCells()) {
				Cell cell = rowHead.getCell(flag);
				if (getRightTypeCell(cell).toString().equals("学号")) {
					headMap.put("id", flag);
				}
				if (getRightTypeCell(cell).toString().equals("姓名")) {
					headMap.put("name", flag);
				}
				if (getRightTypeCell(cell).toString().equals("生日")) {
					headMap.put("date", flag);
				}
				flag++;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("表头不合规范,请修改后重新导入");
		}
		// 获得数据的总行数
		int totalRowNum = sheet.getLastRowNum();

		if (0 == totalRowNum) {
			System.out.println("Excel内没有数据!");
		}
		Cell cell_1 = null, cell_2 = null, cell_3 = null;
		// 获得所有数据
		for (int i = 1; i <= totalRowNum; i++) {
			// 获得第i行对象
			Row row = sheet.getRow(i);
			try {
				cell_1 = row.getCell(headMap.get("id"));
				cell_2 = row.getCell(headMap.get("name"));
				cell_3 = row.getCell(headMap.get("date"));
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("获取单元格错误");
			}
			try {
				double temp = (Double) getRightTypeCell(cell_1);
				int id = (int) temp;
				String name = (String) getRightTypeCell(cell_2);
				String date = (String) getRightTypeCell(cell_3);
				System.out.println("id:" + id + " name:" + name + " date:"
						+ date);
			} catch (ClassCastException e) {
				e.printStackTrace();
				System.out.println("数据不全是数字或全部是文字!");
			}
		}
	}

	public static void main(String[] args) {
		getDataFromExcel("F:" + File.separator + "test.xlsx");
	}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值