清理未关闭的 ZipFile 以在 excel 文件中存档

Posted

技术标签:

【中文标题】清理未关闭的 ZipFile 以在 excel 文件中存档【英文标题】:Cleaning up unclosed ZipFile for archive in excel file 【发布时间】:2020-01-23 23:15:21 【问题描述】:

所以我一直在研究一个使用 excel 作为数据池的简单自动化框架。但是,在更新 pom.xlm 的版本时,我在控制台中遇到了这个错误。

Cleaning up unclosed ZipFile for archive C:<workspace repo>\ExcelFile.xlsx

我不知道出了什么问题。此错误不会停止执行,我的测试运行顺利。我怎么可能解决这个问题。我找不到与此相关的任何内容。

以下是依赖项:

<dependencies>
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>xml-apis</groupId>
        <artifactId>xml-apis</artifactId>
        <version>1.4.01</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-contrib</artifactId>
        <version>3.7-beta3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.9</version>
    </dependency>
    <dependency>
        <groupId>org.dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.jexcelapi</groupId>
        <artifactId>jxl</artifactId>
        <version>2.6.3</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>com.relevantcodes</groupId>
        <artifactId>extentreports</artifactId>
        <version>2.41.2</version>
    </dependency>
    <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>3.1.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-stax-api_1.0_spec</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.0.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>4.0-beta</version>
    </dependency>
    <dependency>
        <groupId>velocity</groupId>
        <artifactId>velocity-dep</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.53.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
        <version>2.53.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>
    <dependency>
        <groupId>org.uncommons</groupId>
        <artifactId>reportng</artifactId>
        <version>1.1.4</version>
    </dependency>
    <dependency>
        <groupId>ru.yandex.qatools.ashot</groupId>
        <artifactId>ashot</artifactId>
        <version>1.5.4</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>4.3.1</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-jvm-deps</artifactId>
        <version>1.0.6</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports-testng-adapter</artifactId>
        <version>1.0.3</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>4.3.1</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

这是我处理excel的方法:

public static String get(String sheetName, String scenario, String columnName) throws IOException 

        org.apache.poi.ss.usermodel.Workbook tempWB;
        int k=0;
        try 

            if(workbookPath.contains(".xlsx") || workbookPath.contains(".xlsm"))
                tempWB = new XSSFWorkbook(workbookPath);
             else             
                InputStream inp = new FileInputStream(workbookPath);
                tempWB = (org.apache.poi.ss.usermodel.Workbook) new HSSFWorkbook(new POIFSFileSystem(inp));                 
            

            org.apache.poi.ss.usermodel.Sheet sheet = tempWB.getSheet(sheetName);

            int rows = sheet.getPhysicalNumberOfRows();

            for(int i=0;i<rows;i++)
                //Getting the number of defined cells in the row
                int cols = sheet.getRow(0).getPhysicalNumberOfCells();

                for (int j = 0; j < cols; j++)  
                    if(sheet.getRow(i).getCell(j,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).toString().replace(".0", "").equalsIgnoreCase(columnName)) 
                        k=j;
                    

                    map.put(sheet.getRow(i).getCell(0,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).toString().replace(".0", ""),
                            sheet.getRow(i).getCell(k,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).toString().replace(".0", ""));
                
             
         catch (Exception e)
            e.printStackTrace(); 
         

        return map.get(scenario);
    

【问题讨论】:

【参考方案1】:

我注意到您尚未关闭 InputStream。如果不关闭流,文件将被锁定,直到输入流被关闭或 JVM 关闭。

因此,在上述上下文中,最简单的方法可能是使用 try-with-resources 构造自动关闭,

try (FileInputStream inp = new FileInputStream(workbookPath)) 
    tempWB = (org.apache.poi.ss.usermodel.Workbook) new HSSFWorkbook(new POIFSFileSystem(inp)); 
 catch (Exception e) 
    Log.error("Class Utils | Method setExcelFile | Exception desc : "+ e.getMessage());

希望对您有所帮助。 :)

【讨论】:

打印Class Utils | Method setExcelFile | Exception desc : The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF) TestString 我添加了tempWB.close();,错误消失了! :)【参考方案2】:

我添加了tempWB.close();,错误消失了! :)

【讨论】:

以上是关于清理未关闭的 ZipFile 以在 excel 文件中存档的主要内容,如果未能解决你的问题,请参考以下文章

ZipFile 关闭后 ZipEntry 是不是仍然存在?

Excel进程未关闭[重复]

ZipFile未声明

Excel工作表未关闭[重复]

怎样在Android中解析doc,docx,xls,xlsx格式文

准备我的数据以在 SVM libsvm matlab 中进行训练 [关闭]