添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have to generate several excel files, but when using a for loop to create the different files, it throws an EmptyFileException:the supplied file was empty . The first time it generates an excel file, but later, it throws that exception. I have tried closing the FileInputStream , but it did not work.

public static void mergeExcelFiles(ArrayList<Employe> employeList, Enterprise enterprise, List<FileInputStream> list) throws IOException {
    ArrayList<String> cell_formulas = generateCellToFormula();
    File file = null;
    for (int j = 0; j < employeList.size(); j++) {
        XSSFWorkbook book = new XSSFWorkbook();
        XSSFSheet sheet = null;
        file = new File(listaEmpleados.get(j).getNombre() + ".xlsx");
        try {
            for (FileInputStream fin : list) {
                XSSFWorkbook b = new XSSFWorkbook(fin);
                for (int i = 0; i < b.getNumberOfSheets(); i++) {
                    sheet = book.createSheet(b.getSheetName(i));
                    copySheets(book, sheet, b.getSheetAt(i));
            setDataWorkerInHoraryModel(book, enterpise, employeList.get(j));
            passWeekendToSheets(book);
            writeFile(book, file);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
                Try debugging list. It seems like there are empty files in there. If so try to check for files that are empty to avoid the exception.
– R4yY
                Dec 1, 2020 at 10:56
                Rafa, I have edited the question to remove the asking-for-help part (all questions here ask for help answering them!) and the description of what you need it for.
– tucuxi
                Dec 1, 2020 at 11:38

https://poi.apache.org/apidocs/4.0/org/apache/poi/EmptyFileException.html

Exception thrown if an Empty (zero byte) file is supplied

 for (FileInputStream fin : list) {
                XSSFWorkbook b = new XSSFWorkbook(fin);
                for (int i = 0; i < b.getNumberOfSheets(); i++) {
                    sheet = book.createSheet(b.getSheetName(i));
                    copySheets(book, sheet, b.getSheetAt(i));

The list you are passing has file that is 0 bytes.

========= Edited ===========

You are doing FileInputStream fin : list, there will be a lock on that file.

You need to close the inputstream if you want your system resources released back.

fin.close() is what you need, before the next iteration of the forloop.

but i generate the first excel file with that list. I repeat the process with a for loop and i don´t clean the list or anything – Rafa Dec 1, 2020 at 11:09 You have more than 1 file in [LIST] Maybe the first file is fine...but the second workbook is not. @Rafa – JCompetence Dec 1, 2020 at 11:13

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.