7

I am trying to check if my excel file already exists. If it doesn't exists, I want to create a new one and if it exists I will delete it and create a new one. I wrote following program but I am getting error at line - workbook= WorkbookFactory.create(instream);

The error is-> java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:89) at tryIng.main(tryIng.java:84)

Here is a program ->

 try {
                String filePath= "C:/Users/pritik/Desktop/t1.xlsx";
                File file = new File(filePath);
                filePath= file.getAbsolutePath(); 
                xlFile = new File(filePath);

                if(xlFile.exists() && !xlFile.isDirectory())
                    xlFile.delete(); //delete if file already exists.
                xlFile.createNewFile();

                inStream = new FileInputStream(xlFile);
                workbook =  WorkbookFactory.create(inStream);  // I get error at this line
                String sheetName="NewSheet";
                Sheet sheet = workbook.createSheet(sheetName);
                FileOutputStream fOut = new FileOutputStream(xlFile);

                int i,j;
                xRows = xTS.length;
                xCols = xTS[0].length;
                for(i =0;i<xRows;i++)
                {
                    row = sheet.createRow(i);
                    for(j=0;j<xCols;j++)
                    {
                        cell = row.createCell(j);
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        cell.setCellValue(xTS[i][j]);
                    } 
                } 
                workbook.write(fOut);
                fOut.flush();
                fOut.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
For Testing
  • 281
  • 2
  • 5
  • 19

1 Answers1

13

Don't create an empty file and try to read it, that won't work. An empty zero byte file is not valid, and can't be loaded Instead, have POI create an new file for you, which you will write later.

Change the code:

if(xlFile.exists() && !xlFile.isDirectory())
    xlFile.delete(); //delete if file already exists.
xlFile.createNewFile();

inStream = new FileInputStream(xlFile);          
workbook =  WorkbookFactory.create(inStream);

To instead be:

if(xlFile.exists() && !xlFile.isDirectory())
    xlFile.delete(); //delete if file already exists.

if (xlFile.toString().endsWith(".xls") {
   workbook = new HSSFWorkbook();
} else {
   workbook = new XSSFWorkbook();
}

Also, if you do want to read an existing file, don't use a stream if you have a file! See this bit of the POI docs for why not.

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • Thanks a lot Gagravarr. It works fine. I also read the information you provided in the link and I changed my code. Thanks again. – For Testing Mar 25 '15 at 13:49
  • 1
    @Gagravarr why are you not using Workbook factory and using hss and xssf workbook is there any reason – Labeo Oct 19 '15 at 05:04
  • 1
    @Labeo WorkbookFactory is only for reading existing files. The OP wants to create a new one from scratch – Gagravarr Oct 19 '15 at 08:20
  • Yeah i am having files that already exists but still i am getting the error – Labeo Oct 19 '15 at 08:44
  • @Labeo Check what the file really is then, not what the extension says. The Apache Tika app in `--detect` mode should tell you – Gagravarr Oct 19 '15 at 08:54