java poi插件处理excel中的数据(只读取)

首先了解以下excel文件怎么和poi中的组件对应起来的。

  • 一个Excel文件对应于一个Workbook对象
  • 一个Workbook可以有多个Sheet对象
  • 一个Sheet对象由多个Row对象组成
  • 一个Row对象是由多个Cell对象组成

基于以上几条,如果想对excel文件进行读写的话就要

  1. 用Workbook打开或者创建一个Excel文件的对象
  2. 用上一步的Excel对象创建或者获取到一个Sheet对象
  3. 用Sheet对象创建或获取一个Row对象
  4. 用Row对象创建或获取一个Cell对象
  5. 对Cell对象读写。

现在需要取出上面的测试用例,并将其转为一个二维的数组

编写代码之前,先创建一个maven项目,然后再pom.xml里面加入依赖,如下:

<!-- poi-ooxml 专业读取excel的框架技术 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

(这个代码是取连续的值)代码如下:

package com.lyl.web.util;
import java.io.File;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import com.lyl.web.pojo.ExcelParam;

public class ExcelUtil {
	public static Object[][] reas(ExcelParam excelparam){
		String filePath=excelparam.getFilePath();
		int endRow=excelparam.getEndRow();
		int endCell =excelparam.getEndCell();
		int startCell=excelparam.getStartCell();
		int startRow=excelparam.getStartRow();
		Object datas[][]=new Object[endRow-startRow+1][endCell-startCell+1];
		try {
			Workbook workbook =WorkbookFactory.create(new File(filePath));
		  Sheet sheet=	workbook.getSheetAt(0);
		  for(int i=startRow;i<=endRow;i++){
			  Row row=sheet.getRow(i-1);
			  for(int j=startCell;j<=endCell;j++){
				  Cell cell=row.getCell(j-1,MissingCellPolicy.CREATE_NULL_AS_BLANK);
				  cell.setCellType(CellType.STRING);
				  String cellValue=cell.getStringCellValue();
				  datas[i-startRow][j-startCell]=cellValue;
			  }
		  }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		 return datas;
	}
	public static void main(String[] args) {
		ExcelParam ec=new ExcelParam("src/test/resources/register.xlsx",2,7,1,4);
	    Object datas[][]=ExcelUtil.reas(ec);
		for(Object[] rowData:datas){
			for(Object cellData:rowData){
				System.out.println(cellData);
			}
			System.out.println("\n");
		}
			
	}
}
package com.lyl.web.pojo;

/**
 * @author Administrator
 *	
 */
public class ExcelParam {
	private String filePath;
	private int startRow;
	private int startCell;
	private int endRow;
	private int endCell;
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public int getStartRow() {
		return startRow;
	}
	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}
	public int getStartCell() {
		return startCell;
	}
	public void setStartCell(int startCell) {
		this.startCell = startCell;
	}
	public int getEndRow() {
		return endRow;
	}
	public void setEndRow(int endRow) {
		this.endRow = endRow;
	}
	public int getEndCell() {
		return endCell;
	}
	public void setEndCell(int endCell) {
		this.endCell = endCell;
	}
	public ExcelParam(){
		super();
	}
	public ExcelParam(String filePath,int startRow, int endRow,int startCell,int endCell){
		this.endCell=endCell;
		this.endRow=endRow;
		this.startCell=startCell;
		this.startRow=startRow;
		this.filePath=filePath;
	}

}

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值