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

发布于 2024-12-09 15:44:29 字数 2939 浏览 0 评论 0原文

好的,基本上我试图在迭代时同时写入 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;

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清眉祭 2024-12-16 15:44:29

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

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

你应该

sheet.getRow(27+cant);

在第一次迭代之后做。

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.

回梦 2024-12-16 15:44:29

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文