How can I output id number in excel without error warning - java

I am using POI to output a product id into excel cell. The product id is a numeric string like
"0000001", "3212230".
After the excel is generated, I found the cell holds this id value has an error check warning "The number in this cell is formated as text or preceded by an apostrophe". If I double click on this cell in excel, it will automatically correct "0000001" to 1, which is not what I want. I know I can turn the error checking option off in excel manually, but that's not what I want.
Is there anyway to keep excel quiet and considering this id as a text?
Here is the code snippet:
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new HSSFRichTextString( quote.getQuoteId() ));
cell.setCellStyle(styles.get("table_t"));

It seems this is not supported yet by POI. However, the low level implementation seems to be done as part of bug 46136. This bug is waiting for suggestions for specifying an interface for this feature so I'd say the best to get this done one day would be to contribute your remarks and maybe vote for it. I just voted right now.

Can you precede it with a single quote?
cell.setCellValue(new HSSFRichTextString("'" & quote.getQuoteId() ));

Not really. Excel really loves its numbers which is why that is considered an error. About the only thing that would make Excel happy would be to change your QuoteId format to start with a letter instead of being completely numeric.

Related

Add a formula to Excel cell using Apache POI XSSF workbook

I must add formulas to an Excel worksheet and I found out this is done in the following way:
String strFormula= "SUM(A1:A10)";
cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula(strFormula);
My questions are:
It may happen that the users use different language versions, means on an English system the formula will be "SUM(A1:A10)" but on a German system will be "SUMME(A1:A10)".
How can I distinguish or don't I have to distinguish at all?
How can I use the column- and row-index I'm using in the code also for the formular instead of the column letters? Means: How can I express the row index 1 without having to find out that is a row B ?
Thank you for your support !
meanwhile I could find out the answers, maybe it help anyone else:
It works to add the formula with the English syntax, even if the System the formula was added is running in German language.
The Excel address used in the Excel frontend like "A1" can be taken from the Cell with yourCell.getaddress() and be formated as String with yourcell.getaddress().formatAsString()

JAsper Report 3.0 issue :

I am using jasper Reoprt Ireport 3.0 for Generating excel. my problem is I have One text field in that i want to keep below data which is coming from my database.
**Disclaimer: Please note that in case of disease conditions which can have both Acute and Chronic manifestations, they are considered as Chronic illness for analysis keeping in view the objective of the report.
Actually in jasper reoprt summary area i have given one text field but it is too small too keep this above line data. but still I want to keep whole data in small field in excel .
because normaly when we are typing in excel cell anything and when it become overflows then also it's looking like it has been typed in next cell but ,when we are clicking in another cell it will show in that cell only.whole data will come in that small cell without wrapping text filed.
just like it i want also through I-report 3.0 . I tried so much but I didn't find solution so i am posting here . please suggest if you faced same problem and got solution too. it will be helpful.
I don't want to merge it in multiple columns because if it will merge multiple columns then for calculating value through Excel column selection will be a problem. and V lookup we cant do so.
if the data in any cell is greater than 2 rows, then the remaining data just gets cut off and not displayed.
Maybe it is time to update. At this time JasperReports 6.x is the version to go, but ...
From my expierience JasperReports is the wrong tool to generate "clean" Excel documents. You get always layout informations in it that result in the behaviour you describe.
To get a clean Excel document I would suggest to go with Apache-Poi (https://poi.apache.org/) and generate it directly or simply to generate a CSV - file which can be easily opened using Excel.

Java, Apache POI, cell value won't change when seen from MS Excel

I'm using Apache POI to modify an existing Excel file (something.xls).
After I set a cell value using:
sheet.getRow(3).getCell(30).setCellValue(222);
I can see the changes reading the value from the same cell with:
sheet.getRow(3).getCell(30).toString();
BUT when I'm opening the something.xls file with MS Excel the change is gone.
Now the funny part:
-If I'm rerunning the code without the line where I set the cell value, I can still see 222, so the change is there, only I see the
old value from MS Excel for some reason.
-I triple checked to make sure I'm not editing a different file from code and opening another with Excel.
-I tried changing some other random cells in a similar way and some work, while others do not. (the cells do not contain formulas and all
are numeric type)
I would really appreciate if anyone could offer an explanation and solution.
Thanks in advance.
I copied the contents of the existing excel file (manually, without any formatting) to a new one and formatted it again. It is working now. I must have set something wrong the first time, I couldn't figure out what though. Thanks for the replies.

Apache Poi formula Strings

I have a strange behavious and hope that someone might know what is going on.
For a personal project in Java i'm using Apache poi (version 3.9) to read and write to excel files.
In these excel files there is a formula i wanted to change to another way writing it.
I have a loop that sets my Excelobject with the required formula string
excelobject.setDataFormula("SUM(L" + counter + "-6,75)"); // it will look like SUM(L2-6,75) and so on
However when i write these formula in a file and check it. it has mysteriously changed to something like SUM(L2-6;75). changing the , to a ; and thus the formula does not work like intended.
can someone explain to me why apache poi setFormula on a cell does this to a , ?
EDIT :
I changed my loop to use a double 6,75 instead of a string 6,75 and that seems to help when creating the formula. So this immediate question is fixed though I am still curious on why this behavious comes.
Go to Control panel -> Region and Language -> Additional settings and change the List Separator to something other than a comma. You should use a character that doesn't appear in your file. Be sure to pick something that won't be added to the file in future either.
It changes from , to ; because , is being used as a delimiter. Anywhere that a , is found indicates a new cell. When your string is read, it changes the comma so that 2 cells wont be created where there should only be one. If you change the List Separator as described, the comma should remain unchanged.

How to determine whether a spread-sheet cell contains a date, or a real number?

When a cell in a spreadsheet contains a simple date (mm/dd/yyyy) the poi API flags the cell-type as 'numeric'. This is probably because spreadsheets ( IMO ) historically recognize only strings and real numbers.
It is possible to hard-code the cell-index, and use it conditionally to call 'getDateCellValue'. But this feels like a hack.
What other ways are there in the poi API to determine whether the content in a cell is a Date rather than a real number?
The answer is in the Getting the Cell Contents section of the HSSF Quick Guide

Categories