kamalpatel291
kamalpatel291

Reputation: 31

How to fix NotOfficeXmlFileException in java Apache POI?

I'm trying to create a new excel file with just "hello" in it. Here's my code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author kamal
 */
public class JavaApplication4 {
    private static String dir = "";
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            // TODO code application logic here
            JFileChooser jc = new JFileChooser();
                        jc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int output = jc.showOpenDialog(null);
            if(output == JFileChooser.APPROVE_OPTION){
                File f = jc.getSelectedFile();
                String directory = f.getAbsolutePath();
                setDir(directory);
            }


            FileOutputStream out = new FileOutputStream(new File(getDir()+"\\Book2.xlsx"));
            FileInputStream in = new FileInputStream(new File(getDir()+"\\Book2.xlsx"));

           org.apache.poi.ss.usermodel.Workbook workbook = new XSSFWorkbook(in);
            org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);

           sheet.createRow(0).createCell(0).setCellValue("hello");

            workbook.write(out);
            workbook.close();


        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            Logger.getLogger(JavaApplication4.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    /**
     * @return the dir
     */
    public static String getDir() {
        return dir;
    }

    /**
     * @param dir the dir to set
     */
    public static void setDir(String directory) {
        dir = directory;
    }

}

..And when I run it I get the following error:

Exception in thread "main" org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException: No valid entries or contents found, this is not a valid OOXML (Office Open XML) file
    at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:286)
    at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:758)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:327)
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:291)
    at javaapplication4.JavaApplication4.main(JavaApplication4.java:46)
C:\Users\kamal\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 7 seconds)

I looked up this code in youtube and it's same but i'm not sure why am i getting the error? Can you help me with this?

Upvotes: 3

Views: 24049

Answers (3)

Stunner
Stunner

Reputation: 1151

Okay. Today I encountered the same problem . The server was linux and the excel file is copied from windows to linux through winscp. Winscp has options like transferring the file in binary mode , text mode etc. When we copy the excel file through text mode , I got the same error you mentioned. The error got resolved when I copy the excel file using binary mode. To summarize , this issue came because we copied excel file from windows to linux. Just make sure you are copying in binary mode if using winscp. Make sure the file is copied correctly.

Upvotes: 1

Stephen C
Stephen C

Reputation: 719596

I think the most likely explanations are that either the file is corrupt, or it is an older format spreadsheet file that XSSFWorkbook does not understand.

It is unlikely anyone can give you a definite diagnosis without looking at the file itself.

Upvotes: 1

ketan bharadwaj
ketan bharadwaj

Reputation: 1

I was facing the same problem. I did create the Excel file by doing right click inside the folder and then ->New->Microsoft Excel Worksheet. As a trial I removed this file and then created the new Excel through Start Menu->Microsoft Office->Excel It worked for me, Hopefully same will work for you too.

Upvotes: -1

Related Questions