Как прочитать указанный столбец c один за другим до определенного номера столбца? - PullRequest
0 голосов
/ 17 февраля 2020

У меня есть существующие коды, которые работают, чтобы получить конкретную ячейку из листа Excel.

У меня есть входные данные в листе Excel от ячейки C2 до ячейки T2.

Текущие коды позволили мне прочитать ячейку C2 один раз.

Я хотел, чтобы он автоматически читал C2, D2, E2, ..... до T2

Коды:

public String ReadCellData(int vRow, int vColumn)
	{
		String value=null;          //variable for storing the cell value
		Workbook wb=null;           //initialize Workbook null
		try
		{
			//reading data from a file in the form of bytes
      FileInputStream fis=new FileInputStream(RunConfiguration.getProjectDir() + "/Data Files/testmatrix.xlsx");
			//constructs an XSSFWorkbook object, by buffering the whole stream into the memory
			wb=new XSSFWorkbook(fis);
		}
		catch(FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch(IOException e1)
		{
			e1.printStackTrace();
		}
		Sheet sheet=wb.getSheetAt(0);   //getting the XSSFSheet object at given index
		Row row=sheet.getRow(vRow); //returns the logical row
		Cell cell=row.getCell(vColumn); //getting the cell representing the given column
		//value=cell.getStringCellValue();    //getting cell value
		//return value;               //returns the cell value
	}
}

коды для получения и печати данных

//read excel cell C2, ignore first row column A1. (int vRow, int vColumn)
def exceldata = CustomKeywords.'test.readexcel.ReadCellData'(2, 2)

String jsondata = JsonOutput.prettyPrint(exceldata.toString())

println(jsondata)

WebUI.delay(1)

//call the object and post the above data as API HTTP request body
def post = ((findTestObject('Object Repository/Web Service Request/test-service/Post')) as RequestObject)

post.setBodyContent(new HttpTextBodyContent(jsondata))

WebUI.delay(2)

//POST and verification
def response = WS.sendRequestAndVerify(post)

println(response.statusCode)

assert response.getStatusCode() == 201

1 Ответ

1 голос
/ 18 февраля 2020

Используйте For l oop, итерируйте l oop и получите значение

Пример

def exceldata="";
for(int row =1;row<=2;row++)
{
for(int col=1;col<=5;col++)
{
exceldata = CustomKeywords.'test.readexcel.ReadCellData'(row,col) 
}
}
...