从excel文件获取数据时出现java.lang.IllegalArgumentException

时间:2012-11-07 05:21:15

标签: java struts apache-poi

我从包含超过3000行的Excel工作表中获取数据。但我得到一个像java.lang.IllegalArgumentException这样的异常:提供的POIFSFileSystem既不包含'Workbook'条目,也不包含'WORKBOOK'条目。它真的是一个excel文件吗?

以下是我的代码 -

public ActionForward exportExtraExcel(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.out.println("inside exportExtraExcel----------> ");

//      String fileName=request.getParameter("filePath");
//      System.out.println("fileName----qqqqqqqqqqqqqq---->"+fileName);


        String fileName = "C:\\Users\\apanigrahi\\Desktop\\tt.xls";

        Statement st = null;
        ResultSet rs = null;
        EmployeeDTO employeeDTO = new EmployeeDTO();
        List cellDataList = new ArrayList();
        try {
            FileInputStream fileInputStream = new FileInputStream(fileName);

            POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);

            HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
            HSSFSheet hssfSheet = workBook.getSheetAt(0);

            int rows = hssfSheet.getLastRowNum() + 1;
            int col = -1;
            for (int i = 0; i < rows; i++) {
                HSSFRow hssfRow = hssfSheet.getRow(i);
                if (i == 0)
                    col = hssfRow.getLastCellNum();
                List cellTempList = new ArrayList();
                for (int j = 0; j < col; j++) {
                    HSSFCell hssfCell = hssfRow.getCell((short) j);
                    cellTempList.add(hssfCell);
                }
                cellDataList.add(cellTempList);
            }

        } catch (Exception e) 
        {
            e.printStackTrace();
        }

        List<Integer> empIdArr = new ArrayList<Integer>();
        List<Integer> userIdArr = new ArrayList<Integer>();
        List<String> empFnameArr = new ArrayList<String>();
        List<String> empLnameArr = new ArrayList<String>();
        List<String> employeeIdArr = new ArrayList<String>();
        List<String> date1 = new ArrayList<String>();

        ArrayList<ArrayList> biometric_Data = getBiometricData(cellDataList);

        ArrayList<String> date_biometric_Data = biometric_Data.get(0);
        ArrayList<String> emp_code_biometric_Data = biometric_Data.get(1);
        ArrayList<String> working_hours_biometric_Data = biometric_Data.get(2);
        ArrayList<String> date_biometric_Data1 = new ArrayList<String>();
        ArrayList<String> emp_code_biometric_Data1 =new ArrayList<String>();
        ArrayList<String> working_hours_biometric_Data1 = new ArrayList<String>();
        List<EmployeeDTO> extra_empLeaveSummaryReport = new ArrayList<EmployeeDTO>();


        try {
            connMgr = InitServlet.connMgr;
            conn = connMgr.getConnection("access");
            st = conn.createStatement();

            for (int j = 0; j < emp_code_biometric_Data.size(); j++)
            {   
                    String qry = "select emp_id,user_id,emp_first_name,emp_last_name,employee_id from \"Employee\" where employee_id='"
                    + emp_code_biometric_Data.get(j)
                    + "' and   is_deleted is null and emp_current_country ='1' order by employee_id";

                rs = st.executeQuery(qry);


                if(rs!=null){
                while (rs.next()) {
                    int empId = rs.getInt(1);
                    empIdArr.add(empId);                    
                    userIdArr.add(rs.getInt(2));                    
                    empFnameArr.add(rs.getString(3));
                    empLnameArr.add(rs.getString(4));
                    employeeIdArr.add(rs.getString(5));             
                    date_biometric_Data1.add(date_biometric_Data.get(j));                   
                    emp_code_biometric_Data1.add(emp_code_biometric_Data.get(j));               
                    working_hours_biometric_Data1.add(working_hours_biometric_Data.get(j));                 
                }
                }
                connMgr.freeConnection("access", conn);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception in exportExcelLeaveSummary");
        } finally {
            rs.close();
            st.close();
            connMgr.freeConnection("access", conn);
        }
        for (int i = 0; i < employeeIdArr.size(); i++) 
        {
            double hours_worked = Double.parseDouble(working_hours_biometric_Data.get(i));

            if (hours_worked < 9.00 && hours_worked > 0.0)
            {
                String day_status = "Halfday";
                try {       
                    System.out.println("Inside if loop Halfday");
                    UserManager userManager = new UserManagerImpl();                        
                    employeeDTO = userManager.getEmployeeLeaveDetails(userIdArr.get(i));                    
                    employeeDTO.setDate(date_biometric_Data1.get(i));
                    employeeDTO.setEmployeeId(employeeIdArr.get(i));
                    employeeDTO.setFirstName(empFnameArr.get(i));
                    employeeDTO.setLastName(empLnameArr.get(i));
                    employeeDTO.setWorking_hours(working_hours_biometric_Data1.get(i));
                    int totalLeaves = employeeDTO.getTotalLeaves();
                    employeeDTO.setTotalLeaves(totalLeaves);
                    int plToCredit = employeeDTO.getPlToCredit();
                    employeeDTO.setPlToCredit(plToCredit);

                    int slclToCredit = employeeDTO.getSlclToCredit();
                    employeeDTO.setSlclToCredit(slclToCredit);
                    int slclTotalBal = totalLeaves - plToCredit;
                    employeeDTO.setSlclTotalBal(slclTotalBal);
                    connMgr = InitServlet.connMgr;
                    conn = connMgr.getConnection("access");
                    st = conn.createStatement();

                    String qry = "";

                    qry = "Select * from \"Est_Employee_Leave_Application\"  Where leave_approval_status='Approved' and '"+ ConvertDate.stringtoSQLDate(date_biometric_Data
                                    .get(i))
                            + "'  between leave_application_from_date and leave_application_to_date And emp_id ='"
                            + empIdArr.get(i) + "'";
                    rs = st.executeQuery(qry);
                    if (!(rs.equals(null))) {
                        employeeDTO.setDay_status(day_status);

                        extra_empLeaveSummaryReport.add(employeeDTO);
                        request.setAttribute("leaveSummary",extra_empLeaveSummaryReport);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    System.out
                            .println("Exception in leaveDetailsReport method");
                } finally {
                    rs.close();
                    st.close();
                    connMgr.freeConnection("access", conn);
                }
            }


             if (hours_worked == 0.0) {
                String day_status = "LOP";
                try {                   
                    System.out.println("Inside if loop LOP");
                    UserManager userManager = new UserManagerImpl();                        
                    employeeDTO = userManager.getEmployeeLeaveDetails(userIdArr.get(i));                    
                    employeeDTO.setDate(date_biometric_Data1.get(i));
                    employeeDTO.setEmployeeId(employeeIdArr.get(i));
                    employeeDTO.setFirstName(empFnameArr.get(i));
                    employeeDTO.setLastName(empLnameArr.get(i));
                    employeeDTO.setWorking_hours(working_hours_biometric_Data1.get(i));
                    int totalLeaves = employeeDTO.getTotalLeaves();
                    employeeDTO.setTotalLeaves(totalLeaves);
                    int plToCredit = employeeDTO.getPlToCredit();
                    employeeDTO.setPlToCredit(plToCredit);

                    int slclToCredit = employeeDTO.getSlclToCredit();
                    employeeDTO.setSlclToCredit(slclToCredit);
                    int slclTotalBal = totalLeaves - plToCredit;
                    employeeDTO.setSlclTotalBal(slclTotalBal);
                    connMgr = InitServlet.connMgr;
                    conn = connMgr.getConnection("access");
                    st = conn.createStatement();

                    String qry = "";

                    qry = "Select * from \"Est_Employee_Leave_Application\"  Where '"
                            + ConvertDate.stringtoSQLDate(date_biometric_Data
                                    .get(i))
                            + "'  between leave_application_from_date and leave_application_to_date And emp_id ='"
                            + empIdArr.get(i) + "'";
                    rs = st.executeQuery(qry);
                    if (!(rs.equals(null))) {
                        employeeDTO.setDay_status(day_status);

                        extra_empLeaveSummaryReport.add(employeeDTO);
                        request.setAttribute("leaveSummary",
                                extra_empLeaveSummaryReport);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    System.out
                            .println("Exception in leaveDetailsReport method");
                } finally {
                    rs.close();
                    st.close();
                    connMgr.freeConnection("access", conn);
                }
            }

        }
        List<String> columnName = new ArrayList<String>();
        columnName.add("Date");
        columnName.add("Emp_Id");
        columnName.add("Emp_First_Name");
        columnName.add("Emp_Last_Name");
        columnName.add("SL/CL_TotalBal");
        columnName.add("PL_ToCredit");
        columnName.add("Total_Leaves");
        columnName.add("SL/CL_ToCredit");
        columnName.add("Working_Hours");
        columnName.add("Day_status");

        request.setAttribute("columnNames", columnName);
        return mapping.findForward("EXTRA_DETAILED_REPORT");
    }

    @SuppressWarnings("rawtypes")
    public ArrayList<ArrayList> getBiometricData(List cellDataList)
             {
        EmployeeDTO employeeDTO = new EmployeeDTO();
        ArrayList<String> date_Biometric = new ArrayList<String>();
        ArrayList<String> emp_code_Biometric = new ArrayList<String>();
        ArrayList<String> working_hours_Biometric = new ArrayList<String>();

        ArrayList<ArrayList> biometric_All = new ArrayList<ArrayList>();
        for (int i = 2; i < cellDataList.size(); i++) 
        {       
            List cellTempList = (List) cellDataList.get(i);

            for (int j = 0; j < cellTempList.size(); j++) 
            {
                if (j == 0) 
                {
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(0);
                    Date d1 = hssfCell.getDateCellValue();
                    SimpleDateFormat sdfAct = new SimpleDateFormat("dd-MMM-YYYY");

                    String d2 = new SimpleDateFormat("EEEE").format(d1);
                    String week_day1 = "Sunday";
                    String week_day2 = "Saturday";

                if (week_day1.equals(d2) || week_day2.equals(d2)) {

                        j = 15;

                    }

                else {
                        employeeDTO.setDate(sdfAct.format(d1).toString());

                }

                }
                if (j == 1) {
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(1);
                    // blank = hssfCell.toString();
                    // if(blank == "")
                    // System.out.println("N/A");
                    // //else
                    // //System.out.println(blank);
                }
                if (j == 2) {
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(2);
                    String emp_code1 = hssfCell.toString();
                    String temp = emp_code1;
                    String non_emp1="10003";
                    String non_emp2="1001";
                    String non_emp3="1002";
                    String non_emp4="1111";
                    String non_emp5="237";
                    String non_emp6="002";
                    String non_emp7="023";
                    String non_emp8="511";
                    if(temp.length()>3){
                        temp = ""+ Integer.parseInt(temp);
                    }

                     if(temp.equals(non_emp1)||temp.equals(non_emp2)||temp.equals(non_emp3)||temp.equals(non_emp4)||temp.equals(non_emp5)||temp.equals(non_emp6)||temp.equals(non_emp7)||temp.equals(non_emp8))
                    {                        
                    j=15;

                    }
                     else{

                    String emp_code = "EST" +temp;                  
                    employeeDTO.setEmp_code(emp_code);
                     System.out.println(emp_code);
                     }                  
                }
                if (j == 3) {
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(3);
                    String emp_name = hssfCell.toString();
                    employeeDTO.setName(emp_name);

                }
                if (j == 4) {
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(4);

                    // System.out.println(card_num);
                }
                if (j == 5) {
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(5);
                    String shift_start = hssfCell.toString();

                }
                if (j == 6) {
                    String emp_in = "";
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(6);
                    try {
                        emp_in = hssfCell.toString();

                    } catch (Exception e) {
                        emp_in = "";
                    }
                    if (emp_in == "" || emp_in.isEmpty() || emp_in == null) {
                    } else {
                    }

                }
                if (j == 7) {
                    String emp_out = "";
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(7);
                    try {
                        emp_out = hssfCell.toString();
                    } catch (Exception e) {
                    }
                    if (emp_out == "" || emp_out.isEmpty() || emp_out == null) {
                    } else {
                    }
                }
                if (j == 8) {
                    String shift_end = "";
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(8);

                }
                if (j == 9) {
                    String status = "";
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(9);
                    status = hssfCell.toString();               
                    String holiday_status="Hol";

                    if(status.equals(holiday_status))
                    {

                        j=15;
                    }

                }
                if (j == 10) {
                    String emp_late = "";
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(10);
                    try {
                        emp_late = hssfCell.toString();

                    } catch (Exception e) {
                        emp_late = "";
                    }
                    if (emp_late == "" || emp_late.isEmpty()
                            || emp_late == null) {
                    } else {
                    }
                }
                if (j == 11) {
                    String emp_early = "";
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(11);
                    try {
                        emp_early = hssfCell.toString();

                    } catch (Exception e) {
                        emp_early = "";
                    }
                    if (emp_early == "" || emp_early.isEmpty()
                            || emp_early == null) {

                    } else {

                    }
                }
                if (j == 12) {
                    String hours_worked = "";
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(12);
                    try {
                        hours_worked = hssfCell.toString();                     
                        employeeDTO.setHours_worked(hours_worked);
                    } catch (Exception e) {
                        hours_worked = "";
                    }
                    if (hours_worked == "" || hours_worked.isEmpty()|| hours_worked == null) 
                    {
                        hours_worked = "0.00";
                        employeeDTO.setHours_worked(hours_worked);                  
                    }

                    date_Biometric.add(employeeDTO.getDate());          
                    emp_code_Biometric.add(employeeDTO.getEmp_code());              
                    working_hours_Biometric.add(employeeDTO.getHours_worked());
                }

                if (j == 13) {
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(13);                    
                }
                if (j == 14) {
                    HSSFCell hssfCell = (HSSFCell) cellTempList.get(14);                    
                }

            }

        }       
        biometric_All.add(date_Biometric);      
        biometric_All.add(emp_code_Biometric);    
        biometric_All.add(working_hours_Biometric); 
        return biometric_All;

    }

.................. 在上面的代码中,我正在考虑上面提到的异常 HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);

0 个答案:

没有答案