Evaluate formula after replacing dependent cells using Apache POI - java

I am able to both read and write cell values using Apache POI. When reading, I evaluate first so that I get the correct values. My use case requires that I read a sheet, replace a few values in the sheet, then read another portion of the sheet that contains cells that depend on the cells I just replaced.
Example.
A1 contains a formula: =B1+C1. B1 contains 2 and C1 contains 3. When I evaluate A1 I correctly get 5. Now, if I replace, with POI api, C1 with 10, I would expect that when I read A1 again I would see 12. I don't... A1 now evaluates to null.
Help!

It seems like your question is answered in Apache POI documentation (under 'Recalculation of Formulas')
Basically it suggests something like:
Workbook wb = ...
wb.getCreationHelper().createFormulaEvaluator().evaluateAll();

Related

EasyXls getValue() which returns the result of the formula

I'm trying to import an Excel file (using easyxls) which contains cells with formulas like '=3*5',
now I would like to get 15 as the value of the cell during the import.
Does anyone know how to get the formula result and not the formula itself from the excel cells?
What I tried so far
cell.getValue().. returns the formula for the formula cells, not the result
cell.getFormulaResultValue() ... this is what I thought should work, but it always returns '#N/A'
cell.getFormulaResultValue() is the method that you need to use, but you need to calculate the sheet first with worksheet.easy_computeFormulas() method.
table.easy_getCell("A1").setValue("=3*5");
worksheet.easy_computeFormulas(workbook, true);
table.easy_getCell("A1").getFormulaResultValue();
Check also this code sample about get formula result in Excel cell with EasyXLS.

csvWriter write long number in cell in Java

While writing the excel file is fine I see that really long numbers are scientific notations in excel
Example: 8.71129E+12
instead of: 1234567890
How can I do it in Java
I am writing like
String nart = "1236547865452";
csvWriter.append(nart);
Not sure how the same can be achieved using writing to simple CSV? ,
but through APACHE POI You can write to excel and you need to do set the cell type if you want to see the whole value like 1236547865452 cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); and this is not needed it you have to see scientific notation as 8.71129E+12

docx4j / xlsx4j : create simple spreadsheet

I want to create a simple spreadsheet in docx4j / xlsx4j. It shall contain only Strings, no formular is needed. The porpuse is basically switching from a CSV to XLSX
Therefore I tried the example here: https://github.com/plutext/docx4j/blob/master/src/samples/xlsx4j/org/xlsx4j/samples/CreateSimpleSpreadsheet.java
Unfortunetly it is not working. Even after removing the deprecated parts ( http://pastebin.com/bUnJWmFD ).
Excel reports unreadable content and suggest a repair. After that I get the error: "Entfernte Datensätze: Zellinformationen von /xl/worksheets/sheet1.xml-Part". It means something like "removed datasets: Cellinformation at /xl/worksheets/sheet1.xml-Part".
This error occures when createCell is called in line 58 (see. Github, not pastebin) or cell.setV is called with "Hello World" instead of "1234"
I think you are raising 2 issues here:
the resulting XLSX needing repair: this was the result of a typo in cell2.setR, fixed at https://github.com/plutext/docx4j/commit/7d04a65057ad61f5197fb9a98168fc654220f61f
calling setV with "Hello World", you shouldn't do that. Per http://webapp.docx4java.org/OnlineDemo/ecma376/SpreadsheetML/v.html
This element expresses the value contained in a cell. If the cell
contains a string, then this value is an index into the shared string
table, pointing to the actual string value. Otherwise, the value of
the cell is expressed directly in this element. .. For applications
not wanting to implement the shared string table, an 'inline string'
may be expressed in an <is> element under <c> (instead of a
<v> element under <c>),in the same way a string would be
expressed in the shared string table.
though I guess our setV method could detect misuse and either throw an exception or do one of those other things instead.
The CreateSimpleSpreadsheet sample as it stands shows you how to set an inline string, so you just need to test whether your input is a number or not.

Apache POI formulas not evaluating

So I'm having some issues getting Apache POI to evaluate formulas.
Here's the code I call to evaluate formulas before writing:
complete.getCreationHelper().createFormulaEvaluator().evaluateAll();
complete.write(fileOut);
Here's the code I call to write to the cells being used (proving they're numbers):
try{
cell.setCellValue((Double)grid[i][j]);
}
catch(Exception e){
cell.setCellValue((String)grid[i][j]);
}
FYI: grid is a 2D Object array containing only entries of the type double and String.
Here's the formulas I'm trying to evaluate:
"=G13 - H13"
"=STDEV.P(C1:L1)"
"=I13/G13"
Any ideas why when I open up my final workbook in Excel the formulas arn't evaluated? Also, when I click on an unevaluated field and hit enter Excel will recognize the formula and evaluate it. In bulk this isn't practical, but I believe it demonstrates that the cells being used are the correct type. Could this be related to the formulas being of the String type?
EDIT:
OK, so looks like you're supposed to explicitly tell it you have a formula cell. Here's my modified code to do that:
try{
cell.setCellValue((Double)grid[i][j]);
}
catch(Exception e){
String val = (String) grid[i][j];
if (val != null && val.startsWith("=")){
val = val.replaceAll("=", "");
cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula(val);
}
else{
cell.setCellValue(val);
}
}
Unfortunately you need to remove the equals sign (which is dumb) to pass formulas and then force it to reevaluate before saving (which is dumb). After trying to get it to reevaluate formulas it complained, however, saying:
Caused by:
org.apache.poi.ss.formula.eval.NotImplementedFunctionException:
STDEV.P
I'm imagining that this means Excel has implemented standard deviation calculations but POI hasn't caught up yet?
Try this:
XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);
or, if you are using xls
HSSFFormulaEvaluator.evaluateAllFormulaCells(hssfWorkbook)
You probably want to call this just before saving.

Writing a formula to a cell with OpenXLS

I'm using Java and OpenXLS to write out an Excel spreadsheet. I want to set a formula for a cell but I haven't got a clue how to do it. Can anybody help me, please? :)
(Can't tag this with "openxls" because I'm a new user...)
I don't know about OpenXLS, but it's easy to do with Andy Khan's JExcel. I'd recommend trying it. I think it's far superior to POI; I'm betting that it's better than OpenXLS as well.
OpenXLS support very well formulas. Look at this example.
I put a value in the columns A and B of a sheet named "testSheet". In the column C of the same sheet I put the result of SUM (A+B).Don't forget to initialise the column C else you will have a CellNotFoundException
WorkBookHandle workbook = new WorkBookHandle();
workbook.createWorkSheet("testSheet");
WorkSheetHandle sheet = workbook.getWorkSheet("testSheet");
for (int i=1 ;i<=10; i++)
{
sheet.add(10*i, "A"+i);
sheet.add(15*i, "B"+i);
CellHandle cx = sheet.add(0,"C"+i);
cx.setFormula("=SUM(A"+i+":B"+i+")");
}
I hope that that this example will help other people.
Ultimately it turned out that OpenXLS doesn't support formula cells. They are included in the paid for version, though...
You can set the formula String directly on the cell in the Worksheet:
CellHandle cell = ws.add( "=SUM(A1:A3)", "A5" );
This adds the SUM(A1:A3) formula in cell A5. Any Cell set with a String value that is prefixed with '=' is considered a Formula.
Updates and maintenance are now happening on github (search for openxls).

Categories

Resources