简体   繁体   中英

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. 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")

the test data could grow horizontally at excel sheet from columnA.....columnN I wanted something like this ("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. Firstly, build a mapping from Column Names to column indexes. Secondly, read specific cells from a row.

Since you've tagged I'd suggest code something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM