apache-poi - 从特定单元格获取数据后读取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",
}
解决方案
您将需要解析响应:
def parsedResponse = new JsonSlurper().parseText(response.getResponseText())
println parsedResponse.get("testId")
(别忘了import groovy.json.JsonSlurper
)
推荐阅读
- javascript - Flowjs:React.Children.map 中缺少地图属性
- qt - 单一设置 exe 文件 Qt 应用程序部署
- shell - Shell 脚本:检查字符串是否不区分大小写
- sql-server - 如何将sql server中的逗号分隔字符串拆分为n列
- c# - ASP.net core 2.0 外部登录错误(异常:OAuth 令牌端点失败:状态:ProxyAuthenticationRequired;标头:缓存控制:无缓存)
- go - 从 Redshift 表中选择时使用 Postgres 数组的应用程序
- android - 我可以将可见性绑定中的多个属性与 MvvmCross 结合起来吗?
- php - 查询以匹配日期获取详细信息
- html - 在悬停时使用过渡到绝对位置
- android - 当我建造时,我总是会出现