Hello to all once again :)
I have an ArrayList of Strings which contains various data. It is filled with numbers, decimal numbers, simple strings and so one(but all of them are stored as a Strings).
The problem is that since a while I have been storing those files only as Strings in Excel file using POI library with the following code:
cell.setCellValue(listOfResults.get(iterationNumber));
Right now I have to face another problem. From time to time the results of this ArrayList are Float numbers with format *,* or Integers and, as you can see, floats numbers are separated by a comma i.e:
0
1
1,23
213,23899
For above data I have to set a cellType to NUMERIC(not as previously as general). So I was trying with this:
CellStyle style = wb.createCellStyle();
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("0,00"));
cell.setCellStyle(style);
cell.setCellValue(Float.parseFloat(listOfResults.get(iteration)));
//after this I have a negative output with the following Exception
java.lang.NumberFormatException: For input string: "2,26776"
And I know that the problem is connected with this comma value.
So please give me a hint in these two areas:
1.
HSSFDataFormat.getBuiltinFormat("0,00")
how to set it properly that it should be working for my example data ["0", "1", "1,23","21312,23999"] and so one
2.
cell.setCellValue(Float.parseFloat(listOfResults.get(iteration)))
How to properly parse a float with "," inside. I have tried with DecimalFormatSymbols but it doesn't work like I want with POI.
Thanks a lot in advance!
I think you could just write the comma formatted floats into your excel file as a string. For that you do:
cell.setCellValue(listOfResults.get(iteration));
If you intern to use the cell value for further calculations as numeric values, then I think you only have to convert the string number to a float.
float value = Float.parseFloat(listOfResults.get(iteration).replace(",", "."));
cell.setCellValue(value);
Related
When I am reading excel numeric cell values, i am getting the output with decimals. eg: 79 is reading as 79.0, 0.00 reading as 0.0. The code for my application I have written is:
int type = cell.getCellType();
if (type == HSSFCell.CELL_TYPE_NUMERIC)
System.out.println(cell.getNumericCellValue());
This question comes up a lot on Stack Overflow, so you'd be well advised to look through many of the similar ones to see there answers!
Excel stores almost all numbers in the file format as floating point values, which is why POI will give you back a double for a numeric cell as that's what was really there. If you want it to look like it does in Excel, you need to apply the formatting rules defined against the cell to the number. Thus, you want to do exactly the same thing as in my answer here. To quote:
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.
All you need to do is:
// Only need one of these
DataFormatter fmt = new DataFormatter();
// Once per cell
String valueAsSeenInExcel = fmt.formatCellValue(cell);
I'm not 100% this would work but I believe what you're looking for is DecimalFormat.
I assume you're using Apache POI.
You should consider playing with DataFormats. Here is some very minimal draft code.
Otherwise, if the data in your work book is consistent, you might want to round the double returned by getNumericCellValue to int.
System.out.println((int)Math.round(cell.getNumericCellValue()));
Use the DataFormatter as the following, it will detect the format of the cell automatically and produce the string output
DataFormatter formatter = new DataFormatter();
String df2 = formatter.formatCellValue(cell);
still people are facing issues with this can use this
https://poi.apache.org/apidocs/dev/org/apache/poi/ss/util/NumberToTextConverter.html
NumberToTextConverter.toText(cell.getNumberiCellValue())
I am using Apache POI 3.9 for XLS/XLSX file processing.
In the XLS sheet, there is a column with numeric value like "3000053406".
When I read it with POI with..
cell.getNumericCellValue()
It gives me value like "3.00E+08". This create huge problem in my application.
How can I set the number formatting while reading data in Apcahe POI ?
There is a way that I know is to set the column as "text" type. But I want to know if there is any other way at Apache POI side while reading the data. OR can we format it by using simple java DecimalFormatter ?
This one comes up very often....
Picking one of my past answers to an almost identical question
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.
Problem can be strictly Java-related, not POI related, too.
Since your call returns a double,
double val = cell.getNumericCellValue();
You may want to get this
DecimalFormat df = new DecimalFormat("#");
int fractionalDigits = 2; // say 2
df.setMaximumFractionDigits(fractionalDigits);
double val = df.format(val);
Creating a BigDecimal with the double value from the numeric cell and then using the
BigDecimal.toPlainString()
function to convert it to a plain string and then storing it back to the same cell after erasing the value solved the whole problem of exponential representation of numeric values.
The below code solved the issue for me.
Double dnum = cellContent.getNumericCellValue();
BigDecimal bd = new BigDecimal(dnum);
System.out.println(bd.toPlainString());
cellContent.setBlank();
cellContent.setCellValue(bd.toPlainString());
System.out.println(cellContent.getStringCellValue());
long varA = new Double(cellB1.getNumericCellValue()).longValue();
This will bring the exact value in variable varA.
i have a problem. When i am writing a double value (say 20.3) in an excel cell and specify a format ("0.0") the "german" Excel does not recognize the value as a number, because in Germany the separator for decimal numbers is a comma, not a point. For example the "english" number 1,000.00 would be 1.000,00
I tried it with a DateFormat like "0,0", but excel still thinks the value is a string.
I need the value to be a number, because of formulas.
And: The clients of my software use Excel with german number format. There is no dicussion about it :(
Is there a way to say POI to write a double value "the german" way?
Or is there any workaround for this problem?
Thanks a lot!
This shouldn't be a problem because in POI the setCellValue(double) method is overridden for double and thus independent of locale settings. It looks like you are using setCellValue(String) or setCellValue(RichTextString). Correct that and it should work.
If that doesn't work for you, you have to post your code.
When I am reading excel numeric cell values, i am getting the output with decimals. eg: 79 is reading as 79.0, 0.00 reading as 0.0. The code for my application I have written is:
int type = cell.getCellType();
if (type == HSSFCell.CELL_TYPE_NUMERIC)
System.out.println(cell.getNumericCellValue());
This question comes up a lot on Stack Overflow, so you'd be well advised to look through many of the similar ones to see there answers!
Excel stores almost all numbers in the file format as floating point values, which is why POI will give you back a double for a numeric cell as that's what was really there. If you want it to look like it does in Excel, you need to apply the formatting rules defined against the cell to the number. Thus, you want to do exactly the same thing as in my answer here. To quote:
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.
All you need to do is:
// Only need one of these
DataFormatter fmt = new DataFormatter();
// Once per cell
String valueAsSeenInExcel = fmt.formatCellValue(cell);
I'm not 100% this would work but I believe what you're looking for is DecimalFormat.
I assume you're using Apache POI.
You should consider playing with DataFormats. Here is some very minimal draft code.
Otherwise, if the data in your work book is consistent, you might want to round the double returned by getNumericCellValue to int.
System.out.println((int)Math.round(cell.getNumericCellValue()));
Use the DataFormatter as the following, it will detect the format of the cell automatically and produce the string output
DataFormatter formatter = new DataFormatter();
String df2 = formatter.formatCellValue(cell);
still people are facing issues with this can use this
https://poi.apache.org/apidocs/dev/org/apache/poi/ss/util/NumberToTextConverter.html
NumberToTextConverter.toText(cell.getNumberiCellValue())
I have a CSV (Comma Separated Value) file that has a mixture of categorical, and numerical data.
I would like to be able to determine whether the data is either categorical or numerical, so I can programatically plot the data in a scatter plot that I created in Swing.
Any ideas on how to achieve this in Java? I'm looking for approaches--not code.
Use Double.parseDouble() or Integer.parseInt() to check for numerical data.
Both methods take a String as an argument and return a double or an int primitive. These methods will throw a NumberFormatException if the string you pass is not numerical.
I see that you have fixed column ordering, as described in your comment. In this case, you already know which data is numerical and which is categorical by its position in the line. So only be sure to parse numerical values on the columns that expect numerical values. If you catch this exception, it means your data file is malformed or you have a bug in your parsing logic. Of course, you have to strip out the dollar sign.