I have generated an Excel document with jxls and POI. Now POI has merged cell, half data is in page 1 & other half of merged cell in page 2. How can I prevent it?
Unfortunately you can't. That's a disadvantage of Excel. Excel does not provide a "keep merged cells on one page" feature. One need to set an explicit page break above of the row which shall the first on the new page.
Apache POI provides Sheet.setRowBreak to do so.
So you need to know what row is the first one having the merged cells split into two pages. Then set row break to the row above.
For example if first row index of the rows having the merged cells split into two pages is 25, then:
...
Sheet sheet ...
...
sheet.setRowBreak(24);
...
Of course this only can be used if the row positions on the page are static or if one knows how many rows fit to one page. I don't know any more dynamic solution.
Same is if merged columns split over two pages. Then manually set a column break is needed.
Apache POI provides Sheet.setColumnBreak to do so.
Of course this has same disadvantages as manually row break has.
Related
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.
I'd working on drive with google sheets and google forms.
I am adding data to the sheet automatically from the responses of the form.
I don't have much knowledge in script editor or script writing,
but can we search for specific words in a column and then copy the entire row to a new tab or spreadsheet.
For example, I have Apple, Banana, Mango listed in column and I have three different spreadsheets apart from the main sheet. Where I'd like to add all the cells in row of Apple in one sheet, and Banana in next one.
Thanks!
You don't need code. Just use filter function.
=filter(Data!A:A,Main!A:A="Apple")
Put this on the "Apple" in column, row where you want data to be filled in assuming your list of items is in Column A of sheet named "Data" in same google sheet.
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 am reading a .xslx file using Apache POI in Java.The last row in it is having some borders missing around some cells.
when I am reading this then in the last row after reading "abc"(written in first cell) ,am directly reading "rule1"(written in third cell) and second column is empty and bottom border is missing for it.
So the value of second cell which is empty cell is lost.So how can I be able to know that whether I have skipped any cell due to this formatting problem?
Use Cell.getColumnIndex() to retrieve the current cell's index. With this information you will be able to exactly get its position, and thus also find out whether there were any groups.
I am creating a table using itext. Now while setting header row, if I set table.setheaderrows(2) then it sets first 2 rows as the header. But in my case I want only row no. 2 (not row number 1) to be reprinted while table is extended on the next page.
Is there any way to achieve this?
If you only work with a single PdfPTable, you can't define the second row as the header row that needs to be repeated. The trick is to use two PdfPTable instances with the same widths for the columns. The first one would be a single row table for the first header row, the second one would start with the header row that needs to be repeated. If you add two tables to a document, one right after the other, they are glued to each other and nobody will notice that it's not a single table.