Identify bullet points in MS Excel using Apache POI - java

I am reading columns in MS Excel using Apache POI but the code is not able to identify bullet point. I am trying this:
String cellValue = cell.getStringCellValue();
if(cellValue.contains("•")){}
but this is not working. the cellvalue is giving some garbage value in case of bullet point and this is why it is not able to compare in my if condition.
Can anyone please suggest solution ?

You need to use cell.getRichStringCellValue().getString() and then check for \u2022 which is unicode for "•"

Related

Reading excel using POI HSSFCell with leading zeros [duplicate]

This question already has answers here:
How can I read numeric strings in Excel cells as string (not numbers)?
(24 answers)
Closed 8 years ago.
I am trying to read an excel file using Java POI HSSF. Everything was working fine, except that when the value is 001001 the HSSFCell will return 1001.0
Is there any way I can use HSSFCell to get the value 001001 ?
I am not supposed to do any modification to the excel file.
Thank in advance for any help and suggestion.
Edit
I have been using the following code:
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.toString();
I also ran a debug mode and checked on the cell value the moment the HSSF grabs it. It truncate the leading zeros and converted it to double therefore I do not see a way to retrieve the truncated zeros. There is a link that stated it could be a bug from HSSF:
http://osdir.com/ml/jakarta.poi.user/2003-02/msg00007.html
By the way, I solved it by hard coded it. The number of digits are know in advanced. The link for the code that I used: How to format a Java string with leading zero?
Can you try reading it as a string instead of Numeric,
cell.setCellType(Cell.CELL_TYPE_STRING);
Changing the cell type usually will not modify the contents of the cell.
it can be retrieved with either of the following approaches:
cell.getStringCellValue();
cell.getRichStringCellValue().getString();

Evaluate formula after replacing dependent cells using Apache POI

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

Apache POI formula cell duplication very slow

I'm generating an excel file using Apache POI 3.8 , and have the need to replicate some existing row n° times.
This because I have some complex formula which I use as a template to create new lines, replacing cell indexes with regexps.
The problem is that performance are awful, it takes 2h to generate some 4000 rows.
I've pinpointed the problem to be not in the regexp part, as I initially thought, but in the duplication of formula cells.
I actually use this to replicate formula cells:
case Cell.CELL_TYPE_FORMULA:
newCell.setCellType(oldCell.getCellType());
newCell.setCellFormula(oldCell.getCellFormula());
break;
If I copy the formula as text like this:
case Cell.CELL_TYPE_FORMULA:
newCell.setCellType(Cell.CELL_TYPE_STRING);
newCell.setCellValue("="+oldCell.getCellFormula());
break;
it's instead pretty fast, even with my regexp in place.
Anyway, this is an imperfect solution, because the formula has english keywords (ie IF()), when I need to write in italian format.
More, cells with formula inserted like that need to be forcefully re-evaluated in excel with something like "replace all -> '=' with '='".
The problem seems to rely in the setCellFormula(), because of the HSSFFormulaParser.parse().
What's strange, is that parsing time seems to grow exponentially:
100 rows -> 6785ms
200 rows -> 23933ms
300 rows -> 51388ms
400 rows -> 88586ms
What it seems, is that each time I copy a formula, the POI library re-evaluates or re-parses or re-something all preceding rows.
Do anyone know how can solve this problem?
Thanks in advance.
Oh my...I think I found it...
Original was:
// If the row exist in destination, push down all rows by 1 else create a new row
if (newRow != null) {
worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
} else {
newRow = worksheet.createRow(destinationRowNum);
}
I've commented everything leaving only
newRow = worksheet.createRow(destinationRowNum);
And now I'm down to 60sec to process all rows!
Probably, there's some dirt in my template which was causing POI to shift everything at each iteration.

Recalculating formulas in spreadsheet using Apache POI

I'm trying to use POI XSSF to evaluate some Excel formulas.
The values do not have to be saved, and I may have to calculate many formulas, so I'm trying to do it all in the same cell.
The problem is that the cell value seems to get stuck on the first formula entered even after I recalculate
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
XSSFCell formulaCell = row.createCell(6);
formulaCell.setCellFormula("Date(2011,10,6)");
CellValue cellValue = evaluator.evaluate(formulaCell);
System.out.println(cellValue.getNumberValue());
formulaCell.setCellFormula("Date(1911,3,4)");
cellValue = evaluator.evaluate(formulaCell);
System.out.println(cellValue.getNumberValue());
This outputs 40822.0
40822.0 (excel equivalent of 10/6/2011) both times instead of reevaluating to the new formula.
If you use the formulaEvaluator more than once, you need this line in between uses, or else it uses the same result each time.
formulaEvaluator.clearAllCachedResultValues()
The FormulaEvaluator caches cell calculated values to speed up processing. If you perform cell updates after creating the evaluator, then you need to tell it!
See the FormulaEvaluator documentation for more details. For you case, try:
formulaCell.setCellFormula("Date(1911,3,4)");
evaluator.notifySetFormula(formulaCell);
cellValue = evaluator.evaluate(formulaCell);
System.out.println(cellValue.getNumberValue());
You can use following steps to get your work done. These are two solutions out of which you can make use of any one function. It evaluates the complete workbook so whatever formula you use would get evaluated. Hope this helps.
1) evaluator.evaluateAll();
2) XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);

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