0

I want to generate random numbers and parallel i want to write them in excel sheet in 1st column upto 15 rows. I tried with below script, but, it's not working.

I tried with below script, every time I want to store up to 15 numbers and those numbers should not be repeated.

I tried by using below methods, but I am not getting how can we iterate both of them. Random numbers and Excel cell.

public long Random_Number() {
    Random rand = new Random();
    long drand = (long)(rand.nextDouble()*1000000000L);
    long correct = 0;
    String numberString = Long.toString(drand);

    if (numberString.length() == 8) {
         System.out.println(drand+ "It's not a 9 digit");
    }
    else if (numberString.length() == 9) {
        correct = drand;
        System.out.println(correct);              
    }

    return correct; 
}

public void writeData_Int_SSN( int cellNum) {
    try {
        File src = new File("filename.xls");
        Cell cell = null;
        FileInputStream fis = new FileInputStream(src);
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        HSSFSheet sh1 = wb.getSheetAt(0);
        long gid = Random_Number();
        String GroupID = Long.toString(gid);
        System.out.println(GroupID);
        int num = Integer.parseInt(GroupID);

        for (int i = 1; i < 12; i++) {
            System.out.println("Entering into excel sheet");
            cell = sh1.getRow(i).getCell(cellNum);
            System.out.println("Iterating cells");

            if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                System.out.println("We are  entering  numeric data");
                int str1 = Integer.parseInt(cell.getStringCellValue());
                System.out.println(str1);
                cell.setCellValue(num);
            }
        }

        FileOutputStream fout = new FileOutputStream(new File("filename.xls"));
        wb.write(fout);
        fout.close();
    } 
    catch (Exception e) {
        System.out.println(e.getMessage());
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mani
  • 75
  • 2
  • 10

0 Answers0