1. 程式人生 > >Java實現讀取Excel指定列的指定行的資料

Java實現讀取Excel指定列的指定行的資料

package utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
 * by zfy
 * 2018/9/20
 */
public class ExcelUtil {

    /**
     *
     * @param filePath  需要讀取的檔案路徑
     * @param column  指定需要獲取的列數,例如第一列 1
     * @param startRow 指定從第幾行開始讀取資料
     * @param  endRow 指定結束行
     * @return 返回讀取列資料的set
     */
    public static HashSet<String> getColumnSet(String filePath, int column, int startRow , int endRow){
        Workbook wb = readExcel(filePath); //檔案
        Sheet sheet = wb.getSheetAt(0); //sheet
        int rownum = sheet.getPhysicalNumberOfRows(); //行數
        Row row = null;
        HashSet<String> result = new HashSet<>();
        String cellData = null;
        if(wb != null){
            for (int i=startRow-1; i<endRow; i++){
                System.out.println(i);
                row = sheet.getRow(i);
                if(row !=null){
                        cellData = (String) getCellFormatValue(row.getCell(column-1));
                        result.add(cellData.replaceAll(" ", ""));
                }else{
                    break;
                }
                System.out.println(cellData);
            }
        }
        return  result;
    }

    /**
     *
     * @param filePath 需要讀取的檔案路徑
     * @param column 指定需要獲取的列數,例如第一列 1
     * @param startRow 指定從第幾行開始讀取資料
     * @return  返回讀取列資料的set
     */
    public static HashSet<String> getColumnSet(String filePath, int column, int startRow){
        Workbook wb = readExcel(filePath); //檔案
        Sheet sheet = wb.getSheetAt(0); //sheet
        int rownum = sheet.getPhysicalNumberOfRows(); //行數
        System.out.println("sumrows " + rownum);

        return  getColumnSet(filePath, column, startRow , rownum-1);
    }



    //讀取excel
    public static Workbook readExcel(String filePath){
        Workbook wb = null;
        if(filePath==null){
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if(".xls".equals(extString)){
                return wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(extString)){
                return wb = new XSSFWorkbook(is);
            }else{
                return wb = null;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }

    public static Object getCellFormatValue(Cell cell){
        Object cellValue = null;
        if(cell!=null){
            //判斷cell型別
            switch(cell.getCellType()){
                case NUMERIC:{
                    cell.setCellType(CellType.STRING);  //將數值型cell設定為string型
                    cellValue = cell.getStringCellValue();
                    break;
                }
                case FORMULA:{
                    //判斷cell是否為日期格式
                    if(DateUtil.isCellDateFormatted(cell)){
                     //轉換為日期格式YYYY-mm-dd
                        cellValue = cell.getDateCellValue();
                    }else{
                            //數字
                        cellValue = String.valueOf(cell.getNumericCellValue());
                    }
                    break;
                }
                case STRING:{
                     cellValue = cell.getRichStringCellValue().getString();
                     break;
                }
                default:
                    cellValue = "";
            }
        }else{
            cellValue = "";
        }
        return cellValue;
    }

}

使用:

@org.testng.annotations.Test
public void testReadExcel(){
    HashSet<String> columnSet = ExcelUtil.getColumnSet("src/main/resources/pboc/Excelfile_V2.xlsx", 1, 90);  //讀取第一列的從第90行開始往後的資料 到set
    System.out.println(columnSet.size());
    System.out.println(columnSet.toString());

HashSet<String> excelParamsSet = ExcelUtil.getColumnSet(CommonConstant.excelJingDongXinYongPath, 8, 2, 26); //讀取第8列的第二行到第26行 資料 到set
}