Apache poi getCell() returns wrong value - java

So im trying to recive cell value like '150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbol'
In some cases ill get correct (150...) value and in some reciving '78.0'. At first i thouth that i got wrong cell type in my .xls but after some work i found that they are the same. Also calling method getCellType returns me '1' and that is CELL_TYPE_STRING.
In the end its working something like this:
String value1 = getCellValue(row.getCell(0)); --150... correct value
String value2 = getCellValue(row.getCell(1)); --150... correct value
String value3 = getCellValue(row.getCell(2)); --78.0 incorrect value
private String getCellValue(Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING: //similar for other cell types
//getting cell value based on its type
}
}
looking for some advices and tips cause im running out of ideas, maby im missing something?
thats how my excel looks :
enter image description here
p.s. there are many '150...' vals just for testing

You are using getCell(int var1) to get the content in a cell. But it returns the object of Cell class. You should use methods in Cell class to get values in Excel cells. There are methods like
getDateCellValue()
getNumericCellValue()
getStringCellValue()
to get the cell content depending on the CellType. Using CellType, you can decide how to get the cell content.
UPDATE:
If the problem still exists, check the data type of the cell from;
And do the necessary changes in the code to get the correct value.
Small tip: Also you can add a single quote ' at the beginning of the cell content. Then Apache POI will get the cell content as a String. The ' will not become a part of the cell content.

Related

How to set ORC BytesColumnVector value to NULL?

I'm writing an ORC file using Groovy.
One of the columns is a String. The ORC column type is:
.addField("Name", TypeDescription.createString())
The column vector is:
BytesColumnVector vName = (BytesColumnVector) batch.cols[1]
The values to be assigned to vName may include NULLs, but I can't get ORC to write a null value into its data.
Attempting to assign a null value through set(), setValue() or setRef() throws a null pointer error, either at the point of assignment, or when the batch row is written deeper within ORC.
The closest I can get is this:
byte[] b = new byte[0]
vName.setRef (i,b,0,0)
but this puts an empty string into the data file, as shown in the following dump snippet (see the second column, 'Name'):
{"ProductID":355,"Name":"","MakeFlag":false,"StandardCost":0,"Weight":null,"ModifiedDate":"2014-02-08 10:01:36.827"}
Any thoughts on how to set a null string?
EDIT: With the answer to this question, I was able to complete some code to write the contents of a database table to ORC. It may be useful to people searching for ORC-related examples.
https://www.linkedin.com/pulse/orc-adls-polybase-ron-dunn/enter link description here
An empty string is what I use. I don't think there's another way to do it.
Just make sure you mark the column as containing nulls.
Your code would ideally look like this:
BytesColumnVector vName = (BytesColumnVector) batch.cols[1];
byte[] EMPTY_BYTES = "".getBytes(StandardCharsets.UTF_8);
vName.setRef(i, EMPTY_BYTES, 0, 0);
vName.isNull[i] = true;
vName.noNulls = false;

Poi ' characted seem to be added automatically

I use POI 3.1 to generate a xlsx file.
This character ' is added automcally in my cell
cell = row.createCell(0);
cell.setCellValue(atm.getEnvelop());
cell = row.createCell(1);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(atm.getAmount().replace(".", ","));
atm.getEnvelop() and atm.getAmount() are string
atm.getEnvelop() value is 8635 but when i check in the file generated i get:' 8635
same thing for the other one
value is 200,00 i get '200,00
any idea?
Your problem is that you're calling cell.setCellValue(String), which automatically sets the cell type to be a string
If you want to set a numeric cell type (so there's no ' prefixed in Excel), you need to give Apache POI a number not a string.
This code will set '200.00 in Excel:
String s200 = "200.00";
cell.setCellValue(s200);
While this one will give you 200.00 in Excel:
// Once per workbook - tell excel to format with with two decimal points
DataFormat fmt = wb.createDataFormat()
CellStyle cs = wb.createCellStyle();
cs.setDataFormat(fmt.getFormat("0.00"));
// Once per cell
double d200 = 200.0;
cell.setCellValue(d200);
cell.setCellStyle(cs);
For your case, where your number seems to be coming in as a String, you'll need to parser it into a number (eg double) before giving it to POI
AT the place of cell.setCellType(Cell.CELL_TYPE_NUMERIC);
Use cell.setCellType(CELL_TYPE_STRING);
Because putting char '' is excel property for numeric value.
it could help you.
I am not sure but I am guessing you are trying to get an integer from a cell of Cell.Cell_TYPE_STRING. Try to get the amount from cell by calling this : cell.getNumericCellValue()

what is this error and how do I prevent this? The bucket expression values are not comparable and no comparator specified

Im using jasperReports with dynamicReports and I want to build a crosstab report. so far I have figured out that this error happens when I add columns that are numeric to rowGroups or columnGroups. this is what I get and I don't know why and I don't know how to solve this.
The error is:
The bucket expression values are not comparable and no comparator specified
My code is:
CrosstabValues crosstabValues = report.getCrosstab().getCrosstabValues();
Collection<CrosstabRowGroupBuilder> rowGroup = generateRowGroup(crosstabValues);
Collection<CrosstabColumnGroupBuilder> columnGroup = generateColumnGroup(crosstabValues);
Collection<CrosstabMeasureBuilder> measures = generateMeasures(crosstabValues);
CrosstabBuilder crosstab = ctab.crosstab();
for(CrosstabRowGroupBuilder row : rowGroup)
crosstab.addRowGroup(row);
for(CrosstabColumnGroupBuilder columnGroupBuilder : columnGroup)
crosstab.addColumnGroup(columnGroupBuilder);
for(CrosstabMeasureBuilder measure : measures)
crosstab.addMeasure(measure);
crosstab.headerCell(cmp.text(crosstabValues.getHeader())
.setStyle(getCrosstabHeaderCellStyle(report.getTemplate().getReportTemplateValues())));
the problem was the class I was giving to this method:
CrosstabRowGroupBuilder cTabRow = ctab.rowGroup(column.getName()
, getColumnTypeClass(column));
i was using Number class for all numeric data. the funny thing is that it worked for measures but it did not work for rowGroup or columnGroup. that is why I got confused.
now with Integer.Class or Long.Class it works good.
Crosstab must know in which order display rowHeader or columnHeader. And crosstab must know in which cell of crosstab put measure. It is possible only if crosstab is able compare rowGroup (and ColumnGroup) values.
Classes which used in rowGroup and columnGroup must implements Comparable interface

POI reading excel strings as numeric

I am using Apache POI for reading excel file. And while reading it I have noticed that it takes strings as float values.
If my cell contains 1 then it will fetch it as 1.0
I took some hints from previous questions here and modified the code but still the float representation remains as it is.
How would I read correctly the data for strings and dates?
DataFormatter df = new DataFormatter();
for (Row row : sheet) {
for(int cn=0; cn<row.getLastCellNum(); cn++) {
// If the cell is missing from the file, generate a blank one
// (Works by specifying a MissingCellPolicy)
Cell cell = row.getCell(cn, Row.CREATE_NULL_AS_BLANK);
// Print the cell for debugging
cell.setCellType(Cell.CELL_TYPE_STRING);
System.out.println("CELL: " + cn + " --> " + df.formatCellValue(cell));
if (row.getRowNum() == 0) {
sheetColumnNames.add(cell.getRichStringCellValue().getString());
}
}
}
Promoting a comment to an answer
The problem is the call
cell.setCellType(Cell.CELL_TYPE_STRING);
What that is doing is asking POI to try to convert the cell from whatever it is currently (eg a number) into a string. The conversion applied to try to do this is a fairly simple one, which is why you're loosing the formatting
If you just want to get back a String that contains the Cell Value as shown in Excel, just call DataFormatter directly, and it'll do its best. Playing around with the Cell Type will only confuse things, and will risk loosing formatting
Adding to above answer, poi will give you 1.0 as output even when you are using dataformatter class if you are trying to execute program against LIBRE OFFICE spreadsheet. As poi does not work in similar fashion with LIBRE SPREADSHEETS as it with excel.

Error in using getRichStringCellValue() of Apache POI

The following is my java code for reading a excel sheet content.
String urlcnt="";
for (Row row : sheet) {
{
Cell firstCell = row.getCell(0);
urlcnt=firstCell.getRichStringCellValue();}
While compiling the above code am getting the following error.
ReadExcel.java:18: incompatible types
found : org.apache.poi.ss.usermodel.RichTextString required: java.lang.String
urlcnt=firstCell.getRichStringCellValue();//This line is the cause for the error
Instead of storing the getRichStringCellValue() in a string, if I just print the value of the cell, it works fine. But I go ahead and store it in a string for further processing the problem occurs.
Kindly let me know what has to be done to proceeed.
The error is because getRichStringCellValue() returns a HSSFRichTextString or XSSFRichTextString (depending on whether your cell is HSSFCell or XSSFCell) and you are assigning it to a String
Depending on your further processing -
Do you want to call applyFont or clearFormatting on the HSSFRichTextString ?
then store it in a HSSFRichTextString/XSSFRichTextString.
If you actually want only the String text, use the getString() method from the POI API
UPDATE as per your comments
Use it as
urlcnt=firstCell.getRichStringCellValue().getString();

Categories