Berikut adalah cara membaca cell pada excel menggunakan java
1. Buat File Excel : EmployeeData.xlsx
2. Buat Source : ReadCellExcel.java
//reading value of a particular cell
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadCellExample
{
public static void main(String[] args){ReadCellExample rc=new ReadCellExample(); //object of the class//reading the value of 2nd row and 2nd columnString vOutput=rc.ReadCellData(2, 2);}System.out.println(vOutput);
//method defined for reading a cell
public String ReadCellData(int vRow, int vColumn)
{
String value=null; //variable for storing the cell valueWorkbook wb=null; //initialize Workbook null
try{//reading data from a file in the form of bytesFileInputStream fis=new FileInputStream("C:\\demo\\EmployeeData.xlsx");//constructs an XSSFWorkbook object, by buffering the whole stream into the memorywb=new XSSFWorkbook(fis);}catch(FileNotFoundException e){e.printStackTrace();}catch(IOException e1){e1.printStackTrace();}Sheet sheet=wb.getSheetAt(0); //getting the XSSFSheet object at given indexRow row=sheet.getRow(vRow); //returns the logical rowCell cell=row.getCell(vColumn); //getting the cell representing the given columnvalue=cell.getStringCellValue(); //getting cell valuereturn value; //returns the cell value
}
}
Hasil :