<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
</dependencies>
ហើយឧបមាថា ទាំងនេះជាទិន្នន័យដែមលមាននៅក្នុងexcel file របស់អ្នក (វាជាទិន្នន័យរបស់សិស្សនៃស្ថាប័នសិក្សាមួយកន្លែង)។ Excel file យើងដក់ទីតាំងវានៅក្នុង project តែប្រសិនបើជាការងារជាក់ស្តែង អ្នកអាចឲ្យគេ upload ឬ browse នៅក្នុង computer របស់គេក៏បាន។
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class App {
public static void main(String[] args) throws Exception {
read("students.xlsx");
}
private static void read(String filePath) throws Exception{
if(filePath==null || filePath==""){
throw new Exception("Please set file path...");
}
FileInputStream fileInputStream = new FileInputStream(new File(filePath));
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> iterator = sheet.iterator();
while(iterator.hasNext()){
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while(cellIterator.hasNext()){
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
break;
default:
break;
}
if(cell.getColumnIndex()<5 cell="" code="" column="" fileinputstream.close="" n-----------------------------------------------------------="" row="" system.out.print="" system.out.println="" workbook.close="">5>