I have an Excel sheet and I used Apache POI library to read that sheet, but I want the user to choose which row & (multiple selected cells) to show its data. for example:
Name | ID | Password | date
john | 1001 | 1234 | 2-9-1997
James | 1002 |4567 | 24-6-1980
Liza | 1003 | 7890 | 18 -11-1990
I want to read the first row but only name, id & data cells, not all of the row. How can I perform this?
Here’s my attempt:
package javaapplication;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class JavaApplication {
public static void main(String[] args) {
try {
// Specify the path of file
File path=new File("/Users/monasaad1/Desktop/emp.xlsx");
// load file
FileInputStream file=new FileInputStream(path);
// load workbook
XSSFWorkbook workbook=new XSSFWorkbook(file);
// Load sheet- Here we are loading first sheetonly
XSSFSheet sheet1= workbook.getSheetAt(0);
int rowCounter = sheet1.getLastRowNum();
// user input row & cell
Scanner input = new Scanner(System.in);
System.out.print("Please choose row");
int choosenRow = input.nextInt();
System.out.print("Please choose cell");
int choosenCell = input.nextInt();
for (int i=0; i<rowCounter+1; i++){
String data =sheet1.getRow(choosenRow).getCell(choosenCell).getStringCellValue();
System.out.println("Data from Row"+i+" is "+data);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}