1

我正在编写此实用程序代码,它将 excel 表转储到数据库中(在本例中为 postgres)。我使用 poi 的 HSSF 技术来处理 excel 表。我将 excel 工作表的每一列存储到单独的字符串类型的 ArrayLists 中。在这种情况下,当 .xls 中的列数大于 23 时,eclipse 会生成错误“堆内存已满”。我的问题是我可以将这些数组列表组合成一个集合对象,我应该使用哪种方法?

public ArrayList<String> getList(String path, String srnoStr,
        String nameStr, String dobStr, String genderStr, String addressStr,
        String pinStr, String mobStr, String eIdStr, String categoryStr,
        String branchStr) throws IOException, SQLException {

    ArrayList<String> errorList = new ArrayList<String>();
    ArrayList<String> cellError = null;

    // String error=null;
    // OrderedMap errorMap=new LinkedMap();
    // errorMap=null;

    List<Cell> cells_srno = new ArrayList<Cell>();
    List<Cell> cells_name = new ArrayList<Cell>();
    List<Cell> cells_dob = new ArrayList<Cell>();
    List<Cell> cells_gender = new ArrayList<Cell>();
    List<Cell> cells_address = new ArrayList<Cell>();
    List<Cell> cells_pin = new ArrayList<Cell>();
    List<Cell> cells_mob = new ArrayList<Cell>();
    List<Cell> cells_eId = new ArrayList<Cell>();
    List<Cell> cells_category = new ArrayList<Cell>();
    List<Cell> cells_branch = new ArrayList<Cell>();

            try {
        int srnoIndex = 0;
        srnoIndex = getIndex(srno, path);
        cellError = fillList(srnoIndex, srNoId, cells_srno, path);
        if (cellError != null)
            errorList.addAll(cellError);

        cellError = null;
        int nameIndex = 0;
        nameIndex = getIndex(name, path);
        cellError = fillList(nameIndex, nameId, cells_name, path);
        if (cellError != null)
            errorList.addAll(cellError);

        cellError = null;
        int dobIndex = 0;
        dobIndex = getIndex(dob, path);
        cellError = fillList(dobIndex, dobId, cells_dob, path);
        if (cellError != null)
            errorList.addAll(cellError);

        cellError = null;
        int genderIndex = 0;
        genderIndex = getIndex(gender, path);
        cellError = fillList(genderIndex, genderId, cells_gender, path);
        if (cellError != null)
            errorList.addAll(cellError);

        cellError = null;
        int addressIndex = 0;
        addressIndex = getIndex(address, path);
        cellError = fillList(addressIndex, addressId, cells_address, path);
        if (cellError != null)
            errorList.addAll(cellError);

        cellError = null;
        int pinIndex = 0;
        pinIndex = getIndex(pin, path);
        cellError = fillList(pinIndex, mobId, cells_pin, path);
        if (cellError != null)
            errorList.addAll(cellError);

        cellError = null;
        int mobIndex = 0;
        mobIndex = getIndex(mob, path);
        cellError = fillList(mobIndex, pinId, cells_mob, path);
        if (cellError != null)
            errorList.addAll(cellError);

        cellError = null;
        int eIdIndex = 0;
        eIdIndex = getIndex(eId, path);
        cellError = fillList(eIdIndex, eIdId, cells_eId, path);
        if (cellError != null)
            errorList.addAll(cellError);

        cellError = null;
        int categoryIndex = 0;
        categoryIndex = getIndex(category, path);
        cellError = fillList(categoryIndex, categoryId, cells_category,
                path);
        if (cellError != null)
            errorList.addAll(cellError);

        cellError = null;
        int branchIndex = 0;
        branchIndex = getIndex(branch, path);
        cellError = fillList(branchIndex, branchId, cells_branch, path);
        if (cellError != null)
            errorList.addAll(cellError);

        // System.out.println(cells);
        int n = cells_srno.size();
        int k = 0;
        System.out.println("Total number Rows = " + (n - 1));
        int j = 0;
        // System.out.println("Column name=" +cells.get(0));
        if (errorList.isEmpty()) {
            for (int i = 1; i < n; i++) {
                k = Insert(cells_srno.get(i), cells_name.get(i),
                        cells_dob.get(i), cells_gender.get(i),
                        cells_address.get(i), cells_pin.get(i),
                        cells_mob.get(i), cells_eId.get(i),
                        cells_category.get(i), cells_branch.get(i));
                if (k > 0)
                    j++;

            }

        } else {
            System.out.println("Error");
        }

        /*
         * System.out
         * .println("Total no. of physical row in you Excel Sheet=" + j);
         * System.out
         * .println("Operation Successfull!! kindly check the database!!! "
         * );
         */

    } catch (Exception e) {
        e.printStackTrace();
    }
    return errorList;
}
4

2 回答 2

1

如果内存不足是问题,我认为您的方法不是很好。您应该通过流式传输来减少内存占用,而不是拥有一个大集合而不是多个集合。这样你只会节省很少的钱,一旦你的输入数据变得更大一点,你就会再次遇到同样的问题。

即一次处理(插入)一行,而不是预先“填充”列表。如果使用健全的库来读取也进行流式传输的输入 excel 表,那么只要它适合磁盘,这应该可以实际处理大量数据。

于 2013-12-25T17:24:01.023 回答
0

我会使用 aMap和 aenum作为键并像这样重构:

public List<String> getList(final String path, final Map<CellType, String> data) {
    final Map<CellType, List<Cell>> cells = new EnumMap<CellType, List<Cell>>();
    for (final CellType cellType : CellType.values()) {
        cells.put(cellType, new ArrayList<Cell>());
    }

    final List<String> errorList = new ArrayList<String>();
    try {
        for (final CellType cellType : data.keySet()) {
            final int index = getIndex(data.get(cellType), path);
            final List<String> cellError = fillList(index, /* the ID for cellType */, cells.get(cellType), path);
            if (cellError != null) {
                errorList.addAll(cellError);
            }
        }

        // ... (use the map)
    } catch (final Exception e) {
        e.printStackTrace();
    }

    return errorList;
}

public static enum CellType {
    SRNO, NAME, DOB // ...
}

(未测试)

注意:srNoIdnameId未在您发布的代码中定义,所以我不知道它们是什么

于 2013-12-25T17:08:55.683 回答