简体   繁体   English

在新的Excel工作表中写行

[英]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可以在其中填充,其中keyfirstname ,而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.

相关问题 如何在java中的excel文件的同一表中插入值的新行 - how to insert new rows with values in the same sheet of an excel file in java 如何在现有Excel工作表的2行之间插入新行 - How to insert new row in between 2 rows in existing excel sheet 没有在excel表中写 - didnot write in excel sheet 如何在每个循环实例的新行中将数据写入excel工作表而不替换以前的数据,即如何为每个循环实例追加行? - how to write data into excel sheet in new row for every loop instance without replacing previous data i.e how to append rows for every loop instance? 将数据写入打开的Excel工作表 - Write data to a Open excel sheet 将数据写入Excel工作表Java - Write data into Excel sheet java 如何为不同行的不同列号的 Excel 工作表编写 TestNG DataProvider 注释 - How to write TestNG DataProvider annotation for Excel sheet with different column numbers for different rows 在Excel工作表的列末尾写入Excel数据 - Write excel data in end of column in the Excel Sheet Apache POI 读取包含多个工作表的 excel 文件并将单个工作表写入新文件 - Apache POI to read excel file with many worksheets and write single sheet to new file 从 excel 表读取数据并写入同一 excel 表 - Read data from excel sheet and write in the same excel sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM