V tem razdelku se bomo naučili, kako lahko beremo podatke iz datoteke excel.
logično v niz
V Javi branje datoteke excel ni podobno branju datoteke word zaradi celic v datoteki excel. JDK ne nudi neposrednega API-ja za branje ali pisanje dokumentov Microsoft Excel ali Word. Zanašati se moramo na knjižnico drugega proizvajalca, ki je Apache POI.
Kaj je Apache POI?
Apache POI (Poor Obfuscation Implementation) je Java API za branje in pisanje Microsoftovih dokumentov v obeh formatih .xls in .xlsx . Vsebuje razrede in vmesnike. Knjižnica POI Apache ponuja dve izvedbi za branje datotek excel:
Vmesniki in razredi v Apache POI
Vmesniki
Razredi
Razredi XLS
Razredi XLSX
Koraki za branje podatkov iz datoteke XLS
Korak 1: Ustvarite preprost projekt Java v eclipse.
2. korak: Zdaj ustvarite mapo lib v projektu.
3. korak: Prenesite in dodajte naslednje datoteke jar v mapo lib:
- commons-collections4-4.1.jar Klikni tukaj
- poi-3.17.jar Klikni tukaj
- poi-ooxml-3.17.jar Klikni tukaj
- poi-ooxml-schemas-3.17.jar Klikni tukaj
- xmlbeans-2.6.0.jar Klikni tukaj
4. korak: Nastavite pot razreda:
Z desno miškino tipko kliknite projekt ->Pot gradnje ->Dodaj zunanje datoteke JAR -> izberite vse zgornje datoteke jar -> Uporabi in zaprite.
5. korak: Zdaj ustvarite datoteko razreda z imenom ReadExcelFileDemo in v datoteko zapišite naslednjo kodo.
6. korak: Ustvarite excel datoteko z imenom 'student.xls' in vanjo zapišite nekaj podatkov.
računalniška organizacija in arhitektura
7. korak: Shranite in zaženite program.
Primer branja 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(); } } }
Izhod:
Name Age Height Swarit 23.0 5' Puneet 25.0 6'1' Swastik 22.0 5'5' Tejas 12.0 4'9'
Branje datoteke XLSX
Vsi koraki bodo ostali enaki, razen oblike datoteke.
Tabela: zaposleni.xslx
ridhima tiwari
Primer branja excel datoteke (.xlsx)
V tem primeru uporabljamo razred XSSFWorkbook.
pridruži se posodobitvi mysql
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(); } } }
Izhod:
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
Branje določene vrednosti celice iz datoteke excel (.xlsx)
Tabela: EmployeeData.xlsx
Primer
V naslednjem primeru preberemo vrednost 2ndvrstica in 2ndstolpec. Štetje vrstic in stolpcev se začne od 0. Torej program vrne 'Software Engineer.'
//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 } }
Izhod:
Software Engineer