[英]Apache POI excel ready read column from A to N
I have written below codes by using Apache POI classes to read data from excel sheet.我通过使用 Apache POI 类从 Excel 表中读取数据编写了以下代码。 currently it reads row name + column name.
目前它读取行名+列名。
I would like to enhance it to allow me input many column name in case I have many Data columns.我想增强它以允许我输入许多列名,以防我有很多数据列。
I need help to get an idea how to enhance it.我需要帮助来了解如何增强它。
currently, it only reads ("rowName", "columnName")
目前,它只读取
("rowName", "columnName")
the test data could grow horizontally at excel sheet from columnA.....columnN I wanted something like this ("rowName", "columnNameA" till "columnNameN....")
.测试数据可以在来自 columnA.....columnN 的 excel 表中水平增长我想要这样的东西
("rowName", "columnNameA" till "columnNameN....")
。 is this possible?这可能吗? or any other better suggestion??
或者其他更好的建议??
public class readexcel { //method defined for reading a cell @Keyword private static findData(String rowName, String columnName) throws IOException { String cellValue = ReadCellData(rowName, columnName); System.out.println(cellValue); } private static String ReadCellData(String rowName, String columnName) throws IOException { FileInputStream fis=new FileInputStream(RunConfiguration.getProjectDir() + "/Data Files/testmatrix.xlsx"); Workbook workbook=new XSSFWorkbook(fis); Sheet sheet = workbook.getSheetAt(0); Row firstRow = sheet.getRow(0); int rowNameColIdx = findColumnIdx("Automation TC", firstRow); int colNameColIdx = findColumnIdx(columnName, firstRow); Iterator<Row> rowIterator = sheet.iterator(); rowIterator.next(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); if (rowName.equals(row.getCell(rowNameColIdx).getStringCellValue())) { return row.getCell(colNameColIdx).getStringCellValue(); } } return null; } private static int findColumnIdx(String text, Row row) { for (Cell cell : row) { if (text.equals(cell.getStringCellValue())) { return cell.getColumnIndex(); } } return -1; } }
Read data script读取数据脚本
def exceldata = CustomKeywords.'test.readexcel.ReadCellData'("rowName", "columnName")
Assuming your spreadshseet looks something like假设您的电子表格看起来像
| Test1 | Test 2 | Test C |
| A | 1 | aa |
| B | 2 | bb |
And you want to be able to say give me the values for Test2 and Test C in row 2 , then I'd suggest you do it in two steps.并且您希望能够告诉我第 2 行中 Test2 和 Test C 的值,然后我建议您分两步进行。 Firstly, build a mapping from Column Names to column indexes.
首先,构建从列名到列索引的映射。 Secondly, read specific cells from a row.
其次,从一行中读取特定的单元格。
Since you've tagged groovy I'd suggest code something like:由于您已标记groovy,因此我建议使用以下代码:
int headerRowNumber = 0 // 0-based row numbering
Sheet sheet = wb.getSheetAt(0) // what sheet to read from
Map<String,Integer> colNamesToNumbers = [:]
DataFormatter fmt = new DataFormatter()
sheet.getRow(headerRowNumber).each { cell ->
// Render the column heading to a string, in case it's a number
String heading = fmt.formatCellValue(cell)
colNamesToNumbers[heading] = cell.getColumnIndex()
}
Then to do the reading, something like然后做阅读,像
Closure readValues = { int rowNum, List<String> columnNames ->
Row row = sheet.getRow(rowNum)
if (row == null) return []
return columnNames.collect { String name ->
int colNum = colNamesToNumbers[name]
return fmt.formatCellValue( row.getCell(colNum) )
}
}
List<String> valsRow2 = readValues(2, ["Test1","Test C"])
// returns ["B","bb"]
List<String> valsRow7 = readValues(7, ["Test 2"])
// returns [] as no row 7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.