Complete the code to create a Workbook object from an Excel file input stream.
FileInputStream file = new FileInputStream("data.xlsx"); Workbook workbook = new [1](file);
The XSSFWorkbook class is used to create a Workbook object for .xlsx files from a FileInputStream.
Complete the code to get the first sheet from the workbook.
Sheet sheet = workbook.[1](0);
The getSheetAt(int index) method returns the sheet at the given index from the workbook.
Fix the error in the code to read the string value from the first cell of the first row.
Row row = sheet.getRow(0); Cell cell = row.getCell(0); String value = cell.[1]();
To read a string from a cell, use getStringCellValue(). Other methods are for different data types.
Fill both blanks to iterate over all rows and print the first cell's string value.
for (int [1] = 0; [2] <= sheet.getLastRowNum(); [1]++) { Row row = sheet.getRow([1]); System.out.println(row.getCell(0).getStringCellValue()); }
The variable i is commonly used as the loop counter. Both blanks must use the same variable to iterate correctly.
Fill all three blanks to create a map of row numbers to the string value of the first cell, filtering rows where the cell is not empty.
Map<Integer, String> dataMap = new HashMap<>(); for (int [1] = 0; [1] <= sheet.getLastRowNum(); [1]++) { Row row = sheet.getRow([1]); if (row != null && row.getCell(0) != null && !row.getCell(0).getStringCellValue().[2]()) { dataMap.put([3], row.getCell(0).getStringCellValue()); } }
The loop variable i is used for iteration and as the key in the map. The isEmpty() method checks if the cell string is empty to filter out empty cells.