Trying to create an JXLS excel template where it should be possible to copy conditional formatting from a cell on a specific row to the next generated row.
In the template, I create my formatting. If the value in the cell is equal to "yes" the row should be red.
Template
Conditional formatting
Formula: =$B2="yes"
Applies to: $A$2:$B$2
I know this formula works on an already populated excel sheet here is an example https://trumpexcel.com/highlight-rows-based-on-cell-value/
But when I do this with my excel template and JXLS 2.0 it fails. It copies the formula as it is to each new generated row. So instead of one condition for the whole sheet, there will now be as many as there are rows. The problem here is that it will copy it as is, which means that the formula in each condition will be based on the value in cell C2. So even if cell C3 is generated with the value "no" it will be red, since it is based on the value in C2.
Output excel
Condition Formatting output excel
Any tips on how to solve this directly in the template?
Using
jxls 2.9.0
jxls-poi 2.9.0
One approach is to modify the formula in the template to acheive what we want.
Formula: =INDIRECT("$B" & ROW())="yes"
Description:
ROW() returns the current row number.
"$B" & ROW() gives the cell reference. For example, at row 5, we will get B5
Finally, using INDIRECT(...) we get the value at cell reference and check if is "yes".
Output excel:
What you are experiencing is standard Excel behaviour. In order to achieve what you want you have 2 options: using a regular Range or a dynamic table. I would use the latter.
Using a regular Range
You need to start with at least 2 rows like this:
and then only insert rows after the first row and before the last row. Never before first or after last. The new rows are picking up the same formatting because the underlying range is expanding. For example, inserting 4 rows in between results in:
Using a dynamic table
Assuming you have headers (you don't need to), you select your start range and then format it as a table:
You will have the option to choose if the table has headers or not via a checkbox in the dialog that will appear.
Then you add the same conditional formatting:
The difference now is that when you add a new row, the conditional formatting will automatically expand. The table itself automatically expands so everyting else (formatting, validation, formulas etc.) are expanding with it.
Just make sure you have the auto expanding option on for tables under File/Options/Proofing/AutoCorrect Options/AutoFormat As You Type/Include new rows and columns in table. You can do that programatically as well (I know in VBA you need to set Application.AutoCorrect.AutoExpandListRange to True). The default is True by the way.
No matter how big your table will get, you will have the formatting expanded.
Related
I am trying to create a table in NatTable that has two columns.
The first column is straight-forward but I need help while creating the second one.
Each cell of the second column has to have a dynamic number of rows, like in the image provided. In other words, each cell of the second column is divided into a variable number of rows.
I am using NatTable because of it's capacity to handle large data. But any solution is good at this point. (JFace etc)
This is what I am trying to achieve (image)
This can be achieved by using the TableCellPainter. A description with an example is contained here: https://eclipse.org/nattable/nandn/nandn_110.php
I have 1 bill which contain table, but my requirement is I want to Print values of rows and columns, with correct margin and on correct position. I am using jswing and jTable. Can anyone help me??
If I were you, I would use not a jTable, but something which intended to print: PDF, *.docx, *.rtf. In this way you have full control on page size and orientation and user may simply edit template in his favorite text processor (f.e., if a new blank has the third column 10 mm smaller).
There are many tools for Java to convert in PDF/doc format. I suppose most of them may insert value in specified field on the template.
I am using Apache POI to write data to an Excel template. The template only contains headers on the first row, but I also applied specific styles to the entire column (e.g. Accounting and Percentage).
It would be nice to use these formats when I write data to the new cells. But if I use the createRow and createCell methods, the cell format is overwritten and I get General for all the cells. If I try to use getRow and getCell instead, I run into NullPointerExceptions retrieving the blank cell.
Is there a way to use the pre-existing cell formatting saved in the template? Or am I stuck setting the data format myself through the API?
Thanks for your help.
If you have applied specific styles to an entire column, then you can retrieve that CellStyle with Sheet's getColumnStyle method, passing it the 0-based column index. It retrieves a normal CellStyle object that can be used anywhere else CellStyles are accepted, such as in Cell's setCellStyle method.
For avoiding the NullPointerException, both getRow and getCell may return null if the row or cell doesn't exist, respectively. You will need to call createRow and/or createCell to create the Cell, on which you can always call setCellStyle.
cell.setCellStyle(sheet.getColumnStyle(index) works well.
How can I create a drop list in excel like drop list in html with the inbuilt "value" like attribute?
My Requirement is: I want to show the description which is not stored in database, but the code for description.
Ex: I have a subject list in an excel cell, for Science the description is "Science", but I want to store the code "SCI" in the Database.
You could use an ActiveX ComboBox in a worksheet, available from the Developers tab.
Enter both columns of data into the worksheet - you can hide the columns. Then set properties of the combobox:
ColumnCount 2
BoundColumn 1
ColumnWidths 0 pt; 20 pt
Set the ListFillRange and LinkedCell.
I understand there used to be issues with distributing workbooks containing ActiveX controls. I am not sure if this is still an issue, particularly when used a common (standard) control.
Of course, Excel is not designed to be a front-end to a database, so you'll need to write all the code to keep everything in-sync.
You could use the simpler Form Control/ComboBox. This will only store the index number in a cell - it does not have any events that you can use. You could use a formula based on the linked cell, that stores the description in another cell. When the user (presumably) clicks a button to submit the data, you would retrieve and store the description from this cell.
Hmm I can see many placeholder occurs all around the web related to java poi... but it seems there is no solution for your question. The only way I found to manage this is to set the cell value with my default text - the first value!
I'm getting this exception while getting a cell. This is what I'm doing:
if(versionToAdd.equals("15.6")) {
Cell X = wb.getSheetAt(0).getRow(28+j).getCell((short)7);
Cell Y = wb.getSheetAt(0).getRow(28+j).getCell((short)8);
X.setCellValue(x);
Y.setCellValue(y);
}
this code it's inside of a for loop (j), and every time I iterate over a List of values, i get a new cell dynamically.
The problem is that, whenever it gets to the first col:row (H:29), i get a NullPointerException.
NOTE: The cells in the xls file are in blank... could that be the problem?
I got a collection of X/Y values in java, starts reading it, but whenever it reaches a certain amount of values, I get a NullPointerException in this line:
Cell cellx = row.getCell(8);
The java list is fine.
If I put less X/Y pairs (i.e: 5 or 6 pairs), works great, but over a certain amount (let's say > 10), I get that exception.
Tried looking for the cells, everything seems to be fine
EDITED: Thanks people for your help! I started to use the XSSF/HSSF from POI. Tried to use it before, but I couldn't get it to run properly, but now I did and it works perfect by using the createRow-createCell methods :)
Both org.apache.poi.ss.usermodel.Sheet and org.apache.poi.ss.usermodel.Row are Iterable. If you use the iterator in nested for-each loops, as shown here, you should get only defined rows.
According to the POI documentation, if you ask for a row that is not defined, null is returned.
The same is true for getting a cell from a row. I would assume that either a row or a cell does not exist.
Also, notice that the short version of getCell is deprecated in favor of getCell(int)
POI Sheet
POI Row
You can use getLastRowNum to verify that the number of rows that you are expecting exists. Same it true for getLastCellNum.
If you are creating a sheet / row / cell, you need to use the create methods. Not the get methods.
Create Row
Create Cell
The first three items on the user's guide are: how to create workbook, how to create sheet, how to create cells. You should check the documentation prior to posting questions.
POI Guide