-
[JAVA] μμ νμΌ(.xlsx) μ½μ΄μ€κΈ°_POIνμ©DEV π/JAVA 2020. 3. 10. 22:27λ°μν
1 POI λΌμ΄λΈλ¬λ¦¬ λ€μ΄λ‘λ & νλ‘μ νΈμ μ μ©
[ μμ νμΌ(. xlsx) μ½μ΄μ€κΈ° ]
μμλ‘ μ½μ΄μ¬ μμ νμΌμ λ΄μ© (ExcelExFile.xlsx)
μ κ΅ λ©κ°λ°μ€ μ§μ μ μ΄λ¦μΌλ‘ νκ³ λΉμ¨(%)λ μ λ ¬μ μν΄μ!
* νμΌμ κ²½λ‘λ μλκ²½λ‘λ‘ νλ κ²μ΄ μ’λ€
μμ νκ²½μ΄ λ³νλλΌκ³ κ°μ ν¨ν€μ§ λ΄μ λ£μ΄λμ μμ νμΌμ κ²½λ‘κ° κ·Έλλ‘!String path = Work4.class.getResource("").getPath(); File file = new File(path + "ExcelExFile.xlsx");
String path = νμ¬ν΄λμ€λͺ .class.getResource("").getPath();
→ pathμ νμ¬ ν΄λμ€μ μ λκ²½λ‘κ° μ μ₯λλ€File file = new File(path + "μμ νμΌμ΄λ¦");
→ path ν΄λ λ΄μ μλ νμΌ
*μμ μ λ΄μ©μ μ½μ΄μμ listμ λ£μ΄λμ κ²μ΄λ€νμ μμ νμΌμ λ΄μ©μ μμ ν΄μ λ€μ μΈ λ νΈνκΈ° μν΄!
exDataλ μμ μ μΈκ°μ§ μλ£(μ΄λ¦, λΉμ¨, %)λ₯Ό ν 리μ€νΈ(list)μ λ£κΈ°μν΄ μμλ‘ μ μν΄λμ classfor(rowindex=2; rowindex <rows ; rowindex++) { exData ed = new exData(); //ν μ½κΈ° XSSFRow row = sheet.getRow(rowindex); XSSFCell cell=row.getCell(2); ed.setName(String.valueOf(row.getCell(0))); ed.setNum(String.valueOf(row.getCell(1))); String value = ""; //cell value typeλ³λ‘ λ΄μ© μ½κΈ° switch (row.getCell(2).getCellType()){ case FORMULA: value = row.getCell(2).getFormula(); break; case STRING: value = row.getCell(2).getStringCellValue() + ""; break; case NUMERIC: value = row.getCell(2).getNumericCellValue() + ""; break; case BLANK: value = row.getCell(2).getBooleanCellValue() + ""; break; case ERROR: value = row.getCell(2).getErrorCellValue() + ""; break; } ed.setPer(value); list.add(ed); }
μ λͺ©(μ κ΅ λ©κ°λ°μ€ μ§μ )κ³Ό ν€λλ€(μ΄λ¦/λΉμ¨/%)λ 리μ€νΈμ λ£μ§ μκΈ° μν΄μ
λ°λ³΅λ¬Έμ index 2λΆν° μμ
XSSFRow row = sheet.getRow(rowindex);
→ νμ¬ μνΈμ ν μ°¨λ‘λ‘ μ½κΈ°
XSSFCell cell = row.getCell(n);
→ row(νμ¬ ν)μ nλ²μ§Έ μ κ°
switch(row.getCell(n).getCellType()) { ... }
→ row(νμ¬ ν)μ nλ²μ§Έ μ κ°μ νμ λ³λ‘ λ΄μ© μ½μ΄μ€κΈ°
(μ¬κΈ°μλ μ λ³λ‘ νμ μ΄ μ ν΄μ Έμμ΄μ νμμμ§λ§ μμλ₯Ό λ€κΈ°μν΄!)
βΌ exData ν΄λμ€(getter/setter) 보기
λ보기class exData implements Comparable<exData>{ private String name; private String num; private String per; public String getName(){ return name; } public String getNum(){ return num; } public String getPer(){ return per; } public void setName(String name){ this.name = name; } public void setNum(String num){ this.num = num; } public void setPer(String per){ this.per = per; } //λ΄λ¦Όμ°¨μ μ λ ¬ public int compareTo(exData ex){ if(Double.parseDouble(this.num) < Double.parseDouble(ex.getNum())) { return 1; } else if(Double.parseDouble(this.num) > Double.parseDouble(ex.getNum())) { return -1; } return 0; } }
readExcel μ 체 μ½λ
public ArrayList<exData> readExcel() { String path = Work4.class.getResource("").getPath(); ArrayList<exData> list = new ArrayList<exData>(); try{ File file = new File(path + "ExcelExFile.xlsx"); FileInputStream fis = new FileInputStream(file); XSSFWorkbook workbook = new XSSFWorkbook(fis); //μμ indexλ 0λΆν° μμ int rowindex=0; int colindex=0; //μνΈ μ XSSFSheet sheet = workbook.getSheetAt(0); //νμ μ int rows = sheet.getPhysicalNumberOfRows(); for(rowindex=2; rowindex <rows ; rowindex++) { exData ed = new exData(); //ν μ½κΈ° XSSFRow row = sheet.getRow(rowindex); XSSFCell cell=row.getCell(2); ed.setName(String.valueOf(row.getCell(0))); ed.setNum(String.valueOf(row.getCell(1))); String value = ""; switch (row.getCell(2).getCellType()){ case FORMULA: value = row.getCell(2).getFormula(); break; case STRING: value = row.getCell(2).getStringCellValue() + ""; break; case NUMERIC: value = row.getCell(2).getNumericCellValue() + ""; break; case BLANK: value = row.getCell(2).getBooleanCellValue() + ""; break; case ERROR: value = row.getCell(2).getErrorCellValue() + ""; break; } ed.setPer(value); list.add(ed); } } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } return list; }
λ°μν'DEV π > JAVA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Javascript/HTML] νμΌ λ€μ΄λ‘λ μ νμΌμ΄λ¦ λ°κΎΈκΈ° (0) 2023.03.17 [JAVA] μμ νμΌ(.xlsx) μ°κΈ° / μ€νμΌ μ μ© (2) 2020.03.17 [JAVA] νλ ¬ μ°μ°_λ§μ λΊμ κ³±μ / λλ€ν¨μ μ¬μ©νκΈ° (0) 2020.03.10 [JAVA] μλ° μμ νμΌ μμ _Apache POI λΌμ΄λΈλ¬λ¦¬ (0) 2020.03.10