"The package must contain part of the content type [M1.13]"

I try to write to an Excel file, but I get an error:

An exception in the stream "main" org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: The package must contain part of the content type [M1.13]

From what I understand, I miss a flask.

Can someone help me determine which file is this?

PS 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);

    }      

}
+7
source share
11 answers

Use file extension to process WorkSheettype

  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);
+9
source

, XLS/XLSX LibreOffice. -, - , , Microsoft Office. , , LibreOffice Calc, MS Excel, .

+5

Excel , . : .

Hitesh Sahu:

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");
}
+2

CHECK EXCEL FORMAT.... -, excel poi, , , . , : ( , , excel

0

. . . .

0

. excel, , ~ $______. Xlsx .

0

Workbook workbook = WorkbookFactory.create(source)

WorkbookFactory Apache POI, ( ). Workbook (XSSFWorkbook HSSFWorkbook). source java.io.InputStream java.io.File.

0

Excel, , - - . , (). , . .

0

, , , line3 :

File xlsxFile = new File( "C:\\myWorkbook.xlsx" );
FileInputStream finXLSX = new FileInputStream( xlsxFile ); //line1

FileOutputStream foutXLSX = new FileOutputStream( xlsxFile ); //line2

XSSFWorkbook workSheet = new XSSFWorkbook( finXLSX ); //line3

But I realized that line3 does not work, since I also opened the output stream for my xlsx file via line2, and then the execution of line3 was unsuccessful. I removed line2 to make it work.

0
source

<<<< Use Apache POI 3.5.

                                                     >>>>
0
source

My Excel worksheet has been encrypted. I deleted the password and it worked.

0
source

Source: https://habr.com/ru/post/1609736/


All Articles