1 下載操做excel的jar文件java
2 加載jar文件apache
3 編寫Excel文件數組
4 編寫解析Excel類ide
package com.selenium.tool; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ParseExcel { private Workbook book; private Sheet sheet; private List<String> oneRowDataList; private List<List<String>> dataesList; /** * 構造同時加載excel * @param excelPath * @param sheetName */ public ParseExcel(String excelPath,String sheetName){ this.load(excelPath,sheetName); } /** * 加載文件 */ public void load(String excelPath,String sheetName){ try { // File file = new File(excelPath); FileInputStream inputStream = new FileInputStream(excelPath); book = WorkbookFactory.create(inputStream); sheet = book.getSheet(sheetName); } catch (FileNotFoundException e) { System.out.println("Excel文件沒有找到!"); e.printStackTrace(); }catch(IOException e){ System.out.println("讀取Excel文件出錯!"); e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } } /** * 獲得指定單元格的值,並轉換成字符串 * 在讀取每一個單元格值的時候,經過getCellType方法得到當前單元格類型 * 在Excel中單元格有6種類型 * 1 CELL_TYPE_BLANK 空值 * 2 CELL_TYPE_BOOLEAN 布爾型 * 3 CELL_TYPE_ERROR 錯誤 * 4 CELL_TYPE_FORMULA 公式型 * 5 CELL_TYPE_STRING 字符串型 * 6 CELL_TYPE_NUMERIC 數值型 * 若是單元格類型爲CELL_TYPE_NUMERIC時,還須要進一步判斷 * 若是是Date類型,在Excel中Date類型是以Double類型數字存儲的,表示當前時間與1900年1月1日相隔的天數 * @param rowNum * @param colNum */ @SuppressWarnings("unused") public String getCellValue(Cell cell){ if(cell == null){ return null; } int dataType = cell.getCellType(); String cellValue = ""; //判斷數據類型 switch(dataType){ case Cell.CELL_TYPE_STRING: cellValue = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BOOLEAN: cellValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: cellValue = String.valueOf(cell.getCellFormula()); break; case Cell.CELL_TYPE_BLANK: cellValue = ""; break; case Cell.CELL_TYPE_ERROR: cellValue = ""; break; case Cell.CELL_TYPE_NUMERIC: if(DateUtil.isCellDateFormatted(cell)){ cellValue = new DataFormatter().formatCellValue(cell); }else { double dValue = cell.getNumericCellValue(); int iValue = (int)dValue; cellValue = dValue - iValue == 0 ? String.valueOf(iValue) : String.valueOf(dValue); } break; default: cellValue = cell.toString().trim(); break; } return cellValue.trim(); } /** * 獲取sheet頁中的數據 */ private void getSheetDataes() { oneRowDataList = new ArrayList<String>(); dataesList = new ArrayList<List<String>>(); int totalRows = sheet.getLastRowNum()+1; //總行數 for(int i=0 ; i<totalRows ; i++){ int totalCols = sheet.getRow(i).getLastCellNum(); //總列數 for(int j=1 ; j<totalCols ; j++){ Cell cell = sheet.getRow(i).getCell(j); if(i==0){ }else{ //將數據放到list中 oneRowDataList.add(this.getCellValue(cell)); } } } dataesList.add(oneRowDataList); } /** * 返回Object[][] */ public Object[][] getParameter(int colsNum){ this.getSheetDataes(); Object[][] param=new Object[dataesList.size()][colsNum]; //定義二維數組new Object[3][2] - 3行2列 for(int i=0; i< dataesList.size(); i++){ for (int j = 0; j < colsNum; j++) { param[i][j]=dataesList.get(i).get(j); } } return param; } }
5 解析Excel文件post
package com.selenium.util; import com.selenium.tool.ParseExcel; public interface Data { public static ParseExcel peLogin = new ParseExcel(".\\data\\data.xls","login"); public static Object[][] paramLogin = peLogin.getParameter(2); }
6 測試類測試
package com.selenium.test2; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import com.selenium.util.Data; public class Test9 implements Data{ @DataProvider public Object[][] loginData(){ Object[][] objects = ParamLogin; return objects; } @Test(dataProvider = "loginData") public void testLogin(String username,String password){ System.out.println("用戶名:" + username); System.out.println("密碼:" + password); } }