Apache POI VLookup substiture - java

I have a excel in which a column A has values with ~ characters 'Total Assets (~)'
When Column A is selected, excel will automatically populate column b with corresponding id
using formula VLOOKUP(SUBSTITUTE(E2,"~","~~"),Sheet2!A:B,2,FALSE)
I have written substitute to overcome ~ character issue.
But if I try to upload the above excel for processing using Apache POI to process
getting ERROR as cellType. Wrote below code to process
XSSFCell vCell = vRow.getCell (2);
String vCellValue = vCell.getCellFormula ();
FormulaEvaluator evaluator = vWorkbook.getCreationHelper().createFormulaEvaluator();
CellType cellType = evaluator.evaluateFormulaCell(vCell);
But unfortunately when evaluator process the cell, its returing cellType as ERROR.
Any ideas.

Related

Evaluate cell which uses named field in its formula - Apache POI

I'm having a problem evaluating excel sheet cell which uses named cell in its formula. I'm using apache-poi version 3.17, java 1.8 and the file I'm working with is .xlsm. Here is an example code:
//Getting the excel file from the database
Document doc = documentRepository.findByDocumentType(DocumentType.EXAMPLE);
InputStream fis = new ByteArrayInputStream(doc.getFileData());
Workbook wb = new XSSFWorkbook(fis);
Sheet mappingSheet = workbook.getSheet("Quote services");
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
// This cell contains a formula which uses named cell
CellReference cellReference = new CellReference("A109");
Row row = mappingSheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol());
CellValue cellValue = evaluator.evaluate(cell);
This gives me the following error:
org.apache.poi.ss.formula.FormulaParseException: Specified named range
'Discount' does not exist in the current workbook.
The excel formula looks something like that:
=Discount*A7
Where Discount is A5 and if I change the formula to look like this:
=A5*A7
it works fine.
This is kind of strange cause if I try to get the named field Discount it finds it, but if I use evaluate on a cell which uses the field it doesn't.
The document is predefined so I can't change anything in it. If I name the given discount cell with poi and with the same name there is no problem, but I don't want to do this cause there are more named fields in the document and if something change I have to change the code. Any help will be appreciated!
I found the problem! It was that the Discount field was referenced from a hidden sheet in the document and it was referenced like this: Discount instead of 'Quote services'!Discount.
Are you sure that Discount is indeed within the Workbook that you are evaluating? The message is pretty clear.
Can you show exactly how the formula looks like - is it something like this:
=SUM('C:\Data\[Finances.xlsx]Discount'!D10:D25)
Try setIgnoreMissingWorkbooks(true), and if it works, then the Discount is set in another workbook. setIgnoreMissingWorkbooks.

Cant find merged cell in excel with java

I am trying to retrieve a value from a cell in excel but it is merged. I am using Java
This is what i have:
ByteArrayInputStream bxf = new ByteArrayInputStream(entity.getAttachment().getFile());
Workbook wb = WorkbookFactory.create(bxf);
Sheet sheet = wb.getSheetAt(0);
Row cpRow = sheet.getRow(4);
Here is the example of the excel:
Red Block is a merged cell :
edit
I am using apache.poi library
Reason why you getting a null pointer are that the value you are looking at currently is the actual row 5 on sheet.
As stated by #Berger in the comment the 'getRow is 0 based'
Use getRow(3) instead.
Please see https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFRow.html
aswell as
it states https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html
HSSFRow getRow(int rowIndex)
Returns the logical row (not physical) 0-based.

How to read the values of a dropdown using Jxl API

I have an Excel sheet in which 2 columns have a dropdown with one about 4-5
values and the other with 2 values that can be selected. I would like to read
the value that is there for each row in these two columns. . What is the code
sample to do that? I did browse a while on the net and this forum, but did not
find any answers. I posted in the JExcel Yahoo groups , but no success.
I have added the following code, but this does not help. This code sample just prevents the "common Assertion failed" error
WorkbookSettings settings = new WorkbookSettings();
settings.setSuppressWarnings(true);
Workbook workbook = Workbook.getWorkbook(sis, settings);
The sheet gives an erroneous row count when the dropdown is there. Has anyone been able to read the values selected in the drop down ?
If you have applied an autofilter to your Excel sheet then probably that is responsible for the issue. I would suggest you to please remove the autofilter and then try fetching the value of the combobox.
Cell yourCell = yourSheet.getCell(x,y);
String comboboxValue = yourCell.getContents();
logger.log("value selected in combobx is : " + comboboxValue );

POI - SXSSFWorkbook - Excel found unreadable content

I have an existing workbook that acts as template. I tried to update a cell value in an existing row, meaning the adjacent cell has a value. The problem is that after the file is created and I open it, I get the following error:
"Excel found unreadable content in...."
I assume the reason is SXSSFWorkbook only handles writing and if a row exists it cant update the contents of the row, which would entail reading and then writing - is this correct or am I experiencing a bug?
Thanks
Another issue is that if you're using addMergedRegion, the CellRangeAddress values for rows and columns have to be in ascending numerical order:
// Note that rows and columns for CellRangeAddress constructor have to be in ascending order
// The commented out line below will generate an error when opening the sheet:
// sheet.addMergedRegion(new CellRangeAddress(lastRow, lastRow-(rowsTobeCreated-1), 0, 0));
// In order this call works:
sheet.addMergedRegion(new CellRangeAddress(lastRow-(rowsTobeCreated-1), lastRow, 0, 0));
In case anyone else has this problem, check to make sure the length of your SheetName is not too long. I was getting this error when setting the workbook sheet name longer than around 30 characters.
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
wb.setSheetName(0, "Supporting Documentation"); // make sure this is not too long

Stuck inserting rows from a file into Database

I want to insert rows from excel sheet into SQL database using java or JSP. Initially I have connected both the database using ODBC. But I want to take an excel file as input from user. For this I created a file browser and upload the file.
How can I connect the uploaded file with ODBC connection?
How can I query the uploaded file?
If I use Apache POI to parse Excel file, I will get column names and that also get inserted. How can I avoid this?
a) How can I insert the rows with particular column name, not from column number?
Please provide me the code snippet
1) How can i connect the uploaded file with ODBC connection?
You will have to write a service to map excel to DB if thats what you are asking?
To get the values from the excel file , you should use Apache POI.
And the solution to the problem that column names are also inserted is :
If the column names are in row 1 , then start a loop from 1 instead of 10.
int i=1;
do{
{
HSSFRow row1 = worksheet.getRow(i);
HSSFCell cellA1 = row1.getCell((short) 0);
String a1Val = cellA1.getStringCellValue().toString();
System.out.println(a1Val);
i++;
// And if you want to enter this value in sql database , write a odbc connection and insert query to sql database.
}
}while(a1val.!equals("");

Categories