I have a code which writes to the excel file using apache poi api. The problem is it everytime writes the new data to the file and not append the data. can you please help me here. Here is my code.
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelWrite {
public static void write(AddExcel addExcel){
try {
XSSFWorkbook workbook = new XSSFWorkbook("NG.xlsx");
XSSFSheet worksheet = workbook.createSheet("Scrap Data");
int lastRow = worksheet.getLastRowNum();
System.out.println(lastRow);
Row row = worksheet.createRow(++lastRow);
row.createCell(2).setCellValue(addExcel.getArtistName());
row.createCell(3).setCellValue(addExcel.getItemName());
row.createCell(6).setCellValue(addExcel.getOriginalPrimaryMarket());
row.createCell(7).setCellValue(addExcel.getAvgResalePrice());
row.createCell(8).setCellValue(addExcel.getPriceChangedFromPrimaryMarket());
row.createCell(9).setCellValue(addExcel.getHighestAvgBid());
row.createCell(10).setCellValue(addExcel.getLastSoldPrice());
row.createCell(11).setCellValue(addExcel.getSecondayMarketVolume());
row.createCell(12).setCellValue(addExcel.getSecondarySales());
row.createCell(13).setCellValue(addExcel.getPrimarySales());
row.createCell(14).setCellValue(addExcel.getDateCreated());
row.createCell(16).setCellValue(addExcel.getInstagramURl());
row.createCell(17).setCellValue(addExcel.getTwitterURL());
FileOutputStream out = new FileOutputStream(new File("NG.xlsx"));
workbook.write(out);
out.close();
System.out.println("Write Successfully.");
}
catch(IOException io){
System.out.println(io.getMessage());
System.out.println(io.getStackTrace());
}
}
}