Java中使用Apache POI使用解析excel文件

背景:

         最近要做个功能,将excel上传后,然后将其中间的某些列的数据插入到数据库中,然后我需要解析excel文件,将中间的某些数据提取出来。

java中解析excel文件可以使用 jxl,也可以使用poi,他们具体的差别就不说了。

POI是属于Apache的,它相应的文档:

官方主页: http://poi.apache.org/index.html
API文档: http://poi.apache.org/apidocs/index.html

第一步:加载poi.jar包

        我这里使用的是 poi-3.7.jar 这个版本。

        pom文件:

<dependency>  
    <groupId>org.apache.poi</groupId>  
    <artifactId>poi</artifactId>  
    <version>3.7</version>  
</dependency>

第二步:了解下它常见的类:

     HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
    XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
    HWPF - 提供读写Microsoft Word DOC97格式档案的功能。
    XWPF - 提供读写Microsoft Word DOC2003格式档案的功能。
    HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
    HDGF - 提供读Microsoft Visio格式档案的功能。
    HPBF - 提供读Microsoft Publisher格式档案的功能。
    HSMF - 提供读Microsoft Outlook格式档案的功能。

我这里使用的就是HSSF(org.apache.poi.hssf.usermodel.*):
它的API文档截图如下:

主要是这三个类,HSSFWorkbook,HSSFSheet,HSSFRow。

第三步,解析示例:

package com.sfpay.excel.exceldispose;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class OperationExcel {
	
	/**
	 * 读取excel文件
	 * @param file
	 * @throws FileNotFoundException
	 */
	public void readExcelTest(File file) throws FileNotFoundException, IOException {
		FileInputStream fis =  new FileInputStream(file);
		BufferedInputStream bfs = new BufferedInputStream(fis);
		POIFSFileSystem pfsfs = new POIFSFileSystem(bfs);
		//创建一个workbook实体,然后将其赋值
		HSSFWorkbook workbook = new HSSFWorkbook(pfsfs);
		int numberOfSheets = workbook.getNumberOfSheets();
		System.out.println(numberOfSheets);
		for (int sheetOrder = 0; sheetOrder < numberOfSheets; sheetOrder++) {
			//Get the HSSFSheet object at the given index.
			HSSFSheet sheet = workbook.getSheetAt(sheetOrder);
			//Gets the number last row on the sheet.
			int lastRowIndex = sheet.getLastRowNum();
			System.out.println(lastRowIndex+"------------");
			for (int i = 0; i < lastRowIndex; i++) {
				HSSFRow row = sheet.getRow(i);
				if (row == null) {
					break;
				}
				short lastCellNum = row.getLastCellNum();
		        for (int j = 0; j < lastCellNum; j++) {
		        	if(row.getCell(j).getCellType() == 1) {//这里获取的数据类型为String行,
		        		String cellValue = row.getCell(j).getStringCellValue();
		        		System.out.println(cellValue);
		        	}else if(row.getCell(j).getCellType() == 0) {//这里表示excel中的数据为number
		        		long numericCellValue = (long)row.getCell(j).getNumericCellValue();
		        		System.out.println(numericCellValue);
		        	}else if(row.getCell(j).getCellType() == 3) {//这里表示excel中的数据为空
		        		System.out.println(row.getCell(j));
		        	}
		        }
			}
		}

		bfs.close();
		fis.close();
	}
	public static void main(String[] args) throws FileNotFoundException, IOException {
		File file = new File("F:dsfadsa.xls");
		OperationExcel oe = new OperationExcel();
		oe.readExcelTest(file);
	}
}

第四步:测试效果

输出结果:


第五步:注意事项

       这里解析excel文件时,出现个情况,就是表格里面的数据如果是纯数字,它就会被解析成double型,所以在上面示例代码中,如果使用row.getCell(j).getStringCellValue()将其强转为String时,会报错。所以我将其转化为了long型,但是不能转化成int型,因为会出现溢出的情况,不过不是纯数字,就会就可以转化为String。

       但是还有其他情况,比如我测试的时候,某一个表格为空的时候,这个时候就不能转化,因为转化了后直接获取不了这个表格的内容,但是我们要知道这个是为空的,所以需要先判断这个表格中这个值的类型,如果 row.getCell(j).getCellType() == 3 时,那就表明这个值为空,这个时候再根据其业务中表达的实际意义去处理这个相应的值。

        当然,还有其它类型的,比如date类型,我发现api中也有返回这个类型的方法,不过我没有去试,如果实际工作中出现了这种数据,也需要好好测试下,然后再解析处理。


关于这个部分方法解析,可以参考这篇文章:

http://blog.csdn.net/vbirdbest/article/details/72870714

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值