Reading excel using POI HSSFCell with leading zeros [duplicate] - java

This question already has answers here:
How can I read numeric strings in Excel cells as string (not numbers)?
(24 answers)
Closed 8 years ago.
I am trying to read an excel file using Java POI HSSF. Everything was working fine, except that when the value is 001001 the HSSFCell will return 1001.0
Is there any way I can use HSSFCell to get the value 001001 ?
I am not supposed to do any modification to the excel file.
Thank in advance for any help and suggestion.
Edit
I have been using the following code:
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.toString();
I also ran a debug mode and checked on the cell value the moment the HSSF grabs it. It truncate the leading zeros and converted it to double therefore I do not see a way to retrieve the truncated zeros. There is a link that stated it could be a bug from HSSF:
http://osdir.com/ml/jakarta.poi.user/2003-02/msg00007.html
By the way, I solved it by hard coded it. The number of digits are know in advanced. The link for the code that I used: How to format a Java string with leading zero?

Can you try reading it as a string instead of Numeric,
cell.setCellType(Cell.CELL_TYPE_STRING);
Changing the cell type usually will not modify the contents of the cell.
it can be retrieved with either of the following approaches:
cell.getStringCellValue();
cell.getRichStringCellValue().getString();

Related

csvWriter write long number in cell in Java

While writing the excel file is fine I see that really long numbers are scientific notations in excel
Example: 8.71129E+12
instead of: 1234567890
How can I do it in Java
I am writing like
String nart = "1236547865452";
csvWriter.append(nart);
Not sure how the same can be achieved using writing to simple CSV? ,
but through APACHE POI You can write to excel and you need to do set the cell type if you want to see the whole value like 1236547865452 cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); and this is not needed it you have to see scientific notation as 8.71129E+12

Converting from a double to a String but keeping an leading zeros

I am trying to convert from a double to a String. It is working but it is removing all leading zeros. For example, 00010000 is being changed to 10000 and 01 is changed to 1. I am trying to read an xls file and it is proving difficult.
The amount of digits is not known when running the program so I couldn't think of a way to get around this problem. Could anyone help?
Below is my code:
while (cells.hasNext()) {
cell = (HSSFCell) cells.next();
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
lineList.add(cell.getStringCellValue());
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String s = String.valueOf(cell.getNumericCellValue());
lineList.add(s);
}
}
My guess as to what is happening here is that you have numeric data in Excel which is formatted as 00010000. However, internally in Excel this data is probably just stored as 10000 or 10000.0 etc. and the leading zeroes are just present because of your formatting. When you imported this numeric data into Java using HSSF, it also came in as 10000 or 10000.0 etc. As others have already mentioned, there are no "leading" zeroes in a Java double primitive type. So when you convert to a String, of course there are no leading zeroes, because they simply don't exist.
Possible workaround:
If you format your Excel numeric column as text, and then import it, then the following if condition should fire for these values:
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
lineList.add(cell.getStringCellValue());
}
You can then simply read in the text values directly, leading zeroes and all, and retain the information which was present in your Excel spreadsheet.
I have not tested this, but this would probably be something I would try if I were faced with your problem.

Avoiding decimal points while reading integer data from excel using Java and Apache POI [duplicate]

This question already has answers here:
returning decimal instead of string (POI jar)
(3 answers)
Closed 7 years ago.
I am trying to develop a data driven Selenium automation framework in Java. I have written code to read input test data from excel sheet. The excel sheet contains two columns - Username and Password. I read the excel data using the following code.
String testData;
for(int j=1;j<currentRow.getPhysicalNumberOfCells();j++){
if(currentRow.getCell(j)!==null)
{
if(currentRow.getCell(j).getCellType()==Cell.CELL_TYPE_BOOLEAN){
testData=Boolean.toString(currentRow.getCell(j).getBooleanCellValue());
}
if(currentRow.getCell(j).getCellType()==Cell.CELL_TYPE_NUMERIC){
testData=Double.toString(currentRow.getCell(j).getNumericCellValue());
}
if(currentRow.getCell(j).getCellType()==Cell.CELL_TYPE_STRING){
testData=currentRow.getCell(j).getStringValue();
}
}
}
The problem is that if the password is 123, the above code will return the value 123.0 and hence the test case fails. I cannot remove the decimal point since if the actual password is 123.0, it would return the result 123. How can I read the excel data as it is given in the cell?
add cell.setCellType(Cell.CELL_TYPE_STRING); before starting to read.
EDIT : POI API Documentation says,
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.
refer This answer for a better solution!
Read everything in string format and then compare

Identify bullet points in MS Excel using Apache POI

I am reading columns in MS Excel using Apache POI but the code is not able to identify bullet point. I am trying this:
String cellValue = cell.getStringCellValue();
if(cellValue.contains("•")){}
but this is not working. the cellvalue is giving some garbage value in case of bullet point and this is why it is not able to compare in my if condition.
Can anyone please suggest solution ?
You need to use cell.getRichStringCellValue().getString() and then check for \u2022 which is unicode for "•"

POI: setCellType(Cell.CELL_TYPE_FORMULA) fails because of Cell.CELL_TYPE_ERROR

My Java application reads an xls file and presents it on a JTable. So far so good.
When I try to save my worksheet, I iterate over row,col in my JTable and:
String str = (String) Table.getValueAt(row, col);
HSSFRow thisrow = sheet.getRow(row);
HSSFCell thiscell = thisrow.getCell(col);
if(thiscell==null) thiscell = thisrow.createCell(col);
switch(inferType(str)) {
case "formula":
thiscell.setCellType(Cell.CELL_TYPE_FORMULA);
thiscell.setCellFormula(str.substring(1));
break;
case "numeric":
thiscell.setCellType(Cell.CELL_TYPE_NUMERIC);
thiscell.setCellValue(Double.parseDouble(str));
break;
case "text":
thiscell.setCellType(Cell.CELL_TYPE_STRING);
thiscell.setCellValue(str);
break;
}
But when I run over a cell which was originally a formula, say A1/B1, that is #DIV/0! at the moment, setCellType fails.
With much investigation I found out that when setCellType is called, it tries to convert the old content to the new type. BUT, this didn't seem a problem to me, since every table formula cell was already a formula in the xls. Hence, I am never actually changing types.
Even so, when I call setCellType(Cell.CELL_TYPE_FORMULA) on a cell that is already a formula, but it is evaluated to #DIV/0!, I get an conversion exception.
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Cannot get a numeric value from a error formula cell
at org.apache.poi.hssf.usermodel.HSSFCell.typeMismatch(HSSFCell.java:648)
at org.apache.poi.hssf.usermodel.HSSFCell.checkFormulaCachedValueType(HSSFCell.java:653)
at org.apache.poi.hssf.usermodel.HSSFCell.getNumericCellValue(HSSFCell.java:678)
at org.apache.poi.hssf.usermodel.HSSFCell.setCellType(HSSFCell.java:317)
at org.apache.poi.hssf.usermodel.HSSFCell.setCellType(HSSFCell.java:283)
Actually my only workaround is, before setCellType:
if(thiscell.getCachedFormulaResultType()==Cell.CELL_TYPE_ERROR)
thiscell = thisrow.createCell(col);
This IS working, but I lose the original layout of the cell, e.g. its colors.
How can I properly setCellType if the Cell is a formula with evaluation error?
I found this in the mailing list of poi-apache:
There are two possible scenarios when setting value for a formula
cell;
Update the pre-calculated value of the formula. If a cell contains formula then cell.setCellValue just updates the pre-calculated
(cached) formula value, the formula itself remains and the cell type
is not changed
Remove the formula and change the cell type to String or Number:
cell.setCellFormula(null); //Remove the formula
then cell.setCellValue("I changed! My type is CELL_TYPE_STRING now"");
or cell.setCellValue(200); //NA() is gone, the real value is 200
I think we can improve cell.setCellValue for the case (1). If the new
value conflicts with formula type then IllegalArgumentException should
be thrown.
Regards, Yegor
Still, it does feel like a workaround to me. But everything is now working.
cell.setCellFormula(null) before any setCellType should prevent conversion failure, because the first will discard the cached content.

Categories

Resources