Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Apache POI读取和编辑大型Excel文件_Java_Excel_Apache_Apache Poi - Fatal编程技术网

Java 使用Apache POI读取和编辑大型Excel文件

Java 使用Apache POI读取和编辑大型Excel文件,java,excel,apache,apache-poi,Java,Excel,Apache,Apache Poi,我读过很多关于ApachePOI大文件问题的文章和文章。但是我仍然有一些问题需要解决。 因此,我的任务是从许多Excel文件中删除空行。它们被安排在目录树结构中,我想一次完成所有程序的执行。 为此,我有下一个方法: public static void listFilesForFolder(final File folder) { for (final File fileEntry : folder.listFiles()) { if (fileEntry

我读过很多关于ApachePOI大文件问题的文章和文章。但是我仍然有一些问题需要解决。 因此,我的任务是从许多Excel文件中删除空行。它们被安排在目录树结构中,我想一次完成所有程序的执行。 为此,我有下一个方法:

public static void listFilesForFolder(final File folder) {
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                System.out.println("\n" + "Folder: " + fileEntry.getName());
                listFilesForFolder(fileEntry);
            } else {
                System.out.println("File:" + fileEntry.getName() + " = " + humanReadableByteCount(fileEntry.length()) + " -> "); 
                System.out.println("New size: " + humanReadableByteCount(workWithWorkbook2(fileEntry)));
            }
        }
    }
humanReadableByteCount方法在这里并不重要。所有使用Excel进行的工作均采用workWithWorkbook2方法。我多次重写此方法,尝试使用XSSF(用户模型)SXSSF和XSSFReader(eventusermodel),结果如下:

  • XSSF-通过ApachePOI的这个实现,我的程序以OutOfMemoryException结束。我试图设置更多的可用内存(-Xmx2g和-Xmx3g),但这还不够。程序开始时使用的RAM约为1.5 GB,在第一次使用Excel之后,它的RAM约为1.7 GB,但随后打开了一个更大的Excel(约40 MB),内存使用了超过2.8 GB的RAM

  • 我只找到了创建新文件的示例。但那不是我需要的。我需要读取文件,分析它并删除空行

  • XSSFReader—在这里,我对XMLParser有一些问题(目前仍然有)。然而,这只是一个阅读器,我需要能够编辑一个文件

  • 我想向您展示我的workWithWorkbook2方法,但它只包含XSSFReader的实现,因为我对不同的ApachePOI实现进行了实验。 因此,有一种方法workWithWorkbook2,其中有许多注释行,可以让您查看我的尝试:

    public static long workWithWorkbook2(File fileEntry) {
    
            if (fileEntry.getName().endsWith(".xltx")) {
                System.out.println("== Jump over .xltx file ==");
            }
            else {
    
                //FileInputStream file = new FileInputStream(new File(fileEntry.getPath()));
    
                //Workbook wb = WorkbookFactory.create(new File(fileEntry.getPath()));
    
                //SXSSFWorkbook workbook = new SXSSFWorkbook(wb, 1000);
    
                //workbook.setCompressTempFiles(true);
    
                OPCPackage pkg = OPCPackage.open(fileEntry.getPath());
    
                XSSFReader r = new XSSFReader(pkg);
    
                SharedStringsTable sst = r.getSharedStringsTable();
    
                XMLReader parser = fetchSheetParser(sst);
    
                Iterator<InputStream> sheets = r.getSheetsData();
    
                while(sheets.hasNext()) {
                    InputStream sheet = sheets.next();
                    InputSource sheetSource = new InputSource(sheet);
                    parser.parse(sheetSource);
                    sheet.close();
                }
    
    //            FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
    //
    //            for (int i = 0; i<wb.getNumberOfSheets(); i++) {
    //
    //                Sheet sheet = wb.getSheetAt(i);
    //
    //                Iterator<Row> rowIterator = sheet.iterator();
    //                while (rowIterator.hasNext()) {
    //
    //                    Row row = rowIterator.next();
    //                    Iterator<Cell> cellIterator = row.cellIterator();
    //
    //                    while (cellIterator.hasNext()) {
    //
    //                        Cell cell = cellIterator.next();
    //
    //                        if (cell == null) {
    //                            System.out.println(row.getRowNum() + " NULL, " + cell.getCellType() + ", " + cell.getColumnIndex());
    //                        }
    //                        else {
    //
    //                            switch (evaluator.evaluateInCell(cell).getCellType()) 
    //                            {
    //                                case Cell.CELL_TYPE_STRING:
    //                                    //System.out.println(row.getRowNum() + " String, " + cell.getCellType() + ", " + cell.getColumnIndex() + ", " + cell.getStringCellValue());
    //                                    break;
    //                                case Cell.CELL_TYPE_NUMERIC:
    //                                    //System.out.println(row.getRowNum() + " Numeric, " + cell.getCellType() + ", " + cell.getColumnIndex() + ", " + cell.getNumericCellValue());
    //                                    break;
    //                                case Cell.CELL_TYPE_FORMULA:
    //                                    //System.out.println(row.getRowNum() + " Formula, " + cell.getCellType() + ", " + cell.getColumnIndex() + ", " + cell.getCellFormula());
    //                                    break;
    //                                case Cell.CELL_TYPE_BOOLEAN:
    //                                    //System.out.println(row.getRowNum() + " Boolean, " + cell.getCellType() + ", " + cell.getColumnIndex() + ", " + cell.getBooleanCellValue());
    //                                    break;
    //                                case Cell.CELL_TYPE_BLANK:
    //                                    //System.out.println(row.getRowNum() + " Empty, " + cell.getCellType() + ", " + cell.getColumnIndex());
    //                                    break;
    //                                case Cell.CELL_TYPE_ERROR:
    //                                    //System.out.println(row.getRowNum() + " Error, " + cell.getCellType() + ", " + cell.getColumnIndex());
    //                                    break;
    //                                default:
    //                                    //System.out.println(row.getRowNum() + " WTF type?, " + cell.getCellType() + ", " + cell.getColumnIndex());
    //                                    break;
    //                            }
    //
    //                        }
    //                    }
    //
    //                } 
    //
    //            }
    //            
    //            evaluator.clearAllCachedResultValues();
    
                //file.close();
    
            }
            return 0;
        }
    
    publicstaticlongtworkwithworkbook2(文件条目){
    if(fileEntry.getName().endsWith(“.xltx”)){
    System.out.println(“==跳过.xltx文件==”);
    }
    否则{
    //FileInputStream file=newfileinputstream(新文件(fileEntry.getPath());
    //工作簿wb=WorkbookFactory.create(新文件(fileEntry.getPath());
    //SXSSFWorkbook工作簿=新的SXSSFWorkbook(wb,1000);
    //workbook.setCompressTempFiles(true);
    OPCPackage pkg=OPCPackage.open(fileEntry.getPath());
    XSSFReader r=新XSSFReader(pkg);
    SharedStringsTable sst=r.getSharedStringsTable();
    XMLReader parser=fetchSheetParser(sst);
    迭代器sheets=r.getSheetsData();
    while(sheets.hasNext()){
    InputStream sheet=sheets.next();
    InputSource sheetSource=新的InputSource(表);
    parser.parse(sheetSource);
    sheet.close();
    }
    //FormulaEvaluator evaluator=wb.getCreationHelper().createFormulaEvaluator();
    //
    
    //对于(int i=0;我如何自动处理小文件,以及少数需要手动处理大于3gb的大文件?这是一个有趣的想法;-)但这不是一个解决方案,因为下周可能会有一个新的报告,我不得不再次手动处理。@user2377585您对此有解决方案吗?请分享最好的一个。