在做接口自动化的时候,通常会遇到数据取用及存放的问题,一般有三种方式可选择
1、数据库存取
2、表格存取
3、项目配置文件存取
这里仅展示下第二种方式表格取数据的
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; public class ReadExcel { public static void main(String[]args){ File file = new File("C:\\Users\\Sunny\\Desktop\\test.xlsx"); try { XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file)); int tabIndex =0; Sheet sheet = xssfWorkbook.getSheetAt(tabIndex); Row row = null; Cell cell_a0 = null; row = sheet.getRow(1); //指定行 cell_a0 = row.getCell(1); //制定列 cell_a0.setCellType(CellType.STRING); String cellValue0 = cell_a0.getStringCellValue(); System.out.println(cellValue0); } catch (Exception e) { e.printStackTrace(); } } }