Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Package should contain a content type part [M1.13]"

I am attempting to write to an Excel file however I keep getting the error:

Exception in thread "main" org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]

From what I understand I am missing a jar file.

Can anyone help me identify which file it is?

P.S. I am using Netbeans.

my current files

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JOptionPane;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author nicholaskissaun
 */

public class Tester {

    public static void main (String args \[\])throws FileNotFoundException, IOException, InvalidFormatException{     
        int RowCount = 7, iChoice;
        String sChoice;
        XSSFSheet s;
        XSSFRow row1;
        XSSFWorkbook wb;
        XSSFCell r1c1, r1c2, r1c8, r1Episodes;

        FileInputStream fis = new FileInputStream("/Users/nicholaskissaun/Google Drive/Grade 11_12/Computer Science/Java/Term1/src/IA/Profiles/Becky/ShowDetails.xlsx");           
        wb = new XSSFWorkbook(fis);  
        s = wb.getSheetAt(0);

    }      

}
like image 411
Tephrite Avatar asked Oct 01 '15 02:10

Tephrite


4 Answers

Use file extension to handle WorkSheet Type

  String inputFilename = new File(path).getName();

                    switch (inputFilename.substring(inputFilename.lastIndexOf(".") + 1,
                            inputFilename.length())) {
                        case "xls":
                            return readXLS(path);

                        case "xlsx":
                            return readXLSX(path);
                        default:
                            Log.e(TAG, "No XLS file chosen");
                            return "Please select valid \"Excel\" File\"";
                    }

For XLSX file: use XSSFWorkbook & XSSFSheet

    XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(path)));

    XSSFSheet sheet = workbook.getSheetAt(0);

For XLS file: use HSSFWorkbook & HSSFSheet

    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File(path)));

    HSSFSheet sheet = workbook.getSheetAt(0);
like image 135
Hitesh Sahu Avatar answered Oct 28 '22 17:10

Hitesh Sahu


I have the same problem. When you open excel file it will generate some file like ~$______.xlsx Just find and delete all of them worked for me.

like image 40
Long Nguyen Avatar answered Oct 28 '22 17:10

Long Nguyen


This may happen when your create your XLS/XLSX file through LibreOffice. Apparently something is lost in the conversion and the file is not the same as a spreadsheet made in Microsoft Office. I had the same error and the solution for me was copying all the work I have done in LibreOffice Calc to a MS Excel spreadsheet and then save a new file.

like image 6
Gabriel Ullmann Avatar answered Oct 28 '22 17:10

Gabriel Ullmann


What you have is version mismatch between your Excel file and workbook you are trying to create. The best way to avoid is: choose Interface implementation.

I built on top of Hitesh Sahu's solution:

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

Workbook workbook = null;

// parse files from request
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartDataPointsFile = multipartRequest.getFile("yourFileHere");

try {
    if(multipartDataPointsFile!=null) {
        String originalFileName= multipartDataPointsFile.getOriginalFilename();
        if(originalFileName!=null && originalFileName.length()>0) {
            switch (originalFileName.substring(originalFileName.lastIndexOf(".") + 1,
                    originalFileName.length())) {
                case "xls":
                    try {
                        workbook = WorkbookFactory.create(multipartDataPointsFile.getInputStream());
                    }catch(org.apache.poi.openxml4j.exceptions.InvalidFormatException ie){
                        logger.error("Malformed Excel");
                        throw new IOException();
                    }
                    if(workbook!=null) {
                        // Do something in here
                    }else{
                        logger.error("Could not pass along the workbook");
                        throw new IOException();
                    }
                case "xlsx":
                    try {
                        workbook = WorkbookFactory.create(multipartDataPointsFile.getInputStream());
                    }catch(org.apache.poi.openxml4j.exceptions.InvalidFormatException ie){
                        logger.error("Malformed Excel");
                        throw new IOException();
                    }
                    if(workbook!=null) {
                          // Do something in here
                    }else{
                        logger.error("Could not pass along the workbook");
                        throw new IOException();
                    }
                default:
                    logger.error("File type is not  recognized  Excell type");
                    throw new IOException();
            }

        }else{
            logger.error("Can Not Read File Name");
            throw new IOException();  
        }
    }else{
        logger.error("Did not select a file");
        throw new IOException();
    }
} catch (IOException e) {
    throw new ApplicationErrorException("Can't parse  Excel file");
}
like image 3
mike oganyan Avatar answered Oct 28 '22 15:10

mike oganyan