尝试并捕获NotOLE2FileException

时间:2017-05-30 09:53:57

标签: java excel apache apache-poi

我编写了这段代码来确定使用的文件是否选择了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");
  }
}

修改

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

0 个答案:

没有答案