
首先先根据sheet找到行
row = sheetgetRow(rowIndex);
然后找
cell = rowgetCell(1);
这样就去到当前的B1了,
cellgetStringCellValue()
就能取到当前的单元格的value
这是正常的。
通过POI取出的数值默认都是double,即使excel单元格中存的是1,取出来的值也是10,这就造成了一些问题,
如果数据库字段是int,那么就会wrong data type,所以需要对数值类型处理。
代码如下:
Cell cell = null;// 单元格
Object inputValue = null;// 单元格值
if(!isEmpty(cell) && cellgetCellType() == CellCELL_TYPE_NUMERIC) {
long longVal = Mathround(cellgetNumericCellValue());
if(DoubleparseDouble(longVal + "0") == doubleVal)
inputValue = longVal;
else
inputValue = doubleVal;
}
这么处理后,单元格中的小数没有变化,如果是整数,也会取到整数。
package comlinyistest1;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFRow;
import orgapachepoihssfusermodelHSSFSheet;
public class HssfCells
{
public void getCellValues(HSSFSheet sheet)
{
if(sheet == null)
{
Systemoutprintln("sheet is null");
return ;
}
for(int i = 0 ; i < sheetgetLastRowNum() ; i ++)
{
HSSFRow row = sheetgetRow(i) ;
if(row == null)
{
Systemoutprintln("单元格第" + (i+1)+ "行为空");
}
else
{
for(int j = 0 ; j < rowgetLastCellNum() ; j ++)
{
HSSFCell cell = rowgetCell(j) ;
if(cell == null)
{
Systemoutprintln("单元格第" + (i+1)+ "行 " + (j+1) + "列为null");
}
else
{
String s = cellgetCellType() == HSSFCellCELL_TYPE_STRINGcellgetStringCellValue():cellgetNumericCellValue()+"";
Systemoutprintln("单元格第" + (i+1)+ "行 " + (j+1) + "列值为" + s);
}
}
}
}
}
}
以上就是关于java用POI *** 作Excel时当遍历单元格时如何获取当前单元个的具体坐标全部的内容,包括:java用POI *** 作Excel时当遍历单元格时如何获取当前单元个的具体坐标、为什么用poi读取excel 的数字全是double、java poi 读取excel 获得一行的单元格个数不对等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)