[英]Write rows in a new excel sheet
I have an excel sheet which looks like that: 我有一个看起来像这样的Excel工作表:
As you can see in the sheet there is more than one value foe
for Name. 正如您在工作表中看到的那样,“名称”有多个价值foe
。
I want to create for every unique Name
element a new excel sheet which looks like that: 我想为每个唯一的 Name
元素创建一个新的Excel工作表,如下所示:
I am using apache poi
and I can read the excel sheet and I saved the distinct values of the colume in an arraylist
. 我正在使用apache poi
,我可以阅读excel工作表,并且将colume的不同值保存在arraylist
。 However, I am struggeling with the algorithm to save each unique name elements row in a new sheet. 但是,我正在努力将算法中的每个唯一名称元素行保存到新表中。
I would appreciate your answer!!! 谢谢您的回答!!!
UPDATE UPDATE
Tried to implement it like that: 试图像这样实现它:
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("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
}
However, it does not work... 但是,它不起作用...
You can read all the rows one by one and create a Map which can be populated where the key
is the firstname
and the value
will be a list of objects
(an object contains the details read from each row in the excel). 您可以一一读取所有行,然后创建一个Map,该Map可以在其中填充,其中key
是firstname
,而value
将是list of objects
(一个对象包含从excel中每一行读取的详细信息)。
Once done, for each entry in the Map, create a new sheet and write the list of objects corresponding to the key in the map in the respective sheet. 完成后,对于Map中的每个条目,创建一个新的工作表,并将与该地图中的键相对应的对象列表写入相应的工作表中。
I don't have everything setup to show an example in POI, but since you're using that, I can show you where, what could be done. 我没有设置所有内容来在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.
}
Once this is done and the map is fully populated, 完成此操作并完全填充地图后,
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
}
I could not elaborate the algorithm more than this. 除此以外,我无法详细说明算法。 It should help you get your solution(you need to handle the coding part). 它应该可以帮助您获得解决方案(您需要处理编码部分)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.