简体   繁体   中英

How continue in the program after finding all the .xls files in a specific directory?

I am making a program where I would read data from multiple excel files and store the data in tables. I have managed to make this program and works fine when the user give the full path of the file. What I am trying to do now is the user would give the directory of where all the excel files are, automatically find all the .xls files and do the rest program for each of them (read the data, create the table and insert the data in it).

The code for the user to give the path and print all the .xls files is:

String dirpath = "";
Scanner scanner1 = new Scanner(System.in);
while (true) {
    System.out.println("Please give the directory:");
    dirpath = scanner1.nextLine();
    File fl = new File(dirpath);
    if (fl.canRead()) break;
    System.out.println("Error:Directory does not exists");
}
try{
    String files;
    File folder = new File(dirpath);
    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].isFile()) {
            files = listOfFiles[i].getName();
                if (files.endsWith(".xls") || files.endsWith(".XLS")) {
                    System.out.println(files);
                }
        }
    }
}catch (Exception e){
    System.out.println();
}

How I would get each one of these file to continue in the rest program? What I am doing next is described in the code below:

List sheetData = new ArrayList();
FileInputStream fis = null;
try {
    fis = new FileInputStream(strfullPath);
    HSSFWorkbook workbook = new HSSFWorkbook(fis);
    HSSFSheet sheet = workbook.getSheetAt(0);

    Iterator rows = sheet.rowIterator();
    while (rows.hasNext()) {
        HSSFRow row = (HSSFRow) rows.next();
        Iterator cells = row.cellIterator();

        List data = new ArrayList();
        while (cells.hasNext()) {
            HSSFCell cell = (HSSFCell) cells.next();
            data.add(cell);
        }
        sheetData.add(data);
    }    
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (fis != null) {
        fis.close();
    }
}

After this code I am creating the table and after that I am filling it with the data.

Wrap the code into a method and instead of

System.out.println(files);

call this method and pass listOfFiles[i] .

You can add the the XLS files to a list of files then iterate it for the xls file processing code.

//Delcare
List<File> xlsFiles = new ArrayList<File>();


//Add the below code in the if loop 
xlsFiles.add(listOfFiles[i])

for(File xlsFile : xlsFiles){

List sheetData = new ArrayList();
FileInputStream fis = null;
try {
    fis = new FileInputStream(xlsFile);
}

Regards, Dinesh.R

You can read data from excel file and it will usable for further execution process.

Please find following example for the same.

private void getSpecificExcelWSheetDataList(String absolutePath) {

    LogLoader.serverLog.trace("In getSpecificExcelWSheetDataList() [File Location : " + absolutePath + "]");

    try {

        File file = new File(absolutePath);

        if (!file.isAbsolute()) {
            LogLoader.serverLog.error("Source file does not exist");
        } else if (!file.canRead()) {
            LogLoader.serverLog.error("Source file doesn't have permission for read operation");
        } else {
            FileInputStream ExcelFile = new FileInputStream(absolutePath);

            excelWBook = new XSSFWorkbook(ExcelFile);

            Iterator<Sheet> sheetIterator = excelWBook.sheetIterator();

            while (sheetIterator.hasNext()) {
                XSSFSheet excelWSheet = (XSSFSheet) sheetIterator.next();
                Iterator<Row> rowIterator = excelWSheet.rowIterator();

                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    if (row.getRowNum() != 0) {
                        this.serviceProcessData = new ServiceProcessData();
                        Iterator<Cell> cellIterator = row.cellIterator();

                        while (cellIterator.hasNext()) {
                            Cell cell = cellIterator.next();
                            int columnIndex = cell.getColumnIndex();
                            String cellValue = dataFormatter.formatCellValue(cell).trim();

                            System.out.println("Row Number :"+row.getRowNum()
                                        +"Column Number :"+columnIndex
                                        +"Value : "+cellValue);
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        LogLoader.errorLog.error(e.getMessage(), e);
        LogLoader.serverLog.error(e.getMessage());
    }

    LogLoader.serverLog.trace("Out getSpecificExcelWSheetDataList()");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM