java读取excel所需jar包及代码实现

需求:将如图EXCEL的内容按行读取,封装成对象。

实现类:进行Excel读取

package bao.test;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.format.CellFormat;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;
public class TestExcel {
	public static void main(String[] args) {
		TestExcel te=new TestExcel();
		File f= new File("G:\\tran.xlsx");
		try {
			List list=te.readExcel(f);
			int size=list.size();
			for(int i=0;i readExcel(File file) throws Exception {
        //获取文件名字
        String fileName = file.getName();
        //获取文件类型
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
        System.out.println(" **** fileType:" + fileType);
        //获取输入流
        InputStream stream = new FileInputStream(file);
        //获取工作薄
        Workbook xssfWorkbook = null;
        if (fileType.equals("xls")) {
            xssfWorkbook = new HSSFWorkbook(stream);
        } else if (fileType.equals("xlsx")) {
            xssfWorkbook = new XSSFWorkbook(stream);
        } else {
            System.out.println("您输入的excel格式不正确");
        }
        TranTaskDao a = null;
        List aList = new ArrayList();
        // Read the Sheet
        Sheet Sheet = xssfWorkbook.getSheetAt(0);
        // Read the Row 从0开始
        for (int rowNum = 0; rowNum <= Sheet.getLastRowNum(); rowNum++) {
            Row Row = Sheet.getRow(rowNum);
            if (Row != null) {
                //判断这行记录是否存在
                if (Row.getLastCellNum() < 1 || "".equals(getValue(Row.getCell(1)))) {
                    continue;
                }
                //获取每一行封装成对象
                a = new TranTaskDao();
                a.setTranid(getValue(Row.getCell(1)));
               // System.out.println(getValue(Row.getCell(1)));
                a.setTask_id(getValue(Row.getCell(2)));
                a.setTask_name(getValue(Row.getCell(3)));
                a.setToTask_id(getValue(Row.getCell(4)));
                a.setToTask_name(getValue(Row.getCell(5)));
                //System.out.println(getValue(Row.getCell(5)));
                a.setTrans_expression(getValue(Row.getCell(6)));
                a.setComments(getValue(Row.getCell(7)));
                aList.add(a);
            }
        }
        return aList;
    }
    private String getValue(Cell cell){
    	if (cell==null) {//单元格为空
    		  return "空空如也!";
    	}else {
        int type = CellFormat.ultimateType(cell);
        if(type == Cell.CELL_TYPE_BOOLEAN)
        {
            return String.valueOf(cell.getBooleanCellValue());
        }
        else if(type == Cell.CELL_TYPE_NUMERIC)
        {
            return String.valueOf(cell.getNumericCellValue());
        }
        else if(type == Cell.CELL_TYPE_BLANK)
        {
            return "";
        }
        	else
        	{
            return cell.getStringCellValue().trim();
        	}
        
    	}
    }
}

​

包装类:将行封装成的对象

package bao.test;
public class TranTaskDao {
	public String tranid;
	public String task_id;
	public String task_name;
	public String toTask_id;
	public String toTask_name;
	public String trans_expression;//转移条件
	public String comments;//备注
	public String getTranid() {
		return tranid;
	}
	public void setTranid(String tranid) {
		this.tranid = tranid;
	}
	public String getTask_id() {
		return task_id;
	}
	public void setTask_id(String task_id) {
		this.task_id = task_id;
	}
	public String getTask_name() {
		return task_name;
	}
	public void setTask_name(String task_name) {
		this.task_name = task_name;
	}
	public String getToTask_id() {
		return toTask_id;
	}
	public void setToTask_id(String toTask_id) {
		this.toTask_id = toTask_id;
	}
	public String getToTask_name() {
		return toTask_name;
	}
	public void setToTask_name(String toTask_name) {
		this.toTask_name = toTask_name;
	}
	public String getTrans_expression() {
		return trans_expression;
	}
	public void setTrans_expression(String trans_expression) {
		this.trans_expression = trans_expression;
	}
	public String getComments() {
		return comments;
	}
	public void setComments(String comments) {
		this.comments = comments;
	}
	public String toString() {
		String str=this.tranid+"\t"+this.getTask_id()+'\t'+this.task_name+'\t'+
	    this.getToTask_id()+"\t"+this.getToTask_name()+'\t'+this.getTrans_expression()+'\t'+this.getComments();
		return str;
	}
}

注意:java读取excel所需jar包下载

https://pan.baidu.com/s/1-m21c-YHkWBW6FVxOxEDiQ
提取码:k18t

你可能感兴趣的:(Java篇,工具篇)