[英]How to convert string value of a cell in excel to numeric to do various calculations?
Sheet sheet = w.getSheet(0);
data = new String[sheet.getColumns()][sheet.getRows()];
data1 = new String[sheet.getColumns()][sheet.getRows()];
for (int j = 0; j <sheet.getColumns(); j++)
{
for (int i = 0; i < sheet.getRows(); i++)
{
Cell cell = sheet.getCell(j, i);
data[j][i] = cell.getContents();
System.out.println(data[j][i]);
// System.out.println(cell.getContents());
} System.out.println();
}
I need to find sum of the array data[j][i]
, but not able to since its a string. 我需要找到数组
data[j][i]
总和,但是由于它是一个字符串而无法找到它。
JXL has a class called NumberCell
whose getValue()
method returns a double. JXL有一个称为
NumberCell
的类, NumberCell
的getValue()
方法返回一个double。
See the documentation here . 请参阅此处的文档。
So if you change your data array to be double you could replace: 因此,如果将数据数组更改为两倍,则可以替换:
Cell cell = sheet.getCell(j, i);
data[j][i] = cell.getContents();
with: 与:
NumberCell cell = (NumberCell)sheet.getCell(j, i);
data[j][i] = cell.getValue();
And then you could do calculations on it. 然后您可以对此进行计算。
Of course, if you know the value to be an integer, you can cast it accordingly. 当然,如果您知道该值是整数,则可以相应地对其进行强制转换。
Note that it'll be best to check the type first, eg: 请注意,最好先检查类型,例如:
if (sheet.getCell(j, i).getType() == CellType.NUMBER)
And wrap it with try/catch whenever converting unknown / unchecked input. 并在转换未知/未经检查的输入时将其包装为try / catch。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.