开发者

Why is this happening while writing an Excel in Java

开发者 https://www.devze.com 2023-04-12 09:33 出处:网络
Ok, basically i\'m trying to write 3 columns and N number of rows simultaneously while iterating. The problem is that 2 of the 3 columns are written with the values... even though they appear in the S

Ok, basically i'm trying to write 3 columns and N number of rows simultaneously while iterating. The problem is that 2 of the 3 columns are written with the values... even though they appear in the System.out.println(), are not being written in the Excel. Here's the code:

for(int i = 0; i < versionsToGraph.length; i++) {           
                    List<WsCallEntry> wfCalls = allCalls.get(i);
                    int cant = -1;
                    if(!wfCalls.isEmpty()) {                            
                        for (WsCallEntry wsCallEntry : wfCalls) {   
                            String x = wsCallEntry.getKey();
                            int y = wsCallEntry.getValue();
                            cant++;
                            if(versionsToGraph[i].equals("15.6")) {
                                System.out.println(x + " " + y + " will be added.");
                                XSSFSheet sheet = (XSSFSheet) wb.getSheetAt(0);     
                                XSSFRow row = sheet.createRow(27+cant);                                 

                                XSSFCell celly = row.createCell(10); 

                                celly.setCellValue(y);
                            }else{
                                if(versionsToGraph[i].equals("15.9")) {

                                    System.out.println(x + " " + y + " will be added.");
                                    XSSFSheet sheet = (XSSFSheet) wb.getSheetAt(0);     
                                    XSSFRow row = sheet.createRow(27+cant);                                 

                                    XSSFCell celly = row.createCell(11); 

                                    celly.setCellValue(y);
                                }
                            }                       


                            xValues.add(x); 


                        }                           


                        Collections.sort(xValues, new StringComparator());

                }                           
                }                       
                ArrayList<String> newList = eliminarRepetidos(xValues); 
                int cant = 0;
                for (String newlist : newList) {
                    String x = newlist;
                    XSSFSheet sheet = (XSSFSheet) wb.getSheetAt(0);         
                    XSSFRow row = sheet.createRow(27+cant);                                 
                    XSSFCell cellx = row.createCell(9); 
                    cellx.setCellValue(x);
                    cant++;
                }               
            }

So, it should add in this part of the code, the 15.6, 15.9 Y values, and in the last foreach writes the X values in the Excel. What i'm getting is X values and 15.9 Y values. What am I not getting the 15.6 ones? :(

(from the comment) I´m using these

import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.ss.usermodel.WorkbookFactory; 
import org.apache.poi.xssf.usermodel.XSSFCell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermode开发者_高级运维l.XSSFSheet;


You are creating the same row on every iteration of versionsToGraph, you are erasing what was added in previously.

XSSFRow row = sheet.createRow(27+cant);

You should be doing

sheet.getRow(27+cant);

after the first iteration.


So, how many instances of wfCalls are there? And how many of those instances of versionToGraph are equal to 15.6 or 15.9? If cant is being incremented many times because the versions don't match, your rows in the sheet will probably be far pasted the first visible rows in Excel. I would double check the row numbers you're writing to.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号