0

我有一个看起来像这样的 excel 表:

在此处输入图像描述

正如您在工作表中看到的,Name 有多个值foe

我想为每个独特的 Name元素创建一个新的 excel 表,如下所示:

在此处输入图像描述

在此处输入图像描述

我正在使用apache poi,我可以阅读 excel 表,并将列的不同值保存在arraylist. 但是,我正在努力使用将每个唯一名称元素行保存在新工作表中的算法。

我会很感激你的回答!!!

更新

试图这样实现它:

for (Row r : sheet) {
            Cell c = r.getCell(4);
            c.setCellType(Cell.CELL_TYPE_STRING);
//          System.out.println(c.getStringCellValue().toString());
            if (c.getStringCellValue().equals(nameList.contains(c.getRowIndex()))) {
                System.out.println(sheet.getRow(r.getRowNum()));
                System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

            }
        }

但是,它不起作用...

4

1 回答 1

1

您可以一一读取所有行并创建一个 Map ,该 Map 可以填充在keyfirstnamevalue将是 a 的位置list of objects(一个对象包含从 excel 中的每一行读取的详细信息)。

完成后,为地图中的每个条目创建一个新工作表,并将与地图中的键对应的对象列表写入相应的工作表中。

我没有在 POI 中显示示例的所有设置,但是由于您正在使用它,我可以告诉您在哪里,可以做什么。

class SomeObject { // This class contains all the details a row in an excel can contain}

Map<String, List<SomeObject>> myMap = new HashMap<String, List<SomeObject>>(); // Create your hashmap

For every row read from the excel using POI {
    1. Create a new SomeObject with the data read from the row
        a. if the firstname value already exists in myMap, then add this new object to the already existing list for that key
        b. if not present, create a new arraylist, add this element and put a new entry in map with firstname and list
    2. continue this, till there are more rows to be read.
}

完成此操作并且地图已完全填充后,

For every Entry in the map {
    1. Create a new sheet
    2.  Write all the objects present as the value for this Entry, to the sheet.
    3. Close the sheet
    4. Continue till there are more entries in the map
}

我无法比这更详细地阐述算法。它应该可以帮助您获得解决方案(您需要处理编码部分)。

于 2013-10-14T11:11:17.760 回答