Java 尝试捕获NotOLE2FileException

Java 尝试捕获NotOLE2FileException,java,excel,apache,apache-poi,Java,Excel,Apache,Apache Poi,我编写此代码是为了确定用户选择的文件是否为excel文件。它确实工作,但在输出中总是抛出异常。尝试捕获此异常的正确方法是什么 我的代码: public class importIntake { public importIntake() throws FileNotFoundException, IOException{ JButton open = new JButton(); JFileChooser fc = new JFileChooser(); f

我编写此代码是为了确定用户选择的文件是否为excel文件。它确实工作,但在输出中总是抛出异常。尝试捕获此异常的正确方法是什么

我的代码:

  public class importIntake {
   public importIntake() throws FileNotFoundException, IOException{


    JButton open = new JButton();
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new java.io.File("/Users/frank/Desktop/"));
    fc.setDialogTitle("Choose");
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

    if(fc.showOpenDialog(open) == JFileChooser.APPROVE_OPTION){
    }
    FileInputStream fis = new FileInputStream(new File(fc.getSelectedFile().toString()));

    String filename = fc.getSelectedFile().toString();
    if(!FilenameUtils.isExtension(filename,"xls")){
      JOptionPane.showMessageDialog(null, "Choose an excel file!");
    }


    HSSFWorkbook wb = new HSSFWorkbook(fis);
    //een sheet object
    HSSFSheet sheet = wb.getSheetAt(0);

    FormulaEvaluator forlulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();

    intakestatus IntakeStatuk = new intakestatus();

    Iterator<Row> rowIterator = sheet.iterator();

    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();

        String intaker = row.getCell(17).getStringCellValue();
        String intakeDatum = row.getCell(18).getStringCellValue();
        String start = row.getCell(19).getStringCellValue();
        String herintake = row.getCell(20).getStringCellValue();
        String stopzetting = row.getCell(21).getStringCellValue();
        String verwijzersdoor = row.getCell(22).getStringCellValue();
        String vdcontactpersoon = row.getCell(23).getStringCellValue();
        String vncontactpersoon = row.getCell(26).getStringCellValue();
        String vnemail = row.getCell(28).getStringCellValue();

        String[] intakers = intaker.split(" ");
        String[] intakedatum = intakeDatum.split(" ");
        String[] startDatum = start.split(" ");
        String[] herIntake = herintake.split(" ");
        String[] stopZetting = stopzetting.split(" ");
        String[] verwijzersDoor = verwijzersdoor.split(" ");
        String[] vdContactpersoon = vdcontactpersoon.split(" ");
        String[] vnContactpersoon = vncontactpersoon.split(" ");
        String[] vnEmail = vnemail.split(" ");

        for(int i = 0; i < intakers.length; i++){

            String intakerSplit = intakers[i];
            String intakeDSplit = intakedatum[i];
            String startDatumF = startDatum[i];
            String herIntakeF = herIntake[i];
            String stopZettingF = stopZetting[i];
            String verwijzersDoorF = verwijzersDoor[i];
            String vdContactpersoonF = vdContactpersoon[i];
            String vnContactpersoonF = vnContactpersoon[i];
            String vnEmailF = vnEmail[i];

            IntakeStatuk.setData(intakerSplit,intakeDSplit, startDatumF, herIntakeF, stopZettingF, verwijzersDoorF, 
                    vdContactpersoonF, vnContactpersoonF, vnEmailF);

            boolean result = intakestatusData.insert(IntakeStatuk);

            if(result){
                System.out.println("Work");
            }else{
                System.err.println("Something wrong");
            }
      }

    }
    JOptionPane.showMessageDialog(null, "Works");
  }
}
公共类导入Take{
public importIntake()抛出FileNotFoundException,IOException{
JButton open=新JButton();
JFileChooser fc=新的JFileChooser();
fc.setCurrentDirectory(新的java.io.File(“/Users/frank/Desktop/”);
fc.setDialogTitle(“选择”);
fc.setFileSelectionMode(仅限于JFileChooser.FILES_);
如果(fc.showOpenDialog(打开)=JFileChooser.APPROVE\u选项){
}
FileInputStream fis=新的FileInputStream(新文件(fc.getSelectedFile().toString());
字符串文件名=fc.getSelectedFile().toString();
如果(!FilenameUtils.isExtension(文件名,“xls”)){
showMessageDialog(null,“选择excel文件!”);
}
HSSF工作手册wb=新的HSSF工作手册(fis);
//een图纸对象
HSSFSheet sheet=wb.getSheetAt(0);
FormulaEvaluator forlulaEvaluator=wb.getCreationHelper().createFormulaEvaluator();
intakestatus IntakeStatuk=新intakestatus();
迭代器rowIterator=sheet.Iterator();
while(roweiterator.hasNext()){
行=行迭代器。下一步();
String intaker=row.getCell(17.getStringCellValue();
字符串intakDatum=row.getCell(18.getStringCellValue();
String start=row.getCell(19.getStringCellValue();
String=row.getCell(20.getStringCellValue();
String stopzetting=row.getCell(21.getStringCellValue();
字符串verwijzersdoor=row.getCell(22.getStringCellValue();
字符串vdcontactpersoon=row.getCell(23.getStringCellValue();
字符串vncontactpersoon=row.getCell(26.getStringCellValue();
字符串vnemail=row.getCell(28.getStringCellValue();
字符串[]intakers=intaker.split(“”);
字符串[]intakedatum=intakedatum.split(“”);
字符串[]startDatum=start.split(“”);
字符串[]herIntake=herIntake.split(“”);
字符串[]stopZetting=stopZetting.split(“”);
字符串[]verwijzersDoor=verwijzersDoor.split(“”);
字符串[]vdContactpersoon=vdContactpersoon.split(“”);
字符串[]vnContactpersoon=vnContactpersoon.split(“”);
字符串[]vnEmail=vnEmail.split(“”);
对于(int i=0;i
编辑


引发的异常是:org.apache.poi.poifs.filesystem.NotOLE2FileException:头签名无效;读取0x302E312D46445025,应为0xE11AB1A1E011CFD0-您的文件似乎不是有效的OLE2文档。我还用我使用的apache代码更新了代码。

引发了什么异常?它在哪里抛出(在代码的哪一行)?如果抛出异常会发生什么?这不是完整的代码
NotOLE2FileException
是Apache POI异常,但没有可见的POI代码。此外:异常消息是什么?我已更新了我的问题如果您在十六进制编辑器中键入0x302E312D46445025 reverse,您将得到“%PDF-1.0”。。。我会使用WorkbookFactory.create(InputStream)并捕获InvalidFormatException,也就是说,将代码从HSSF重定到SS公共接口,因此,如果处理旧的OLE2或更新的基于OOXML的文件,您不需要太在意