java - 无法从数字单元格java中获取文本值
问题描述
当我尝试读取 excel 表时,我遇到了代码问题,如果它包含它将读取的文本,但它不读取它的数字并给我这个错误 Cannot get a text value from a numeric cell java
public static void main(String[] args) throws IOException, Exception {
/*WebDriver dr;
// TODO Auto-generated method stub
dr = new FirefoxDriver();
dr.get("https://www.example.com/");
ArrayList<String> username=readExcelData(0);
ArrayList<String> pass=readExcelData(1);
for(int i=0; i<username.size();i++) {
dr.findElement(By.id("email")).sendKeys(username.get(i));
dr.findElement(By.id("pass")).sendKeys(pass.get(i));
dr.findElement(By.id("loginbutton")).click();
Thread.sleep(6000);
dr.findElement(By.id("email")).clear();
dr.findElement(By.id("pass")).clear();
}*/
// it work with txt
//readExcelData(0);
// it will not work with the number
//readExcelData(1);
}
public static ArrayList<String> readExcelData(int colNo) throws IOException {
FileInputStream fis= new FileInputStream("C:\\dat.xls");
HSSFWorkbook wb=new HSSFWorkbook(fis);
HSSFSheet s=wb.getSheet("sh1");
Iterator<Row> rowIterator=s.iterator();
rowIterator.next();
ArrayList<String> list=new ArrayList<String>();
while (rowIterator.hasNext()) {
list.add( rowIterator.next().getCell(colNo).getStringCellValue());
}
System.out.println("List :::"+list);
return list;
}
解决方案
试试下面的代码:
public static ArrayList<String> readExcelData(int colNo) throws IOException {
FileInputStream fis= new FileInputStream("C:\\dat.xls");
HSSFWorkbook wb=new HSSFWorkbook(fis);
HSSFSheet s=wb.getSheet("sh1");
Iterator<Row> rowIterator=s.iterator();
rowIterator.next();
ArrayList<String> list=new ArrayList<String>();
DataFormatter formatter = new DataFormatter();
while (rowIterator.hasNext()) {
String val = formatter.formatCellValue(rowIterator.next().getCell(colNo));
list.add(val);
}
System.out.println("List :::"+list);
return list;
}
推荐阅读
- ruby-on-rails - Rails 5如何从公共文件夹中的css文件访问公共文件夹中的图像
- javascript - 使从输入中获取值的变量在词法范围内可用。javascript
- reactjs - 如何使用样式组件设置所有元素的样式?
- android - 为什么android toast消息总是在我的应用程序中显示appname:message?
- ssl - 仅来自某些服务器的链中自签名证书
- python-3.x - 在解析文件时制作一行列表
- haskell - 如何遍历字符列表并在 Haskell 中操作字符?
- python - 如何使用 opencv 从市场传单中获取产品图片?
- c++ - 使用 & 的基于范围的 for 循环
- spring-boot - 为什么 Spring Boot @Value 将属性值读取为 1 而不是 001?