0
以下の方法を使用すると、Excelシートにarraylistが印刷されます。このメソッドをループで呼び出すと、2番目の繰り返しが同じシートに印刷されます。私は次の繰り返しを別のシートに印刷したい、おそらく新しいシートを作成してそこに書きたいと思う。同じExcelワークブックの別のシートで反復を印刷するためにはどのような変更が必要ですか。XSSF Apache POI
for (int indexSelect=1;indexSelect>=10; indexSelect++){
excelWrite(Prints, indexSelect)
}
public static void excelWrite(ArrayList<Object> Prints, int indexSelect) {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Achievers"+indexSelect);
System.out.println("Getting sheet at ..:" +indexSelect);
//Sheet sheet = workbook.getSheetAt(indexSelect);
int rowCount = 0;
// for (Object Names : Prints) {
// org.apache.poi.ss.usermodel.Row row = sheet.createRow(++rowCount);
//org.apache.poi.ss.usermodel.Row row = sheet.createRow(rowCount);
int columnCount = indexSelect;
for (Object field :Prints) {
org.apache.poi.ss.usermodel.Row row = sheet.createRow(++rowCount);
Cell cell = row.createCell(columnCount);
if (field instanceof String) {
cell.setCellValue((String) field);
} else if (field instanceof Integer) {
cell.setCellValue((Integer) field);
}
}
// }
System.out.println("Current indexSelect is: "+indexSelect);
try {
FileOutputStream outputStream = new FileOutputStream("C:\\Softwares\\DataSource\\AchieversDataCopy.xlsx") ;
workbook.write(outputStream);
workbook.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ざっと見で、それが表示されます毎回、最後の反復だけが続く。プログラムが完了した後のシートのタイトルは何ですか? –
https://stackoverflow.com/a/15148013/566092 - 既存のブックに新しいシートを追加する方法 –
@coding_idiot最後のシートはforループ繰り返しのAchievers10最後のイテレータです;はい上書きします。 – PJAutomator