我想通过迭代行在Excel工作表中写一组随机数。我能够写随机数,但我无法将它们存储在Excel中

时间:2019-01-31 13:12:30

标签: apache-poi excel-reader

我想生成随机数并并行,我想将它们写在excel表格的第一行中,最多15行。我尝试使用以下脚本,但无法正常工作。   我尝试使用以下脚本,每次我要存储最多15个数字,并且这些数字不应重复。

我尝试使用下面的方法,但是我不知道如何迭代这两个方法。随机数和excell细胞。

`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());
    }

}`

0 个答案:

没有答案