首页 > 解决方案 > 从特定单元格获取数据后读取excel内容

问题描述

我有现有的代码可以从 Excel 工作表中获取特定单元格。是否可以读取单元格内的特定内容?

excel阅读器:

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 C11, ignore first row column A1. (int vRow, int vColumn)
def exceldata = CustomKeywords.'test.readexcel.ReadCellData'(11, 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

单元格 C11 中的 Excel 数据

我想获取 key testId 的值。

{
  "testId": "test123",
  "created": "2020-02-06T17:02:39.257Z",
}

标签: apache-poikatalon-studio

解决方案


您将需要解析响应:

def parsedResponse = new JsonSlurper().parseText(response.getResponseText())
println parsedResponse.get("testId")

(别忘了import groovy.json.JsonSlurper


推荐阅读