我对 Java 还比较陌生,并且已经拼凑了足够多的代码,使我可以将数据写入新的 Excel 文件。但是,我希望它写入(附加到末尾)到现有文件。prepareDataToWriteToExcel() 获取一些数据以写入 3 列数据。
public List writeDataToExcelFile(String fileName) throws IOException {
Map excelData = prepareDataToWriteToExcel();
List receiversList=new ArrayList();
HSSFWorkbook myWorkBook = new HSSFWorkbook();
HSSFSheet mySheet = myWorkBook.createSheet();
HSSFRow myRow = null;
HSSFCell myCell = null;
Iterator it=excelData.entrySet().iterator();
int rowNum=0;
while (it.hasNext()) {
myRow = mySheet.createRow(rowNum);
Map.Entry pairs = (Map.Entry)it.next();
String[]arr= (String[]) pairs.getValue();
for (int cellNum = 0; cellNum < arr.length ; cellNum++){
myCell = myRow.createCell((short) cellNum);
myCell.setCellValue(arr[cellNum]);
}
receiversList.add(arr[2]);
rowNum++;
}
try{
FileOutputStream out = new FileOutputStream(fileName);
myWorkBook.write(out);
System.out.println("WRITING TO EXCEL COMPLETED");
out.close();
}catch(Exception e){}
return receiversList;
}