Cannot get a text value from a numeric cell “Poi”

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

I'm trying to consume data from a spreadsheet in Excel, but always of this error, already tried formatting the worksheet to text and number and still the error persists.

I saw a person using it resolved cell.setCellType ( Cell.CELL_TYPE_STRING ) ; but I do not know where I fit this passage in my code.

WebElement searchbox = driver.findElement(By.name("j_username")); WebElement searchbox2 = driver.findElement(By.name("j_password"));           try {     FileInputStream file = new FileInputStream(new File("C:\\paulo.xls"));      HSSFWorkbook workbook = new HSSFWorkbook(file);     HSSFSheet sheet = workbook.getSheetAt(0);      for (int i=1; i 

回答1:

Formatter will work fine in this case.

import org.apache.poi.ss.usermodel.DataFormatter;  FileInputStream fis = new FileInputStream(workbookName); Workbook workbook = WorkbookFactory.create(fis); Sheet sheet = workbook.getSheet(sheetName); DataFormatter formatter = new DataFormatter(); String val = formatter.formatCellValue(sheet.getRow(col).getCell(row)); list.add(val);   //Adding value to list 


回答2:

    Cell cell = sheet.getRow(i).getCell(0);     cell.setCellType ( Cell.CELL_TYPE_STRING );     String j_username = cell.getStringCellValue(); 

UPDATE

Ok, as have been said in comments, despite this works it isn't correct method of retrieving data from an Excel's cell.

According to the manual here:

If what you want to do is get a String value for your numeric cell, stop!. This is not the way to do it. Instead, for fetching the string value of a numeric or boolean or date cell, use DataFormatter instead.

And according to the DataFormatter API

DataFormatter contains methods for formatting the value stored in an Cell. This can be useful for reports and GUI presentations when you need to display data exactly as it appears in Excel. Supported formats include currency, SSN, percentages, decimals, dates, phone numbers, zip codes, etc.

So, right way to show numeric cell's value is as following:

 DataFormatter formatter = new DataFormatter(); //creating formatter using the default locale  Cell cell = sheet.getRow(i).getCell(0);  String j_username = formatter.formatCellValue(cell); //Returns the formatted value of a cell as a String regardless of the cell type. 


回答3:

As explained in the Apache POI Javadocs, you should not use cell.setCellType(Cell.CELL_TYPE_STRING) to get the string value of a numeric cell, as you'll loose all the formatting

Instead, as the javadocs explain, you should use DataFormatter

What DataFormatter does is take the floating point value representing the cell is stored in the file, along with the formatting rules applied to it, and returns you a string that look like it the cell does in Excel.

So, if you're after a String of the cell, looking much as you had it looking in Excel, just do:

 // Create a formatter, do this once  DataFormatter formatter = new DataFormatter(Locale.US);   .....   for (int i=1; i 

The formatter will return String cells as-is, and for Numeric cells will apply the formatting rules on the style to the number of the cell



回答4:

Using the DataFormatter this issue is resolved. Thanks to "Gagravarr" for the initial post.

DataFormatter formatter = new DataFormatter();  String empno = formatter.formatCellValue(cell0); 


回答5:

Use that code it definitely works and I modified it.

import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import org.apache.poi.poifs.filesystem.POIFSFileSystem; //import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.*;  public class TestApp {      public static void main(String[] args) throws Exception {          try {              Class forName = Class.forName("com.mysql.jdbc.Driver");             Connection con = null;             con = DriverManager.getConnection("jdbc:mysql://localhost/tables", "root", "root");             con.setAutoCommit(false);             PreparedStatement pstm = null;             FileInputStream input = new FileInputStream("C:\\Users\\Desktop\\a1.xls");             POIFSFileSystem fs = new POIFSFileSystem(input);             Workbook workbook;             workbook = WorkbookFactory.create(fs);             Sheet sheet = workbook.getSheetAt(0);             Row row;             for (int i = 1; i 


回答6:

This will work:

WebElement searchbox = driver.findElement(By.name("j_username")); WebElement searchbox2 = driver.findElement(By.name("j_password"));            try {        FileInputStream file = new FileInputStream(new File("C:\\paulo.xls"));        HSSFWorkbook workbook = new HSSFWorkbook(file);        HSSFSheet sheet = workbook.getSheetAt(0);      for (int i=1; i 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!