U ovom odjeljku ćemo naučiti kako možemo čitati podatke iz excel datoteke.
U Javi, čitanje excel datoteke nije slično čitanju word datoteke zbog ćelija u excel datoteci. JDK ne pruža izravan API za čitanje ili pisanje Microsoft Excel ili Word dokumenta. Moramo se osloniti na biblioteku treće strane koja je Apache POI.
Što je Apache POI?
Apache POI (Poor Fuscation Implementation) je Java API za čitanje i pisanje Microsoftovih dokumenata u oba formata .xls i .xlsx . Sadrži klase i sučelja. Apache POI biblioteka nudi dvije implementacije za čitanje excel datoteka:
Sučelja i klase u Apache POI
sučelja
Nastava
XLS klase
XLSX klase
popis java nizova
Koraci za čitanje podataka iz XLS datoteke
Korak 1: Napravite jednostavan Java projekt u eclipseu.
Korak 2: Sada stvorite mapu lib u projektu.
Korak 3: Preuzmite i dodajte sljedeće jar datoteke u mapu lib:
- commons-collections4-4.1.jar Kliknite ovdje
- poi-3.17.jar Kliknite ovdje
- poi-ooxml-3.17.jar Kliknite ovdje
- poi-ooxml-schemas-3.17.jar Kliknite ovdje
- xmlbeans-2.6.0.jar Kliknite ovdje
Korak 4: Postavite stazu klase:
Desnom tipkom miša kliknite na projekt -> Putanja izgradnje -> Dodaj vanjske JAR-ove -> odaberite sve gornje jar datoteke -> Primijeni i zatvorite.
Korak 5: Sada stvorite datoteku klase s imenom ReadExcelFileDemo i napišite sljedeći kod u datoteku.
string.compareto c#
Korak 6: Napravite excel datoteku s nazivom 'student.xls' i u nju upišite neke podatke.
Korak 7: Spremite i pokrenite program.
Primjer čitanja datoteke excel (.xls).
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; public class ReadExcelFileDemo { public static void main(String args[]) throws IOException { //obtaining input bytes from a file FileInputStream fis=new FileInputStream(new File('C:\demo\student.xls')); //creating workbook instance that refers to .xls file HSSFWorkbook wb=new HSSFWorkbook(fis); //creating a Sheet object to retrieve the object HSSFSheet sheet=wb.getSheetAt(0); //evaluating cell type FormulaEvaluator formulaEvaluator=wb.getCreationHelper().createFormulaEvaluator(); for(Row row: sheet) //iteration over row using for each loop { for(Cell cell: row) //iteration over cell using for each loop { switch(formulaEvaluator.evaluateInCell(cell).getCellType()) { case Cell.CELL_TYPE_NUMERIC: //field that represents numeric cell type //getting the value of the cell as a number System.out.print(cell.getNumericCellValue()+ ' '); break; case Cell.CELL_TYPE_STRING: //field that represents string cell type //getting the value of the cell as a string System.out.print(cell.getStringCellValue()+ ' '); break; } } System.out.println(); } } }
Izlaz:
Name Age Height Swarit 23.0 5' Puneet 25.0 6'1' Swastik 22.0 5'5' Tejas 12.0 4'9'
Čitanje XLSX datoteke
Svi će koraci ostati isti osim formata datoteke.
Stol: zaposlenik.xslx
java graditelj nizova
Primjer čitanja excel datoteke (.xlsx)
U ovom primjeru koristimo klasu XSSFWorkbook.
trostruka zima
import java.io.File; import java.io.FileInputStream; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class XLSXReaderExample { public static void main(String[] args) { try { File file = new File('C:\demo\employee.xlsx'); //creating a new file instance FileInputStream fis = new FileInputStream(file); //obtaining bytes from the file //creating Workbook instance that refers to .xlsx file XSSFWorkbook wb = new XSSFWorkbook(fis); XSSFSheet sheet = wb.getSheetAt(0); //creating a Sheet object to retrieve object Iterator itr = sheet.iterator(); //iterating over excel file while (itr.hasNext()) { Row row = itr.next(); Iterator cellIterator = row.cellIterator(); //iterating over each column while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: //field that represents string cell type System.out.print(cell.getStringCellValue() + ' '); break; case Cell.CELL_TYPE_NUMERIC: //field that represents number cell type System.out.print(cell.getNumericCellValue() + ' '); break; default: } } System.out.println(''); } } catch(Exception e) { e.printStackTrace(); } } }
Izlaz:
Employee ID Employee Name Salary Designation Department 1223.0 Harsh 20000.0 Marketing Manager Marketing 3213.0 Vivek 15000.0 Financial Advisor Finance 6542.0 Krishna 21000.0 HR Manager HR 9213.0 Sarika 34000.0 Sales Manager Sales
Čitanje određene vrijednosti ćelije iz excel datoteke (.xlsx)
Stol: EmployeeData.xlsx
Primjer
U sljedećem primjeru čitamo vrijednost 2ndred i 2ndstupac. Brojanje redaka i stupaca počinje od 0. Dakle, program vraća 'Softverski inženjer'.
//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 column String 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 value Workbook wb=null; //initialize Workbook null try { //reading data from a file in the form of bytes FileInputStream fis=new FileInputStream('C:\demo\EmployeeData.xlsx'); //constructs an XSSFWorkbook object, by buffering the whole stream into the memory wb=new XSSFWorkbook(fis); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e1) { e1.printStackTrace(); } Sheet sheet=wb.getSheetAt(0); //getting the XSSFSheet object at given index Row row=sheet.getRow(vRow); //returns the logical row Cell cell=row.getCell(vColumn); //getting the cell representing the given column value=cell.getStringCellValue(); //getting cell value return value; //returns the cell value } }
Izlaz:
Software Engineer