好吧,基本上我試圖在迭代時同時寫入3列和N行。問題是,3列中的2列是用值寫入的...即使它們出現在System.out.println()中,也沒有寫入Excel中。下面的代碼:爲什麼在使用Java編寫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在Excel中寫入X的值。 我得到的是X值和15.9 Y值。我沒有得到15.6的? :(
(從評論) I'm使用這些
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;
你用什麼java包創建你的xls? – smp7d
我正在使用這些 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; – msqar
因爲我正在編輯創建的XLSX文件,這就是爲什麼我需要使用XSSF。 – msqar