该进程无法访问该文件,因为该文件正由另一个进程使用

时间:2020-12-08 20:23:39

I am trying to read data from one sheet of excel file and write to other sheet of same excel file.I have tried this :

我试图从一张excel文件中读取数据并写入相同excel文件的其他表。我试过这个:

FileInputStream file = new FileInputStream(new File("E:\\excel\\input.xlsx"));
            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook = new XSSFWorkbook(file);
            //Get first/desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);
            //Iterate through each rows one by one
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) 
            {
                Row row = rowIterator.next();
                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) 
                {
                    Cell cell = cellIterator.next();
                    //Check the cell type and format accordingly
                    switch (cell.getCellType()) 
                    {
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print((int)cell.getNumericCellValue()+ "       ");
                        break;
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue());
                        break;
                    }
                }
                System.out.println("");
            }
            file.close();
            CreationHelper createHelper = workbook.getCreationHelper();
            XSSFSheet newSheet = workbook.createSheet("new sheet");
            // Create a row and put some cells in it. Rows are 0 based.
            Row row = newSheet.createRow((short)0);
            // Create a cell and put a value in it.
            Cell cell = row.createCell(0);
            cell.setCellValue(1);
            // Or do it on one line.
            row.createCell(1).setCellValue(1.2);
            row.createCell(2).setCellValue(
                    createHelper.createRichTextString("This is a string"));
            row.createCell(3).setCellValue(true);
            System.out.println("writing to file");
            // Write the output to a file
            FileOutputStream fileOut = new FileOutputStream(new File("E:\\excel\\input.xlsx"));
            workbook.write(fileOut);
            fileOut.close();

I have successfully read data from excel sheet But I am getting exception while writing in new sheet given below

我已成功读取excel表中的数据但是我在下面给出的新表中写了异常

java.io.FileNotFoundException: E:\excel\input.xlsx (The process cannot access the file because it is being used by another process)

2 个解决方案

#1


3  

This will happen when you are trying to execute this program while the "input.xlsx" file is already opened by another application.

当您尝试执行此程序而另一个应用程序已打开“input.xlsx”文件时,会发生这种情况。

Please close any instance of MS Excel and try to run it again

请关闭任何MS Excel实例并尝试再次运行它

#2


0  

Just a guess but XSSFWorkbook should be implementing Closeable thus it has a .close() that should be called. Maybe the file result still in use because of that? You are probably going to need a middle step there: write to a new file, close the XSSFWorkbook and then copy the new file on the old one that you want to write on

只是一个猜测,但XSSFWorkbook应该实现Closeable因此它有一个应该被调用的.close()。也许文件结果仍在使用中因为那个?您可能需要在那里迈出中间步骤:写入新文件,关闭XSSFWorkbook,然后将新文件复制到要写入的旧文件上

#1


3  

This will happen when you are trying to execute this program while the "input.xlsx" file is already opened by another application.

当您尝试执行此程序而另一个应用程序已打开“input.xlsx”文件时,会发生这种情况。

Please close any instance of MS Excel and try to run it again

请关闭任何MS Excel实例并尝试再次运行它

#2


0  

Just a guess but XSSFWorkbook should be implementing Closeable thus it has a .close() that should be called. Maybe the file result still in use because of that? You are probably going to need a middle step there: write to a new file, close the XSSFWorkbook and then copy the new file on the old one that you want to write on

只是一个猜测,但XSSFWorkbook应该实现Closeable因此它有一个应该被调用的.close()。也许文件结果仍在使用中因为那个?您可能需要在那里迈出中间步骤:写入新文件,关闭XSSFWorkbook,然后将新文件复制到要写入的旧文件上