poi读excel2007

以前做了很多东西都是jxl写excel文档的,但是,最近有需求要读入excel2007文件的内容,发现jxl对2007不支持,网上找了下,说jxl已经不更新了,但是apache的poi是可以支持的,所有就学了下,现贴出一段poi读取excel的代码,供大家参考,需要的poi的jar包文件,可以去我的空间下载poi,是最新的。代码是项目中的代码,大家可以复制后改动,如果有错误,请指正,谢谢!

/**
	 * poi的方式读取excel2007
	 * @param strpath
	 * @return
	 */
	public String loadExcel2007(String strpath) {
		DecimalFormat df = new DecimalFormat("#0.00%");// 格式化数据
		XSSFWorkbook workbook = null;
		File tempDir = new File(strpath);
		if (!tempDir.exists()) {
			System.out.println(" the file not exist :" + strpath);
		}
		try {
			workbook = new XSSFWorkbook(new FileInputStream(strpath));
			// workbook.getNumberOfSheets();//可以获取sheet的数量
			if (null != workbook.getSheetAt(0)) {
				XSSFSheet asheet = workbook.getSheetAt(0); // 此处获取第一个sheet
				int rowNum = asheet.getLastRowNum();// 获取总行数
				for (int i = 1; i <= rowNum; i++) {
					XSSFRow aRow = asheet.getRow(i);
					int cellNum = aRow.getLastCellNum();// 获取本行的列数
					for (int j = 0; j < cellNum; j++) {
						String strCell = "";
						XSSFCell cell = aRow.getCell(0);
						if (cell != null) {
							int cellType = cell.getCellType();
							switch (cellType) {
							case 0:// Numeric
								strCell = df.format(cell.getNumericCellValue());
								break;
							case 1:// String
								strCell = cell.getStringCellValue();
								break;
							default:
								strCell = "无法读取格式"; // System.out.println("格式不对不读");//其它格式的数据
							}
							System.out.println(strCell);
						} else {
							System.out.println("空cell!");
						}
					}
				}
			}
		} catch (Exception e) {
			System.out.println("ReadExcelError" + e);
		} finally {
			if (workbook != null) {
				workbook = null;
			}
		}
		return "";
	}

 

pio读取excel2003,这个和读2003用的不是一个类,此处贴出一段代码

/**
	 * poi读取excel2003
	 * @param strpath
	 * @return
	 */
	public String loadExcel2003(String strpath) {
		DecimalFormat df = new DecimalFormat("#0.00%");
		String correctTelephone = "";
		int correctnum = 0;// 统计通过验证的数目
		HSSFWorkbook workbook = null;
		try {
			workbook = new HSSFWorkbook(new FileInputStream(strpath));
			// workbook.getNumberOfSheets());//获取sheet数
			if (null != workbook.getSheetAt(0)) {
				HSSFSheet aSheet = workbook.getSheetAt(0);// 获得一个sheet
				// System.out.println("+++getFirstRowNum+++" +
				// aSheet.getFirstRowNum());//
				// System.out.println("+++getLastRowNum+++" +
				// aSheet.getLastRowNum());
				int rowNum = aSheet.getLastRowNum();// 获取总行数
				for (int i = 1; i <= rowNum; i++) {
					HSSFRow aRow = aSheet.getRow(i);
					int cellNum = aRow.getLastCellNum();// 获取本行的列数
					// aRow.getLastCellNum());
					for (int j = 0; j < cellNum; j++) {
						String strCell = "";
						HSSFCell cell = aRow.getCell(0);
						if (cell != null) {
							int cellType = cell.getCellType();
							switch (cellType) {
							case 0:// Numeric
								strCell = df.format(cell.getNumericCellValue());
								break;
							case 1:// String
								strCell = cell.getStringCellValue();
								break;
							default:
								strCell = "无法读取格式"; // 其它格式的数据
							}
							System.out.println(strCell);
						} else {
							System.out.println("cell内容为空!");
						}
					}
				}
			}
		} catch (Exception e) {
			System.out.println("ReadExcelError" + e);
		} finally {
			if (workbook != null) {
				workbook = null;
			}
		}
		return "";
	}

 

简略说明下,在读取字段的时候,需要简单的判断下,对于不同类型的应应该用不同的方式读取,如果常用数据库的理解应该比较简单,此例只用到数值型和字符串两种,所以处理的比较简

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值