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<TranTaskDao> list=te.readExcel(f);
			int size=list.size();
			for(int i=0;i<size;i++) {//輸出每一行
				System.out.println(i+"\t"+list.get(i).toString());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<TranTaskDao> 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<TranTaskDao> aList = new ArrayList<TranTaskDao>();
        // 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章