programing

XLS(Excel) 파일에서 데이터를 읽는 방법 [Java, Android]

bestprogram 2023. 6. 11. 11:07

XLS(Excel) 파일에서 데이터를 읽는 방법 [Java, Android]

스택 오버플로를 검색했지만 명확한 답을 찾지 못했습니다.XLS 파일의 특정 행과 열에서 안드로이드 응용 프로그램으로 데이터를 읽으려면 어떻게 해야 합니까?XLS 파일을 읽으려면 어떻게 해야 합니까?CSV로 변환하려고 하면 오류가 발생하기 때문에 CSV로 변환하고 싶지 않습니다.

http://www.andykhan.com/jexcelapi/tutorial.html#reading 을 사용할 수도 있지만 어떻게 프로젝트에 가져올 수 있는지조차 모르겠습니다.제발 도와주세요.

안녕하세요. 외부 jxl jar만 포함하면 됩니다. 그리고 엑셀 파일을 읽는 과정을 이해하는 데 도움이 되는 동일한 튜토리얼을 거칠 수 있습니다.참고로 엑셀의 첫 번째 시트를 읽고 결과 세트를 작성하는 참조 코드를 첨부합니다.

    public List<String> read(String key) throws IOException  {
    List<String> resultSet = new ArrayList<String>();

    File inputWorkbook = new File(inputFile);
    if(inputWorkbook.exists()){
        Workbook w;
        try {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet sheet = w.getSheet(0);
            // Loop over column and lines
            for (int j = 0; j < sheet.getRows(); j++) {
                Cell cell = sheet.getCell(0, j);
                if(cell.getContents().equalsIgnoreCase(key)){
                    for (int i = 0; i < sheet.getColumns(); i++) {
                        Cell cel = sheet.getCell(i, j);
                        resultSet.add(cel.getContents());
                    }
                }
                continue;
            }
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    else
    {
        resultSet.add("File not found..!");
    }
    if(resultSet.size()==0){
        resultSet.add("Data not found..!");
    }
    return resultSet;
}
private void printlnToUser(Cell str) {
    final Cell string = str;
    if (output.length() > 8000) {
        CharSequence fullOutput = output.getText();
        fullOutput = fullOutput.subSequence(5000, fullOutput.length());
        output.setText(fullOutput);
    }
    output.append(string + "\n");
}

public void ReadXLSX(File path) {
    try {
        FileInputStream fis = new FileInputStream(path);
        XSSFWorkbook myWorkBook = new XSSFWorkbook(fis);
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
        Iterator<Row> rowIterator = mySheet.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        break;
                    default:
                }
                printlnToUser(cell);
            }
        }
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

언급URL : https://stackoverflow.com/questions/16890206/how-to-read-data-from-xls-excel-file-java-android