티스토리 뷰

반응형
SMALL

 

 

 

 

Excel 읽기.

 

POI Library 를 이용.

Project 에 Library 추가.

 

 

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
 
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
public class Write {
 
    /**
     * 용도: 
     * 파라미터: 
     * 작성자 :
     */
    public static void main(String[] args) {
        
        ArrayList<ArrayList> rows = new ArrayList<ArrayList>();
        
        String filePath = "d:/윤지훈/통계.xlsx";
        try {
            // *********************************************** xlsx 파일 읽기
            XSSFWorkbook workBook = new XSSFWorkbook(new FileInputStream(new File(filePath)));
            XSSFSheet sheet = null;
            XSSFRow row = null;
            XSSFCell cell = null;
            // ****************************************************************
            int sheetNum = workBook.getNumberOfSheets();
            for (int k = 0; k < sheetNum; k++) {
                sheet = workBook.getSheetAt(k);
                int rowss = sheet.getPhysicalNumberOfRows();    //row 의 총 갯수를 구함.
                
                
                for (int r = 0; r < rowss; r++) {
                    row = sheet.getRow(r);
                    int cellss = row.getPhysicalNumberOfCells();    //cells 의 총 갯수를 구함.
                    ArrayList cells = new ArrayList<>();
                    for (short c = 0; c < cellss; c++) {
                        
                        cell = row.getCell(c);
                        if (cell == null) {
                            continue;
                        }
                        switch (cell.getCellType()) {
                        case 0:
                            cells.add(String.valueOf(cell.getNumericCellValue()));        //int to String 형변환 하여 저장.
                            break;
                        case 1:
                            cells.add(cell.getStringCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            cells.add(cell.getStringCellValue());
                            break;
                        }
                        
                    } // cell
                    rows.add(cells);
                } // row
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        for(int i = 0; i < rows.size(); i ++){
            for(int j = 0; j < rows.get(i).size(); j++){
                System.out.print(rows.get(i).get(j)+"\t\t");
            }
            System.out.println();
        }
        
        
    }
}
 
cs

 

프로그램 결과

 

Excel 내용

 

 

이 라이브러리를 이용하여 차계부를 만들 예정.

반응형
LIST
댓글