【问题标题】:Excel Read Multiple Files Java Apache POIExcel 读取多个文件 Java Apache POI
【发布时间】:2018-02-20 16:53:04
【问题描述】:

我可以从一个 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;

}`

【问题讨论】:

  • 只扫描目录中具有适当扩展名的文件
  • 你能告诉我怎么做吗:/ .... 我知道它的基本原理

标签: java excel apache-poi


【解决方案1】:

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

    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"));
        }
    }

第二步:使用过滤器读取一个目录下的所有Excel文件:

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

【讨论】: