Testng中数据驱动常用的注解是 @DataProvider,但是这个方法必须返回一个Object[][]。
    简单思路就是,读取excel数据,excel数据第一行作为map的Key其它行为值,放入map并返回。 @DataProvider 注解对应方法去获取调读取excel方法,拿到返回的Object[][],其余都一样使用。
    1、导入maven依赖

    1. <dependency>
    2. <groupId>org.apache.poi</groupId>
    3. <artifactId>poi-ooxml</artifactId>
    4. <version>3.15</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.apache.poi</groupId>
    8. <artifactId>poi</artifactId>
    9. <version>3.15</version>
    10. </dependency>

    2、读取Excel数据,并放入Map中返回

    1. public class DateCenter {
    2. /**
    3. * @param file 读取某个excel文件
    4. * @return Object
    5. */
    6. public Object[][] testData(String file) {
    7. ArrayList<String> arrkey = new ArrayList<String>();
    8. Workbook workbook = getWorkbook(file);
    9. Sheet sheet = workbook.getSheetAt(0);
    10. // 获取总行数
    11. int rowTotalNum = sheet.getLastRowNum() + 1;
    12. // 总列数
    13. int columns = sheet.getRow(0).getPhysicalNumberOfCells();
    14. HashMap<String, String>[][] map = new HashMap[rowTotalNum - 1][1];
    15. // 对数组中所有元素hashmap进行初始化
    16. if (rowTotalNum > 1) {
    17. for (int i = 0; i < rowTotalNum - 1; i++) {
    18. map[i][0] = new HashMap();
    19. }
    20. } else {
    21. System.out.println("测试的Excel" + file + "中没有数据");
    22. }
    23. // 获得首行的列名,作为hashmap的key值
    24. for (int c = 0; c < columns; c++) {
    25. String cellvalue = CellUnit.getCellValue(sheet, 0, c);
    26. arrkey.add(cellvalue);
    27. }
    28. // 遍历所有的单元格的值添加到hashmap中
    29. for (int r = 1; r < rowTotalNum; r++) {
    30. for (int c = 0; c < columns; c++) {
    31. String cellvalue = CellUnit.getCellValue(sheet, r, c);
    32. map[r - 1][0].put(arrkey.get(c), cellvalue);
    33. }
    34. }
    35. return map;
    36. }
    37. /**
    38. * 创建 workbook
    39. *
    40. * @param filePath excel文件路径
    41. * @return Workbook 对象
    42. * @throws IOException
    43. */
    44. public static Workbook getWorkbook(String filePath) {
    45. Workbook wb = null;
    46. try {
    47. if (filePath.endsWith(".xls")) {
    48. File file = new File(filePath);
    49. InputStream is = new FileInputStream(file);
    50. wb = new HSSFWorkbook(is);
    51. } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".xlsm")) {
    52. wb = new XSSFWorkbook(filePath);
    53. }
    54. } catch (IOException e) {
    55. e.printStackTrace();
    56. }
    57. return wb;
    58. }
    59. /**
    60. * 单元格处理内部类
    61. */
    62. private static class CellUnit {
    63. /**
    64. * 通过sheet 行号和列返回值
    65. *
    66. * @param sheet sheet name
    67. * @param rowNum 行号
    68. * @param cellNum 列号
    69. * @return
    70. */
    71. public static String getCellValue(Sheet sheet, int rowNum, int cellNum) {
    72. String value;
    73. Cell cell = sheet.getRow(rowNum).getCell(cellNum);
    74. if (cell != null) {
    75. // 有值时,转换成字符串
    76. value = CellUnit.getCellValue2(cell);
    77. } else {
    78. // 当单元格没有值时,设为空串
    79. value = "";
    80. }
    81. return value;
    82. }
    83. /**
    84. * 把不同类型的单元格转换成字符串,并返回
    85. *
    86. * @param cell cell
    87. * @return 单个单元格值
    88. */
    89. public static String getCellValue2(Cell cell) {
    90. String value = "";
    91. switch (cell.getCellTypeEnum()) {
    92. case STRING:
    93. value = String.valueOf(cell.getRichStringCellValue());
    94. return value;
    95. case NUMERIC:
    96. // 处理数值类型自动转成科学计数法问题
    97. DecimalFormat df = new DecimalFormat("0");
    98. value = String.valueOf(df.format(cell.getNumericCellValue()));
    99. return value;
    100. case BOOLEAN:
    101. value = String.valueOf(cell.getBooleanCellValue());
    102. return value;
    103. case FORMULA:
    104. value = String.valueOf(cell.getCellFormula());
    105. return value;
    106. case ERROR:
    107. value = String.valueOf(cell.getErrorCellValue());
    108. return value;
    109. case BLANK:
    110. return value;
    111. default:
    112. System.out.println("单元格异常");
    113. return value;
    114. }
    115. }
    116. }
    117. }

    3、TestNG使用数据

    1. @DataProvider(name = "testData")
    2. public Object[][] data() {
    3. TestCaseExcel testcase = new TestCaseExcel();
    4. return testcase.testData(file);
    5. }
    6. @Test(dataProvider = "testData")
    7. public void testCase(HashMap<String, String> data) {
    8. String fileName = data.get("excelName");
    9. String bpSheetName = data.get("Benefits Package Sheet");
    10. int bpRowNum = Integer.parseInt(data.get("BP sheet RowNum"));
    11. String csvSheetName = data.get("Cost Share Variances Sheet");
    12. int csvRowNum = Integer.parseInt(data.get("CSV Sheet RowNum"));
    13. String hiosPlanID = data.get("HIOS Plan ID");
    14. String isPass = data.get("isPass");