Excel读取多个文件Java Apache POI

时间:2018-02-20 16:53:04

标签: java excel apache-poi

我可以从一个excel中读取一张纸,然后用另一张excel写入。但是现在我想读一下excel文件的文件夹。我怎么做? 此程序适用于一个特定文件。我想从多个xls文件中读取每个提取的第1张纸张,并在新文件中仅打印第一张纸张。

public static boolean readWriteXLSXFile() throws IOException
{
 //File filename=new File("/temp/raw");
    //InputStream ExcelFileToRead = new FileInputStream(filename);

 String pathname="C:/temp/";
 String ffile="raw";
 String oldfilename = pathname + ffile;
    String newExcel = "C:/temp/updatedraw";
    String sheetname= ffile + "Sheet";
    InputStream ExcelFileToRead = new FileInputStream(oldfilename);

    XSSFWorkbook  workbook = new XSSFWorkbook(ExcelFileToRead);
    XSSFSheet datatypeSheet = workbook.getSheetAt(0);


            XSSFWorkbook workbook1 = new XSSFWorkbook(); 
    XSSFSheet newsheet = workbook1.createSheet(sheetname);

    XSSFRow currentRow, newrow; 
    XSSFCell currentCell, newcell;
            Iterator iterator = datatypeSheet.rowIterator();
            int rowIndex=0;

    while (iterator.hasNext())
    {        

        currentRow=(XSSFRow) iterator.next();             
        newrow=newsheet.createRow(currentRow.getRowNum());
        int cellIndex=0;
        Iterator cellIterator = currentRow.cellIterator();

                    while (cellIterator.hasNext())
                    {
                            currentCell=(XSSFCell) cellIterator.next();

                            XSSFCellStyle newCellStyle ;


                                    switch (currentCell.getCellType())
                                    {
                                            case XSSFCell.CELL_TYPE_STRING:

                                                    System.out.print(currentCell.getStringCellValue()+" ");

                                                    newcell= newrow.createCell(cellIndex);
                                                    newcell.setCellValue(currentCell.getStringCellValue());

                                                    newCellStyle = newcell.getSheet().getWorkbook().createCellStyle();
                                                    newCellStyle.cloneStyleFrom(currentCell.getCellStyle());
                                                    newcell.setCellStyle(newCellStyle);
                                                    cellIndex++;

                                                    break;


                                            case XSSFCell.CELL_TYPE_NUMERIC:

                                                    System.out.print(currentCell.getNumericCellValue()+" ");

                                                    newcell= newrow.createCell(cellIndex);
                                                    newcell.setCellValue(currentCell.getNumericCellValue());

                                                    newCellStyle = newcell.getSheet().getWorkbook().createCellStyle(); 
                                                    newCellStyle.cloneStyleFrom(currentCell.getCellStyle());
                                                    newcell.setCellStyle(newCellStyle);
                                                    cellIndex++;

                                                    break;


                                            default:

                                                    break;
                                    }
        }

        FileOutputStream fileOut = new FileOutputStream(newExcel);
        workbook1.write(fileOut);
        fileOut.close();
        System.out.println();
    }
            return true;

}`

2 个答案:

答案 0 :(得分:1)

步骤1:定义文件过滤器(这是具有典型Excel-Suffix的所有文件的示例,根据您的需要进行调整):

    public class ExcelFileFilter implements java.io.FileFilter {
        @Override
        public boolean accept(File file) {
            return file != null &&
                file.isFile() &&
                file.canRead() &&
                (file.getName().endsWith("xls")
                || file.getName().endsWith("xlsx"));
        }
    }

步骤2:使用过滤器读取目录中的所有Excel文件:

FileFilter filter = new ExcelFileFilter ();
File directory = new File("MyDirectoryWithExcelfiles");
File[] files = directory.listFiles(filter);
for (File file : files) {
    //init workbook and do stuff
}

答案 1 :(得分:0)

使用apache poi读取Java中的excel文件:-

    /**
     * Checks if provided module is rejected during load.
     *
     * @param {Object} module - Module to be checked.
     * @return {Boolean}
     */
    function isRejected(module) {
        return registry[module.id] && (registry[module.id].inited || registry[module.id].error);
    }

    /**
     * Checks if provided module has unresolved dependencies.
     *
     * @param {Object} module - Module to be checked.
     * @returns {Boolean}
     */
    function isPending(module) {
        if (!module.depCount) {
            return false;
        }

        return module.depCount > _.filter(module.depMaps, isRejected).length;
    }

我已找到完整的指南here