165
  1. I have excel file with such contents:

    • A1: SomeString

    • A2: 2

    All fields are set to String format.

  2. When I read the file in java using POI, it tells that A2 is in numeric cell format.

  3. The problem is that the value in A2 can be 2 or 2.0 (and I want to be able to distinguish them) so I can't just use .toString().

What can I do to read the value as string?

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
joycollector
  • 1,986
  • 4
  • 17
  • 18

24 Answers24

346

I had same problem. I did cell.setCellType(Cell.CELL_TYPE_STRING); before reading the string value, which solved the problem regardless of how the user formatted the cell.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
wil
  • 3,596
  • 1
  • 16
  • 3
  • I use poi-3.8-beta4, and its working as expected! Why don't TS accept this as the answer? – swdev Sep 10 '12 at 09:34
  • Be aware that POI numeric to String conversion doesn't take into a consideration the system locale, it always uses the dot as a decimal separator. For example, if your system uses ",", and in Excel numbers look like "1,9", POI will return "1.9" instead. – Alexey Berezkin Jan 25 '13 at 10:39
  • I wonder will this account for formatting? Like if the Excel file always shows up to 2 decimal places, will the string value be exactly the same? – ryvantage Sep 27 '13 at 02:23
  • Not working for me. Actually i am reading time from Excel sheet cell, getting numeric value. How can i read it as a Time or String. Pls help . – Akash5288 May 24 '14 at 07:19
  • Beware of setCellType(CellType.CELL_TYPE_STRING) memory footprint. With 3.10-FINAL, I ran into trouble reading large files. FlighRecorder showed enormous Object[] allocations due to org.apache.poi.xssf.usermodel.XSSFWorkbook.onDeleteFormula(XSSFCell) call. I suggest using XSSFCell.getRawValue() for reading. This solved my problem, allocations are gone. Be sure to have all formulas evaluated, though. – Jurri Sep 27 '14 at 21:01
  • 67
    Note that the [Apache POI javadocs explicitly say not to do this!](https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#setCellType%28int%29) As they explain, you should use DataFormatter instead – Gagravarr May 24 '15 at 11:13
  • 8
    Gagravarr's warning against doing this is right! From the docs: "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." https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#setCellType%28int%29 I was using this technique myself until I wound up accidentally changing data I didn't intend to change. (Set type to String, read value, set type back to numeric, read again and get a different numeric value!) – Chris Finley Jul 31 '15 at 13:19
  • 1
    @Wil cell.setCellType(Cell.CELL_TYPE_STRING); not works for .xlsx sheet. Any other solution for this? – Meenaxi Oct 24 '15 at 06:26
  • 6
    Use DataFormatter. The Javadoc warns us from using the above method. – Balu SKT Feb 26 '16 at 13:38
  • Excellent solution. I'm using version 3.14 and for that cell.setCellType(XSSFCell.CELL_TYPE_STRING); will be works – Harsha Apr 08 '16 at 14:36
  • This answer should clearly be degraded and/or removed, as it's incorrect. – maxxyme Oct 28 '16 at 13:18
  • Work fine for interger, not for double – 世界知名伟人 May 21 '20 at 02:12
  • @deprecated This method is deprecated and will be removed in POI 5.0. – Tiago Medici Jun 30 '20 at 15:49
  • Please be aware that excel is not very good in long sequences of digits. When you enter a sequence of 20 times four "44444444444444444444", then excel will show you "4,44444E+19". When you represent this as a number format "0" (no decimal difgits) then you get "44444444444444400000". It has already lost the precision of the least important digits. Numeric value strings should be entered in a cell that is already formatted as TEXT. This is the only way not to lose data. Of course, you may not be in the position to do anything about this, and then your best bet is the DataFormatter. – Radboud Feb 05 '21 at 12:54
127

I don't think we had this class back when you asked the question, but today there is an easy answer.

What you want to do is use the DataFormatter class. You pass this a cell, and it does its best to return you a string containing what Excel would show you for that cell. If you pass it a string cell, you'll get the string back. If you pass it a numeric cell with formatting rules applied, it will format the number based on them and give you the string back.

For your case, I'd assume that the numeric cells have an integer formatting rule applied to them. If you ask DataFormatter to format those cells, it'll give you back a string with the integer string in it.

Also, note that lots of people suggest doing cell.setCellType(Cell.CELL_TYPE_STRING), but the Apache POI JavaDocs quite clearly state that you shouldn't do this! Doing the setCellType call will loose formatting, as the javadocs explain the only way to convert to a String with formatting remaining is to use the DataFormatter class.

A simple example of using this class:

DataFormatter dataFormatter = new DataFormatter();
String formattedCellStr = dataFormatter.formatCellValue(cell);
Renis1235
  • 4,116
  • 3
  • 15
  • 27
Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • Thanks @Gagravarr only your answer work for me, cell.setCellType(Cell.CELL_TYPE_STRING); in convert the 2.2 value as 2.2000000000000002, but i want 2.2. it return anything in string format thank – ankush yadav Jun 14 '16 at 11:06
  • dataformatter does not seem to work for Formula cells, it returns a string representation of the formula instead of the value – gaurav5430 Jul 04 '16 at 10:58
  • 3
    Just one minor note: Please provide short code snippets for such answers, also if those are stated in provided links – BAERUS Sep 06 '17 at 09:33
  • @gaurav5430 Yes, it doesnt go well with formulas... According to doc, `When passed a null or blank cell, this method will return an empty String (""). Formulas in formula type cells will not be evaluated.` – SaratBhaswanth Jan 25 '19 at 07:03
  • This also does not work well with large integers that have a numeric cell type, because `DataFormatter` internally converts it to a `double`, then back to a `String`, so there's the risk of losing precision, and leading/trailing 0s – Stik Jan 25 '23 at 13:41
66

The below code worked for me for any type of cell.

InputStream inp =getClass().getResourceAsStream("filename.xls"));
Workbook wb = WorkbookFactory.create(inp);
DataFormatter objDefaultFormat = new DataFormatter();
FormulaEvaluator objFormulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) wb);

Sheet sheet= wb.getSheetAt(0);
Iterator<Row> objIterator = sheet.rowIterator();

while(objIterator.hasNext()){

    Row row = objIterator.next();
    Cell cellValue = row.getCell(0);
    objFormulaEvaluator.evaluate(cellValue); // This will evaluate the cell, And any type of cell will return string value
    String cellValueStr = objDefaultFormat.formatCellValue(cellValue,objFormulaEvaluator);

}
Vinayak Dornala
  • 1,609
  • 1
  • 21
  • 27
  • 9
    Worked just fine! My suggestion would be to change the way the FormulaEvaluator is retrieved. The Workbook class provides a formula evaluator though the `getCreationHelper().createFormulaEvaluator()` method. This way your code won't be coupled with the HSSFFormulaEvaluator class. – Vitor Santos Feb 16 '16 at 17:48
  • 2
    This should be the accepted answer. Thanks @Vinayak – Mattiavelli Jun 22 '17 at 17:13
  • Can `FormulaEvaluator` simply be removed from this solution? Does it serve a purpose? – P.Brian.Mackey Sep 08 '17 at 20:23
  • 1
    the call to objFormulaEvaluator.evaluate is not necessary. The return value of that is not being used here. – Radu Simionescu Oct 08 '18 at 14:24
43

I would recommend the following approach when modifying cell's type is undesirable:

if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
    String str = NumberToTextConverter.toText(cell.getNumericCellValue())
}

NumberToTextConverter can correctly convert double value to a text using Excel's rules without precision loss.

Stanislav Mamontov
  • 1,734
  • 15
  • 21
20

As already mentioned in the Poi's JavaDocs (https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#setCellType%28int%29) don't use:

cell.setCellType(Cell.CELL_TYPE_STRING);

but use:

DataFormatter df = new DataFormatter();
String value = df.formatCellValue(cell);

More examples on http://massapi.com/class/da/DataFormatter.html

userM1433372
  • 5,345
  • 35
  • 38
16

Yes, this works perfectly

recommended:

        DataFormatter dataFormatter = new DataFormatter();
        String value = dataFormatter.formatCellValue(cell);

old:

cell.setCellType(Cell.CELL_TYPE_STRING);

even if you have a problem with retrieving a value from cell having formula, still this works.

Rajesh Mbm
  • 814
  • 9
  • 11
  • 5
    But you have to be careful using this for double values. For me it turned the value 7.9 into 7.8999956589965... – Chris Oct 24 '14 at 11:52
  • 2
    The [Apache POI javadocs are very clear that you shouldn't be doing it like that](https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#setCellType%28int%29): *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.* – Gagravarr Feb 26 '16 at 13:19
5

Try:

new java.text.DecimalFormat("0").format( cell.getNumericCellValue() )

Should format the number correctly.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
iTake
  • 4,082
  • 3
  • 33
  • 26
  • As I understand, the asker wants to be able to distinguish between `2` and `2.0`. Your solution wouldn't do this. (But still, welcome to Stack Overflow!) – Paŭlo Ebermann Aug 17 '11 at 19:15
2

You can read numerical cells as String using java.

int type = cell.getCellType();
if(type == 0){
   String value = NumberToTextConverter.toText(cell.getNumericCellValue());
}
else{
   value = String.valueOf(cell.getStringCellValue());
}

Here,

0 => numeric cell

getCellType() => this method use to get type of excel cell.

1

As long as the cell is in text format before the user types in the number, POI will allow you to obtain the value as a string. One key is that if there is a small green triangle in the upper left-hand corner of cell that is formatted as Text, you will be able to retrieve its value as a string (the green triangle appears whenever something that appears to be a number is coerced into a text format). If you have Text formatted cells that contain numbers, but POI will not let you fetch those values as strings, there are a few things you can do to the Spreadsheet data to allow that:

  • Double click on the cell so that the editing cursor is present inside the cell, then click on Enter (which can be done only one cell at a time).
  • Use the Excel 2007 text conversion function (which can be done on multiple cells at once).
  • Cut out the offending values to another location, reformat the spreadsheet cells as text, then repaste the previously cut out values as Unformatted Values back into the proper area.

One final thing that you can do is that if you are using POI to obtain data from an Excel 2007 spreadsheet, you can the Cell class 'getRawValue()' method. This does not care what the format is. It will simply return a string with the raw data.

0

getStringCellValue returns NumberFormatException if the cell type is numeric. If you don't want to change the cell type to string, you can do this.

String rsdata = "";
try {
    rsdata = cell.getStringValue();
} catch (NumberFormatException ex) {
    rsdata = cell.getNumericValue() + "";
}
zawhtut
  • 8,335
  • 5
  • 52
  • 76
0

When we read the MS Excel's numeric cell value using Apache POI library, it read it as numeric. But sometime we want it to read as string (e.g. phone numbers, etc.). This is how I did it:

  1. Insert a new column with first cell =CONCATENATE("!",D2). I assume D2 is cell id of your phone-number column. Drag new cell up to end.

  2. Now if you read the cell using POI, it will read the formula instead of calculated value. Now do following:

  3. Add another column

  4. Select complete column created in step 1. and choose Edit->COPY

  5. Go to top cell of column created in step 3. and Select Edit->Paste Special

  6. In the opened window, Select "Values" radio button

  7. Select "OK"

  8. Now read using POI API ... after reading in Java ... just remove the first character i.e. "!"

Asif Shahzad
  • 843
  • 2
  • 11
  • 21
0

Many of these answers reference old POI documentation and classes. In the newest POI 3.16, Cell with the int types has been deprecated

Cell.CELL_TYPE_STRING

enter image description here

Instead the CellType enum can be used.

CellType.STRING 

Just be sure to update your pom with the poi dependency as well as the poi-ooxml dependency to the new 3.16 version otherwise you will continue to get exceptions. One advantage with this version is that you can specify the cell type at the time the cell is created eliminating all the extra steps described in previous answers:

titleRowCell = currentReportRow.createCell(currentReportColumnIndex, CellType.STRING);
Nelda.techspiress
  • 643
  • 12
  • 32
0

This worked perfect for me.

Double legacyRow = row.getCell(col).getNumericCellValue();
String legacyRowStr = legacyRow.toString();
if(legacyRowStr.contains(".0")){
    legacyRowStr = legacyRowStr.substring(0, legacyRowStr.length()-2);
}
Rama Krishna
  • 275
  • 3
  • 12
0

I would much rather go the route of the wil's answer or Vinayak Dornala, unfortunately they effected my performance far to much. I went for a HACKY solution of implicit casting:

for (Row row : sheet){
String strValue = (row.getCell(numericColumn)+""); // hack
...

I don't suggest you do this, for my situation it worked because of the nature of how the system worked and I had a reliable file source.

Footnote: numericColumn Is an int which is generated from reading the header of the file processed.

KeaganFouche
  • 581
  • 5
  • 12
0
public class Excellib {
public String getExceldata(String sheetname,int rownum,int cellnum, boolean isString) {
    String retVal=null;
    try {
        FileInputStream fis=new FileInputStream("E:\\Sample-Automation-Workspace\\SampleTestDataDriven\\Registration.xlsx");
        Workbook wb=WorkbookFactory.create(fis);
        Sheet s=wb.getSheet(sheetname);
        Row r=s.getRow(rownum);
        Cell c=r.getCell(cellnum);
        if(c.getCellType() == Cell.CELL_TYPE_STRING)
        retVal=c.getStringCellValue();
        else {
            retVal = String.valueOf(c.getNumericCellValue());
        }

I Tried This and It worked For me

Prasanna
  • 35
  • 3
0

There is a ready-to-use wrapper (some additional optimizations can be applied)

  • it supports numeric and String cells

  • formulas are recognized and handled automatically

  • avoid some boilerplate

     public final class Cell {
    
     private final static DataFormatter FORMATTER = new DataFormatter();
    
     private XSSFCell mCell;
    
     public Cell(@NotNull XSSFCell cell) {
         mCell = cell;
    
         if (isFormula()) {
             XSSFWorkbook book = mCell.getSheet().getWorkbook();
             FormulaEvaluator evaluator = book.getCreationHelper().createFormulaEvaluator();
             mCell = (XSSFCell) evaluator.evaluateInCell(mCell);
         }
     }
    
     /**
      * Get content
      */
     public final int getInt() {
         return (int) getLong();
     }
    
     public final long getLong() {
         return Math.round(getDouble());
     }
    
     public final double getDouble() {
         return mCell.getNumericCellValue();
     }
    
     public final String getString() {
         if (!isString()) {
             return FORMATTER.formatCellValue(mCell);
         }
         return mCell.getStringCellValue();
     }
    
     /**
      * Get properties
      */
     public final boolean isNumber() {
         if (isFormula()) {
             return mCell.getCachedFormulaResultType().equals(CellType.NUMERIC);
         }
         return mCell.getCellType().equals(CellType.NUMERIC);
     }
    
     public final boolean isString() {
         if (isFormula()) {
             return mCell.getCachedFormulaResultType().equals(CellType.STRING);
         }
         return mCell.getCellType().equals(CellType.STRING);
     }
    
     public final boolean isFormula() {
         return mCell.getCellType().equals(CellType.FORMULA);
     }
    
     /**
      * Debug info
      */
     @Override
     public String toString() {
         return getString();
     }
     }
    
Sergey Krivenkov
  • 579
  • 1
  • 6
  • 14
0

I also have had a similar issue on a data set of thousands of numbers and I think that I have found a simple way to solve. I needed to get the apostrophe inserted before a number so that a separate DB import always sees the numbers as text. Before this the number 8 would be imported as 8.0.

Solution:

  • Keep all the formatting as General.
  • Here I am assuming numbers are stored in Column A starting at Row 1.
  • Put in the ' in Column B and copy down as many rows as needed. Nothing appears in the worksheet but clicking on the cell you can see the apostophe in the Formula bar.
  • In Column C: =B1&A1.
  • Select all the Cells in Column C and do a Paste Special into Column D using the Values option.

Hey Presto all the numbers but stored as Text.

Garrett Hyde
  • 5,409
  • 8
  • 49
  • 55
0

I encountered the same issue and easiest fix would be setting the CELL TYPE as STRING. This will avoid exceptions being prompted.

FileInputStream fis = new FileInputStream(new File(filePath));
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0); // get first sheet
row.getCell(1).setCellType(CellType.STRING); // set Cell Type as String
String val = row.getCell(1).getStringCellValue(); // get the value as String type
System.out.println(val); // prints the value; 
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
0

Another option would be to force excel to evaluate the integer value as a string. To achieve that you will have to prefix a single quote before the number.

Here is an example appending a single quote to number 1:

enter image description here

Flavio Oliva
  • 401
  • 4
  • 15
-1

Do you control the excel worksheet in anyway? Is there a template the users have for giving you the input? If so, you can have code format the input cells for you.

datatoo
  • 2,019
  • 2
  • 21
  • 28
-1

It looks like this can't be done in the current version of POI, based on the fact that this bug:

https://issues.apache.org/bugzilla/show_bug.cgi?id=46136

is still outstanding.

Simon D
  • 4,150
  • 5
  • 39
  • 47
-2

We had the same problem and forced our users to format the cells as 'text' before entering the value. That way Excel correctly stores even numbers as text. If the format is changed afterwards Excel only changes the way the value is displayed but does not change the way the value is stored unless the value is entered again (e.g. by pressing return when in the cell).

Whether or not Excel correctly stored the value as text is indicated by the little green triangle that Excel displays in the left upper corner of the cell if it thinks the cell contains a number but is formated as text.

Turismo
  • 2,064
  • 2
  • 16
  • 23
-2

cell.setCellType(Cell.CELL_TYPE_STRING); is working fine for me

  • 1
    However, it is [explicitly listed in the JavaDocs as not the right way to do it](https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#setCellType%28int%29)! – Gagravarr May 24 '15 at 11:11
-4

cast to an int then do a .toString(). It is ugly but it works.

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
WolfmanDragon
  • 7,851
  • 14
  • 49
  • 61