In Apache POI, I'm trying to read an Excel spreadsheet that has a Cell (raw) value of: 0.575
From what I understand, the purpose of the DataFormatter.formatCellValue() method is to return cell's value in the way it is shown in the Excel document.
The Cell format in Excel is Custom: "£"#,##0.00;[Red]-"£"#,##0.00
The way it displays in Excel is: £0.58
when I use:
DataFormatter formatter = new DataFormatter();
String displayValue = formatter.formatCellValue(cell);
I get: £0.57
Why is POI's formatter rounding down the value, when Excel rounds it up?
I've tried:
if( type == CellType.NUMERIC ) {
Format format = formatter.getDefaultFormat(cell);
if( format instanceof DecimalFormat ) {
DataFormatter.setExcelStyleRoundingMode((DecimalFormat)format);
}
}
But the Rounding mode doesn't get set, because the format is ExcelGeneralNumberFormat. One of the members of format is a DecimalFormat (as I can see from the debugger), but it seems I don't have API access to it.
I believe this is related: When I inspect the XML value of the cell, I see:
<xml-fragment r="D1" s="2">
<main:v>0.57499999999999996</main:v>
</xml-fragment>
Which is of course different to the "raw" value I see in Excel (0.575).
If the POI framework is rounding 0.57499999999999996 and not 0.575 then that of course explains why it's rounding down and not up.
The only way to reproduce the behaviour of Excel then, is to Round the raw value to 3dp (0.575) - then round again to 2dp (0.58)
Related
Please, could you advise, how to set up a Conditional formatting related to the MS Excel Pivot Table Data Field (not to the Excel sheet cell !). The Excel pivot table is created via Java Apache Poi. I need a pure Java solution either via the Apache Poi library or via Spire.XLS or Aspose library. I prefer open source solution but if there is no other option I would welcome paid library if the solution works well. I failed searching this via Google Worldwide. Thank you very much. Miroslav
Aspose.Cells for Java can serve your needs. You may add conditional formattings to pivot table fields (row, column, data, etc.).
There are relevant APIs (method overloads, etc.) for the purpose:
PivotFormatCondition.addDataAreaCondition(string fieldName) method
which adds PivotTable conditional format limit in the data fields.
PivotFormatCondition.addDataAreaCondition(PivotField dataField)
method which adds PivotTable conditional format limit in the data
fields.
PivotFormatCondition.addRowAreaCondition(string fieldName) method
which adds PivotTable conditional format range limit in the row
fields.
PivotFormatCondition.addRowAreaCondition(PivotField rowField) method
which adds PivotTable conditional format range limit in the row
fields.
PivotFormatCondition.addColumnAreaCondition(string fieldName) method
which adds PivotTable conditional format range limit in the column
fields.
PivotFormatCondition.addColumnAreaCondition(PivotField columnField)
method which adds PivotTable conditional format range limit in the
column fields.
PivotFormatCondition.setConditionalAreas() method which sets
conditional areas of PivotFormatCondition object.
e.g.
Sample code:
String filePath = "........";
Workbook workbook = new Workbook(filePath + "Book1.xlsx");
//Get the first pivot table in the first worksheet.
PivotTable pivot = workbook.getWorksheets().get(0).getPivotTables().get(0);
PivotFormatConditionCollection pfcs = pivot.getPivotFormatConditions();
//clear all the current conditional formats (if required).
pfcs.clear();
workbook.getWorksheets().get(0).getConditionalFormattings().clear();
//Add new conditional formatting
int pIndex = pfcs.add();
PivotFormatCondition pfc = pfcs.get(pIndex);
pfc.setScopeType(PivotConditionFormatScopeType.FIELD);
//Sample code 1:
pfc.addDataAreaCondition("myDataField1");
pfc.addRowAreaCondition("myRowField");
//Or
//Sample code 2:
// pfc.addDataAreaCondition(pivot.DataFields[2]);
// pfc.addRowAreaCondition(pivot.RowFields[0]);
pfc.setConditionalAreas();
FormatConditionCollection fcc = pfc.getFormatConditions();
int idx = fcc.addCondition(FormatConditionType.CELL_VALUE);
FormatCondition fc = fcc.get(idx);
fc.setFormula1("0.4");
fc.setOperator(OperatorType.GREATER_OR_EQUAL);
fc.getStyle().setBackgroundColor(com.aspose.cells.Color.getRed());
workbook.save(filePath + "out.xlsx");
workbook.save(filePath + "out.pdf");
You may also post your queries or comments in other forums.
PS. I am working as Support developer/ Evangelist at Aspose.
This question already has an answer here:
Regarding HSSF Sheet issue(Invalid row number (65536) outside allowable range (0..65535) exception)
(1 answer)
Closed 2 years ago.
i'm tried 3 type of formats .xls,.xlxs,.xls.
my aim is getting a values from the query and display output as a xls file.
my code is :
filename = "file.xlxs";
workbook = new HSSFWorkbook()
sheet1 = workbook.createSheet("All Files");
rs=stmt.executeQuery("select slno from files");
while (rs.next()){
i++;
HSSFRow row = sheet1.createRow((int)y);
cell = row.createCell((short)0);
cell.setCellValue(rs.getString("slno"));
cell.setCellStyle(cellStyle);
}
i'm getting error like this:
java.lang.IllegalArgumentException: Invalid row number (65536) outside allowable range (0..65535)
my query slno have contain above 96000 numbers
Naming the file .xlxs doesn't change the fact that HSSFWorkbook uses the format of .xls files.
If you wanted to write an .xlsx (not .xlxs) file, which doesn't have that row count restriction, use XSSFWorkbook.
Just like the documentation says:
HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.
BigDecimal myNumber = new BigDecimal(1234.56);
NumberFormat instance = NumberFormat.getInstance(Locale.GERMAN);
String localizedNumber = instance.format(myNumber);
System.out.print("\nformatting " + localizedNumber); o/p ->1.234,56
Till here code works fine but below line gives NumberFormatter exception as given string contains comma in it.
BigDecimal bigDecimal = new BigDecimal(localizedNumber);
I want numeric values to be localized but I cannot return string as putting number as string shows below error in excel
Number in this cell is formatter as text or preceded by an apostrophe
Is there any way by which I can return back numeric value(BigDecimal / Integer / BigInteger etc) in localized format
So that I won't get above error in excel and I can perform filter operations on excel data.
I've also tried new BigDecimalType().valueToString(value, locale); and new BigDecimalType().stringToValue(s, locale); of dynamic jasper reports api.
Happy to answer this question which asked me only and quite surprised that no one replied to this.
Actually we don't have to do anything for number localization in excel export because it's done by our operating system settings automatically.
Go to "Region and Language" -> Format -> Select language/country name -> Apply and check your excel earlier generated in English.
You will see numbers in currently selected country's number format. :)
I am using Apache POI to read the text values from the cells in an excel sheet.
String str = cell.getStringCellValue();
However, when the cell contains text other than English, say , from any foreign language, it does not get stored in the string variable. What am I supposed to do in order to make it compatible with unicode characters as well?
Moreover, can I store them safely in a database using Sqlite?
If you are using eclipse IDE change the encoding to UTF-8.
Project > Properties > Resources > Text file encoding > Other > UTF-8
Use the data formatter as well.
DataFormatter formatter = new DataFormatter();
LOGGER.info(formatter.formatCellValue(cell));
I have a string like "2,345".I want to put it into a excel cell.I successfully did but in my excel file i got "2,345" as a string.So please suggest me how can i get "2,345" as a number value but with the same format as i used above(comma seperated).
Thanks in advance.
Remove the comma before inserting it into Excel, cast it to a number before inserting, then format the column to show the comma.
String replace
In Excel the code to format a Range with commas is:
SomeRange.Style = "Comma" 'or, recorded version
SomeRange.NumberFormat = "_-* #,##0_-;-* #,##0_-;_-* ""-""??_-;_-#_-"
'a simpler version..
SomeRange.NumberFormat = "#,##0"