为什么用 Java 编写 Excel 时会发生这种情况
好的,基本上我试图在迭代时同时写入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将在
versionsToGraph
的每次迭代中创建同一行,并删除之前添加的内容。你应该
在第一次迭代之后做。
You are creating the same row on every iteration of
versionsToGraph
, you are erasing what was added in previously.You should be doing
after the first iteration.
那么,有多少个
wfCalls
实例?其中有多少versionToGraph
实例等于 15.6 或 15.9?如果由于版本不匹配而导致cant
多次递增,则工作表中的行可能会远远粘贴到 Excel 中的第一个可见行。我会仔细检查您正在写入的行号。So, how many instances of
wfCalls
are there? And how many of those instances ofversionToGraph
are equal to 15.6 or 15.9? Ifcant
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.