简体   繁体   English

如何使用Apache POI在已存在的Excel文件中创建新工作表

[英]How to create new sheet in already existed excel file using apache poi

I am trying to create a new excelsheet for the existing excel file, but i am getting error org.apache.poi.EmptyFileException . 我正在尝试为现有的excel文件创建一个新的excelsheet,但是出现了org.apache.poi.EmptyFileException错误。 Below is my code 下面是我的代码

public static void main(String[] args)
    {
        XSSFWorkbook workbook = null;
        File file = new File("C:/Users/NewUser/Desktop/Sample.xls");
        FileOutputStream fileOut = null;
        try 
        {
            fileOut = new FileOutputStream(file);

             if (file.exists()) {
                    try {
                        workbook = (XSSFWorkbook)WorkbookFactory.create(file);
                    } catch (InvalidFormatException e) {
                        e.printStackTrace();
                    } catch (EncryptedDocumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    XSSFSheet sheet = workbook.createSheet("Sample sheet2");
                }
                else{
                    workbook = new XSSFWorkbook();
                    XSSFSheet sheet = workbook.createSheet("Sample sheet1");
                }
        } 
        catch (FileNotFoundException e1) 
        {
            e1.printStackTrace();
        }


        try {
            workbook.write(fileOut);
            System.out.println("File Created Succesfully");
            fileOut.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

How to overcome with that exception and how to create multiple sheets with in a single excel file? 如何克服这种例外情况,以及如何在单个excel文件中创建多个工作表?

fileOut = new FileOutputStream(file);

erases your file, but it still exists ( file.exists() == true) ). 删除您的文件,但它仍然存在( file.exists() == true) )。 So you are opening an empty file when using WorkbookFactory.create(...) and you get an EmptyFileException . 因此,使用WorkbookFactory.create(...)时,您正在打开一个空文件,并且得到EmptyFileException

If you debug your prog and stop before the use of WorkbookFactory.create(...) you will see a zero file size for Sample.xls . 如果在使用WorkbookFactory.create(...)之前调试程序并停止,则Sample.xls文件大小Sample.xls

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用java中的selenium webdriver中的apache poi在excel文件中创建新工作表 - How to create a new sheet in an excel file using apache poi in selenium webdriver with java 无法使用apache POI在工作簿中创建新的Excel工作表 - Cannot create new excel sheet in a workbook using apache POI 如何使用 Apache POI 将现有 Excel 工作表中的一行复制到新的 Excel 工作表中? - How to copy a row from existing excel sheet to a new excel sheet using Apache POI? 使用Apache POI for Java在现有Excel工作簿中创建新工作表 - Creating New Sheet In Existing Excel Workbook Using Apache POI for Java 使用Java和Apache POI将工作表追加到现有Excel文件 - Appending sheet to existing excel file using Java and Apache POI 使用Apache POI将工作表追加到Excel文件中-NullPointerException导致POIXMLException - Appending a sheet to an Excel file using Apache POI - POIXMLException caused by NullPointerException 如何使用Apache POI 3.14创建和编辑密码保护Excel工作表? - How to create and edit a password protect excel sheet using Apache POI 3.14? 如何使用 POI Apache 将工作表从 Excel 文件单元弹出文档复制到另一个工作表? - How to copy a sheet from an Excel File Cell pop-up docuentation to another using POI Apache? 如何使用Apache POI从Excel工作表中检索日期 - How to retrieve date from an excel sheet using Apache POI 如何使用 Apache POI 为 Excel 工作表中的行应用背景颜色? - How to apply background color for the rows in excel sheet using Apache POI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM