generate excel sheet using Apache poi with Email Validation - java

I am generating excel sheet using apache poi for email --that means after generating excel if user enter email in the cell i need to validate that email
below is my code
DVConstraint EmailConstraint=null;
DataValidation EmaildataValidation =null;
EmailConstraint=DVConstraint.createCustomFormulaConstraint("AND(FIND("+'#'+",$A$2),FIND(.,$A$2),ISERROR(FIND( ,$A$2)))");---->getting error here only
EmaildataValidation =dataValidationHelper.createValidation(EmailConstraint, addressList1);
EmaildataValidation.setSuppressDropDownArrow(false);
sheet.addValidationData(EmaildataValidation);
when i execute this method i am getting following error
Stacktrace:] with root cause
org.apache.poi.ss.formula.FormulaParseException: Parse error near char 9 '#' in specified formula 'AND(FIND(#,$A$2),FIND(.,$A$2),ISERROR(FIND( ,$A$2)))'. Expected cell ref or constant literal
at org.apache.poi.ss.formula.FormulaParser.expected(FormulaParser.java:188)
at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1128)
at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1083)
at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1070)
at org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1430)
at org.apache.poi.ss.formula.FormulaParser.additiveExpression(FormulaParser.java:1530)
at org.apache.poi.ss.formula.FormulaParser.concatExpression(FormulaParser.java:1514)
at org.apache.poi.ss.formula.FormulaParser.comparisonExpression(FormulaParser.java:1471)
at org.apache.poi.ss.formula.FormulaParser.Arguments(FormulaParser.java:1056)
at org.apache.poi.ss.formula.FormulaParser.function(FormulaParser.java:943)
at org.apache.poi.ss.formula.FormulaParser.parseNonRange(FormulaParser.java:536)
at org.apache.poi.ss.formula.FormulaParser.parseRangeable(FormulaParser.java:416)
at org.apache.poi.ss.formula.FormulaParser.parseRangeExpression(FormulaParser.java:237)
at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1123)
at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1083)
at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1070)
at org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1430)
at org.apache.poi.ss.formula.FormulaParser.additiveExpression(FormulaParser.java:1530)
at org.apache.poi.ss.formula.FormulaParser.concatExpression(FormulaParser.java:1514)
at org.apache.poi.ss.formula.FormulaParser.comparisonExpression(FormulaParser.java:1471)
at org.apache.poi.ss.formula.FormulaParser.Arguments(FormulaParser.java:1056)
at org.apache.poi.ss.formula.FormulaParser.function(FormulaParser.java:943)
at org.apache.poi.ss.formula.FormulaParser.parseNonRange(FormulaParser.java:536)
at org.apache.poi.ss.formula.FormulaParser.parseRangeable(FormulaParser.java:416)
at org.apache.poi.ss.formula.FormulaParser.parseRangeExpression(FormulaParser.java:237)
at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1123)
at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1083)
at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1070)
I tried a lot but not able do it.

First parameter to find function is string hence it must be quoted.
Find("#",..
In java you can use escape string character in between formula.

Related

Apache POI cannot recognize sheet name: Specified named range 'RtSummary.1' does not exist in the current workbook

I am generating Excel file by java. I need to reference "summary" sheet in "overview" sheet. First, I declare the summary sheet name as "RtSummary.1-PLAN1". Then I worked on cells in overview sheet. However, I got error saying:
Exception in thread "main" org.apache.poi.ss.formula.FormulaParseException: Specified named range 'RtSummary.1' does not exist in the current workbook.
I print out the sheet name. It prints exactly what I expect. (It prints "RtSummary.1-PLAN1" instead of "RtSummary.1").
Then I tried to change the sheet name from "RtSummary.1-PLAN1" to "test". I didn't get such error any more.
I am confused what's wrong with my sheet name to cause such error. My code is as below:
//cell 3 (total orders) will be filled later
String summarySheetName="RtSummary."+snapshots[i].trim()+Integer.toString(i+1);
String detailSheetName="RtDetail."+snapshots[i]+(i+1);
Row orderRow;
//first comparison table content
int rowOffset=15+snapshots.length;
curRow=overviewSheet.createRow(rowOffset+i);
orderRow=curRow;
curCell=curRow.createCell(1);
curCell.setCellStyle(lrBarStyle);
curCell.setCellValue(snapshots[i]);
curCell=curRow.createCell(2);
curCell.setCellStyle(lrBarStyle);
if(sameDay)
curCell.setCellValue(dateSDF.format(startDate.getTime()));
else
curCell.setCellValue(dateSDF.format(startDate.getTime())+"-"+dateSDF.format(endDate.getTime()));
curCell=curRow.createCell(4);
curCell.setCellStyle(lrBarStyle);
System.out.println(summarySheetName);
curCell.setCellFormula(summarySheetName+"!N"+(routes.length+2));
System.out.println("0k");

Apache POI: Accounting format (MUST be presented as "Accounting" in Cell Format dialogue)

I need to format input text in excel cell to be styled as "Accounting" cell. I'm using this code:
HSSFCellStyle styleCurrency = workbook.createCellStyle();;
CreationHelper ch = workbook.getCreationHelper();
styleCurrency.setDataFormat(ch.createDataFormat().getFormat("_-* # ##0\\ _\u20BD_-;-* # ##0\\ _\u20BD_-;_-* \"-\" _\u20BD_-;_-#_-"));
I got the regExp argument from excel cell. (I found it here). But when i check cell format i get ('custom') instead of ('Accounting'). The text must be written without currency prefix and must be of 'Accounting' format. How to achieve it? I couldn't find any solution in google.
EDIT:
My above regExp ("_-* # ##0\\ _\u20BD_-;-* # ##0\\ _\u20BD_-;_-* \"-\" _\u20BD_-;_-#_-")
is transfering to: _-* #\ ##0\ _₽_-;-* #\ ##0\ _₽_-;_-* "-" _₽_-;_-#_- in Excel's Cell Format Dialogue...
POI or Excel itself adds slash after first #s.
It must look like this: _-* # ##0\ _₽_-;-* # ##0\ _₽_-;_-* "-" _₽_-;_-#_- (whitespace after first #, not slash)

Unable to create record in MS-Dynamic CRM from mulesoft

Following is what I am doing.
I am using mule MS-Dynamics connector to create a contact
I get records from mysql Database (Inserted from source file)
Transform it to CRM specific object in dataweave
This works for over 10 Million records. But for a few hundred records
I am getting the following error:
Problem writing SAAJ model to stream: Invalid white space character (0x1f) in text to output (in xml 1.1, could output as a character entity)
With some research I found out that (0x1f) represents US "Unit separator".
I tried replacing this character in my dataweave like this
%var replaceSaaj = (x) -> (x replace /\"0x1f"/ with "" default "")
but the issue persists.
I even tried to look for these characters in my source file and database with no luck.
I am aware that this connector internally uses SOAP services.

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()

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