java – “包应包含内容类型部分[M1.13]”

我试图写入Excel文件,但我一直收到错误

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

根据我的理解,我错过了一个jar文件.

任何人都可以帮我识别它是哪个文件

附:我正在使用Netbeans.

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

    }      

}

解决方法

使用文件扩展名来处理WorkSheet类型
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\"";
                    }

对于XLSX文件:使用XSSFWorkbook& XSSFSheet

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

    XSSFSheet sheet = workbook.getSheetAt(0);

对于XLS文件:使用hssfWorkbook& hssfSheet

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

    hssfSheet sheet = workbook.getSheetAt(0);

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...