when am executing the below code am getting <terminated> WriteExcelFile[java application] error



  • package seleniumpackage;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import org.apache.poi.hssf.usermodel.HSSFWorkbook;

    import org.apache.poi.ss.usermodel.Cell;

    import org.apache.poi.ss.usermodel.Row;

    import org.apache.poi.ss.usermodel.Sheet;

    import org.apache.poi.ss.usermodel.Workbook;

    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class WriteExcelFile {

    public void writeExcel(String filePath,String fileName,String sheetName,String[] dataToWrite) throws IOException{
    
        //Create an object of File class to open xlsx file
    
        File file =    new File(filePath+"\\"+fileName);
    
        //Create an object of FileInputStream class to read excel file
    
        FileInputStream inputStream = new FileInputStream(file);
    
        Workbook guru99Workbook = null;
    
        //Find the file extension by splitting  file name in substring and getting only extension name
    
        String fileExtensionName = fileName.substring(fileName.indexOf("."));
    
        //Check condition if the file is xlsx file
    
        if(fileExtensionName.equals(".xlsx")){
    
        //If it is xlsx file then create object of XSSFWorkbook class
    
        guru99Workbook = new XSSFWorkbook(inputStream);
    
        }
    
        //Check condition if the file is xls file
    
        else if(fileExtensionName.equals(".xls")){
    
            //If it is xls file then create object of XSSFWorkbook class
    
            guru99Workbook = new HSSFWorkbook(inputStream);
    
        }    
    
    //Read excel sheet by sheet name    
    
    Sheet sheet = guru99Workbook.getSheet(sheetName);
    
    //Get the current count of rows in excel file
    
    int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
    
    //Get the first row from the sheet
    
    Row row = sheet.getRow(0);
    
    //Create a new row and append it at last of sheet
    
    Row newRow = sheet.createRow(rowCount+1);
    
    //Create a loop over the cell of newly created Row
    
    for(int j = 0; j < row.getLastCellNum(); j++){
    
        //Fill data in row
    
        Cell cell = newRow.createCell(j);
    
        cell.setCellValue(dataToWrite[j]);
    
    }
    
    //Close input stream
    
    inputStream.close();
    
    //Create an object of FileOutputStream class to create write data in excel file
    
    FileOutputStream fos = new FileOutputStream(file);
    
    //write data in the excel file
    System.out.println(file);
    
    guru99Workbook.write(fos);
    
    //close output stream
    
    fos.close();
    
    }
    
    public static void main(String...strings) throws IOException{
    
        //Create an array with the data in the same order in which you expect to be filled in excel file
    
        String[] dataToWrite = {"Mr. E","Noida"};
    
        System.out.println(dataToWrite);
        //Create an object of current class
    
        WriteExcelFile objExcelFile = new WriteExcelFile();
    
        //Write the file using file name, sheet name and the data to be filled
    
        objExcelFile.writeExcel(System.getProperty("user.dir")+"\\src\\excelExportAndFileIO","ExportExcel.xlsx","ExcelGuru99Demo",dataToWrite);
    
    }
    

    }



  • I went through the code and tried running it, and got the same message in the console. But it isn't an error actually.

    More specifically it is giving message as: C:\Program Files\Java\jre1.8.0_191\bin\javaw.exe (Sep 24, 2019, 4:00:39 PM)

    In above statement, "Terminated" means the program finished, and exit code 0 means "normal completion" So basically Eclipse says "your program is done."

    Verify your excel, data you are trying to add must have been inserted.


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2