为什么在用Java编写Excel时会发生这种情况

时间:2011-10-12 18:59:50

标签: java excel apache-poi xls

好的,基本上我在尝试迭代时同时写3列和N行。问题是3列中有2列是用值写的......即使它们出现在System.out.println()中,也没有写在Excel中。这是代码:

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++;
                }               
            }

因此,它应该在代码的这一部分中添加15.6,15.9 Y值,并在最后一个foreach中将X值写入Excel中。 我得到的是X值和15.9 Y值。我没有得到15.6的那些? :(

(来自评论) 我正在使用这些

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.usermodel.XSSFSheet;

2 个答案:

答案 0 :(得分:1)

那么,wfCalls有多少个实例?那些versionToGraph的实例中有多少等于15.6或15.9?如果cant由于版本不匹配而多次递增,则表单中的行可能会粘贴到Excel中的第一个可见行。我会仔细检查你写的行号。

答案 1 :(得分:1)

您正在versionsToGraph的每次迭代中创建相同的行,您正在删除之前添加的内容。

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

你应该这样做

sheet.getRow(27+cant);

在第一次迭代之后。