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.
Related
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.
Now I am successfully setting the number cell type using POI. For example, #,##0.00 formats as 1,343.23.
However, when I want to make #,##0.#####, I get 1343.23000, here the thousand separator "," disappeared. How can I show like this 1,343.23000?
doubleStyle = workbook.createCellStyle();
doubleStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.#####"));
doubleStyle = workbook.createCellStyle();
doubleStyle.setDataFormat(workbook.createDataFormat().getFormat("#,##0.#######"));
I had to set my format.
The problem is very concrete: using Apache POI, I want to create cell (done), assign number format to it (done), and set format's locale (stuck here).
The code looks like this:
SXSSFWorkbook workbook = new SXSSFWorkbook(100);
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(1);
Cell cell = row.createCell(0);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(8); //currency with thousands separator and two decimal places after period
cell.setCellValue(123456.78);
//??? how to set custom locale for the cell's number format?
The problem that I'm trying to solve with custom locale is customizing thousands separator char (French's non-breaking space is OK for me).
XLSX workbooks allow such customization (update: I mean setting format locale per cell), this is achievable with both MS Office and OpenOffice. I want to do the same in code.
(Apache POI 3.12)
In Offixe OpenXML (*.xlsx) for currency number format only the currency symbol can be localized but not the decimal separator. The decimal separator comes from Windows system locale settings of the Windows system the Excel is running on. And the thousands delimiter also defaults to the Windows system locale settings of the Windows system the Excel is running on.
In Excel this looks like:
As you see only the currency symbol can be localized.
At least the thousands delimiter can be set using a format string. So a format string could be
"#\\ ###\\ ##0.00\\ [$€-40C];[RED]\\-#\\ ###\\ ##0.00\\ [$€-40C]".
This is currency number format having localized french Euro currency symbol and space as the thousands delimiter. Because we are faking the thousands delimiter, we have to give as much digits as needed in the format string.
The decimal separator is the default, which means it comes from Windows system locale settings of the Windows system the Excel is running on. So the dot . within the format string does not means to always use a dot as decimal delimiter but to use the decimal delimiter which comes from the Windows system locale settings of the Windows system the Excel is running on. And if we would had used comma , as the thousands delimiter in the format string, this also would had used the thousands delimiter which comes from the Windows system locale settings of the Windows system the Excel is running on. And then we would had not need giving so much digits in the format string because then the thousands delimiter settings would repeat every thousands digits. So
"#,##0.00\\ [$€-40C];[RED]\\-#,##0.00\\ [$€-40C]"
would be enough.
Example:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.streaming.SXSSFSheet;
public class CreateExcelNumberFormat {
public static void main(String[] args) throws Exception {
SXSSFWorkbook workbook = new SXSSFWorkbook(100);
DataFormat dataformat = workbook.createDataFormat();
CellStyle cellStyleCurrency = workbook.createCellStyle();
cellStyleCurrency.setDataFormat(dataformat.getFormat("#\\ ###\\ ##0.00\\ [$€-40C];[RED]\\-#\\ ###\\ ##0.00\\ [$€-40C]"));
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(1);
Cell cell = row.createCell(0);
cell.setCellValue(123456.78);
cell.setCellStyle(cellStyleCurrency);
((SXSSFSheet)sheet).trackColumnForAutoSizing(0);
sheet.autoSizeColumn(0);
workbook.write(new FileOutputStream("CreateExcelNumberFormat.xlsx"));
workbook.close();
workbook.dispose();
}
}
But this is not the same as the localized currency format which is usable in Libreoffice OpenDocument Spreadsheet format. This looks like:
As you see here both, the currency symbol and the language of the whole format, can be localized.
But it is that the Office OpenXML (*.xlsx) cannot store localized currency number formats. OpenDocument Spreadsheet (*.ods), which is the native format of OpenOffice/Libreoffice, can save localized currency number formats, but if Excel will open such a file, the localization will be lost.
The settings of the "Language" combo-box of OpenOffice/Libreoffice cannot be stored in *.xlsx, also not from OpenOffice/Libreoffice. Set something else than the default there in OpenOffice/Libreoffice, save the file as *.xlsx, close OpenOffice/Libreoffice, open the stored *.xlsx file in OpenOffice/Libreoffice again. You will see the "Language" is reseted to the default.
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()
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();