-1

在上传excel时我遇到了一些问题。

以下是一段代码。

fileStream = new FileInputStream(dirToStore+filename);

        exReader = new ExcelReader(fileStream);
        workbook = exReader.getWorkBook();
        sheet = getDataSheet(workbook, out);
        List rowList = sheet.getAllRows();
        noRows = rowList.size();

        fileStream = new FileInputStream(dirToStore+filename);

        exReader = new ExcelReader(fileStream);
        workbook = exReader.getWorkBook();
        sheet = getDataSheet(workbook, out);
        List rowList = sheet.getAllRows();
        noRows = rowList.size();

int BATCH_SIZE = 1000;
            for (int i = 1; i < noRows; i++) {
                row = (Row) rowList.get(i);

                cellList = row.getAllCells();

                org_Code = ((Cell) cellList.get(0)).getName();
                prod_Family = ((Cell) cellList.get(1)).getName();
                buss_Unit = ((Cell) cellList.get(2)).getName();
                class_code = ((Cell) cellList.get(3)).getName();
                current_Buyer = ((Cell) cellList.get(4)).getName();
                new_Buyer = ((Cell) cellList.get(5)).getName();
                item = ((Cell) cellList.get(6)).getName();
                item_Revision = ((Cell) cellList.get(7)).getName();
                description = ((Cell) cellList.get(8)).getName();
                parent_Item_Number = ((Cell) cellList.get(9)).getName();
                type = ((Cell) cellList.get(10)).getName();
                purchasable = ((Cell) cellList.get(11)).getName();

                if(((Cell) cellList.get(12)) != null && !"".equals(((Cell) cellList.get(12))))
                global_Part_Flag = ((Cell) cellList.get(12)).getName();
                else
                global_Part_Flag = "";

                if (org_Code.equalsIgnoreCase("") && current_Buyer.equalsIgnoreCase("")&& new_Buyer.equalsIgnoreCase("") && item.equalsIgnoreCase(""))
                    continue;

                System.out.println("org_Code=========> " + org_Code);
                System.out.println("prod_Family=========> " + prod_Family);
                System.out.println("buss_Unit=========> " + buss_Unit);
                System.out.println("class_code=========> " + class_code);
                System.out.println("current_Buyer=========> " + current_Buyer);
                System.out.println("new_Buyer=========> " + new_Buyer);
                System.out.println("item=========> " + item);
                System.out.println("item_Revision=========> " + item_Revision);
                System.out.println("description=========> " + description);
                System.out.println("parent_Item_Number=========> " + parent_Item_Number);
                System.out.println("type=========> " + type);
                System.out.println("purchasable=========> " + purchasable);
                System.out.println("global_Part_Flag=========> " + global_Part_Flag);

                pstmt.setInt(1, groupId);
                pstmt.setInt(2, i);
                pstmt.setString(3, new_Buyer);
                pstmt.setString(4, current_Buyer);
                pstmt.setString(5, org_Code);
                pstmt.setString(6, prod_Family);
                pstmt.setString(7, buss_Unit);
                pstmt.setString(8, class_code);
                pstmt.setString(9, item);
                pstmt.setString(10, item_Revision);
                pstmt.setString(11, description);
                pstmt.setString(12, parent_Item_Number);
                pstmt.setString(13, type);
                pstmt.setString(14, purchasable);
                pstmt.setString(15, global_Part_Flag);
                pstmt.setString(16, userID);
                pstmt.setInt(17, Integer.parseInt(employeeNumber));
                pstmt.setTimestamp(18, sqlDate);
                pstmt.setInt(19, Integer.parseInt(employeeNumber));
                pstmt.setTimestamp(20, sqlDate);

                pstmt.addBatch();

                if (i % BATCH_SIZE == 0){
                    int [] updateCounts = pstmt.executeBatch();
                    pstmt.clearBatch();
                    System.out.println("updateCounts====>" + updateCounts.length);
                    if(updateCounts.length == 0)
                    updateFlag = "N";
                }
            }

            if ((noRows-1) % BATCH_SIZE != 0){
                int [] updateCounts1 = pstmt.executeBatch();
                pstmt.clearBatch();
                System.out.println("updateCounts1====>" + updateCounts1.length);
                if(updateCounts1.length == 0)
                updateFlag = "N";
            //  if(updateCounts1.length ==0)
            //      throw new EMException("Uploaded file has no data. Please modify and upload again !!!.");
            }

                if("N".equals(updateFlag)){
                    throw new EMException("Uploaded file has no data. Please modify and upload again !!!.");    
                }
            /*  System.out.println("Before exe");
                int [] updateCounts = pstmt.executeBatch();
                System.out.println("After exe"); */
                conn1.commit();

            }catch (EMException e) {
                conn1.rollback();
                throw new EMException (e.getMessage());
            }catch (Exception e) {
                conn1.rollback();
                System.out.println("error : " + e.getMessage());
                e.printStackTrace();
                throw new Exception (e.getMessage());
            }finally{
                if(conn1 !=null)
                    conn1.setAutoCommit(true);
                if(pstmt != null) pstmt.close();
                if(conn1 != null) conn1.close();
            }
            System.out.println("before submitRequest");
            submitRequest(groupId, userID,out);
            System.out.println("After submitRequest");
        }

    }catch (EMException e) {

        System.out.println(" Exception while reading the excel : " + e.getMessage());
        out.println("<br> <br><table border=1 align=center>");
        out.println("<tr bgcolor='#77BFC7'>");
        if("Error in ExcelReader".equalsIgnoreCase(e.getMessage()))
            out.println("<td> <font size=3> Error occurred while reading the Excel. Please make sure that upload file is saved in 2003-2007 xls format.</font></td>");
        else
            out.println("<td> <font size=3> " + e.getMessage() +"</font></td>");
        out.println("</tr>");
        out.println("</table><br><br>");
        return;
    }catch (Exception e) {
        System.out.println(" Exception while reading the excel : " + e.getMessage());
        out.println("<br> <br><table border=1 align=center>");
        out.println("<tr bgcolor='#77BFC7'>");
        out.println("<td> <font size=3> An error occurred while processing your request. Check logs for error details. </font></td>");
        out.println("</tr>");
        out.println("</table><br><br>");
        return;
    }

以下DETAILS是代码的输出

noRows=============> 34
groupId==========> 16602
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 74-3163-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 74-3210-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 74-3202-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 74-3783-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 74-3230-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 39-0249-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 74-3204-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 74-3164-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 39-0246-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 39-0219-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 72-4146-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 39-0250-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
prod_Family=========> 
buss_Unit=========> 
class_code=========> 
current_Buyer=========> 
new_Buyer=========> XYZ
item=========> 72-4143-01
item_Revision=========> 
description=========> 
parent_Item_Number=========> 
type=========> 
purchasable=========> 
global_Part_Flag=========> 
org_Code=========> XY
O prod_Family=========> 
O buss_Unit=========> 
O class_code=========> 
O current_Buyer=========> 
O new_Buyer=========> XYZ
O item=========> 74-3232-01
O item_Revision=========> 
O description=========> 
O parent_Item_Number=========> 
O type=========> 
O purchasable=========> 
O global_Part_Flag=========> 
O org_Code=========> XY
O prod_Family=========> 
O buss_Unit=========> 
O class_code=========> 
O current_Buyer=========> 
O new_Buyer=========> XYZ
O item=========> 39-0247-01
O item_Revision=========> 
O description=========> 
O parent_Item_Number=========> 
O type=========> 
O purchasable=========> 
O global_Part_Flag=========> 
error : Index: 7, Size: 7
Exception while reading the excel : Index: 7, Size: 7

在我的 Web 应用程序中上传 excel 时出现上述异常。

请帮我解决上述问题..我不是 java 人:)

4

1 回答 1

0

你写了

cellList = row.getAllCells();

在这段代码之后,您从cellList对象中获取值

org_Code = ((Cell) cellList.get(0)).getName();

但你之后没有放任何支票

 cellList = row.getAllCells();

当此列表只有 5 个对象并且您尝试执行此操作时,这一行将做什么

item = ((Cell) cellList.get(6)).getName();
item_Revision = ((Cell) cellList.get(7)).getName();

你肯定会得到你提到的例外。

于 2013-09-20T10:19:00.823 回答