从excel表中读取随机单元格值 [英] Reading in a random cell value from excel sheet

查看:377
本文介绍了从excel表中读取随机单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在excel表中存储了 = RANDBETWEEN(10,20),这个公式在excel表格中生成10到20之间的随机值。我需要阅读这个我的java程序中有5个随机值。

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 in my java program.

虽然函数在我的excel表中给出了不同的值,但是我的java程序正在读取相同的常量随机值5次..我怎样才能读取5个不同的随机值?

Although the function is giving out different value in my excel sheet but my java program is reading the same constant random value 5 times.. how can I read 5 different random values?

这就是我所做的:

public class JavaApplication1 {
    public static void main(String[] args) {
        try {
            for (int i = 0; i < 5; i++) {
                FileInputStream fileInputStream =
                    new FileInputStream("C://users/user/Desktop/C.xls");
                HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
                HSSFSheet worksheet = workbook.getSheet("POI Worksheet");
                HSSFRow row1 = worksheet.getRow(0);
                HSSFCell cellA1 = row1.getCell((short) 0);
                double a1Val = cellE1.getNumericCellValue();
                System.out.println("A1: " + a1Val);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我得到的输出是:

A1: 12.0
A1: 12.0
A1: 12.0
A1: 12.0
A1: 12.0

如何让我的程序读取五个不同的值?

How can I make my program to read five different values?

推荐答案

您使用 i 查询同一个单元格(A1)五次。在Excel中打开/刷新工作表时计算此值。为什么你认为快速连续查询相同的单元格会导致每次都有不同的东西?

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?

我不确定为什么你依赖Excel来获取随机数。有没有理由你不能直接在Java中生成这些?例如:

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;

这篇关于从excel表中读取随机单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆