gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:58:24 25 4
gpt4 key购买 nike

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

最佳答案

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

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

你应该这样做

sheet.getRow(27+cant);

第一次迭代后。

关于java - 为什么用 Java 编写 Excel 时会发生这种情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7745105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com