Reading in a random cell value from excel sheet

后端 未结 2 881
孤街浪徒
孤街浪徒 2021-01-22 03:31

I have stored =RANDBETWEEN(10, 20) in my excel sheet, this formula generates random values in the excel sheet between 10 to 20. I need to read this 5 random values

相关标签:
2条回答
  • 2021-01-22 04:12

    You use i to query the same cell (A1) five times. This value is calculated upon opening / refreshing the worksheet in Excel. Why do you think that querying the same cell in rapid succession would result in something different each time?

    I'm not sure why you're relying on Excel for random numbers. Is there a reason you can't generate these in Java directly instead? For example:

    // Assuming 20 is your max, 10 your min
    Random random = new Random();
    int yourRandomNumber = random.nextInt((20 - 10) + 1) + 10;
    
    0 讨论(0)
  • 2021-01-22 04:14

    You always get 0-th row, but 5 times in line

    HSSFRow row1 = worksheet.getRow(0);  
    

    Change 0 to i

    for (int i=0;i<5;i++)
    {
       // ...
       HSSFRow row1 = worksheet.getRow(i); 
       // ...
    }
    

    You will get 0-th, 1-nd ... 4-th rows

    0 讨论(0)
提交回复
热议问题