I am using POI to generate a docx file. I need to create table in which a cell of table contains another table. How this can be done?
I had the same problem.
Instead of insert a table into a tableCell, I decided to "split" the cell, although I was not able to do it.
So, I decided to create new rows/column and merge all cells except the cells at the row/column I wanted to split.
For merging the cells I decided to follow the following steps:
For merging horizontally/vertically you need to create 2 CTHMerge and use the setVal:
one for the cells that you will remain (STMerge.RESTART);
a second one for the merged cells (STMerge.CONTINUE);
a) example for a horizontal merge for 2x2 table:
// First Row
CTHMerge hMerge = CTHMerge.Factory.newInstance();
vmerge.setVal(STMerge.RESTART);
table.getRow(0).getCell(0).getCTTc().getTcPr().setHMerge(hMerge);
table.getRow(1).getCell(0).getCTTc().getTcPr().setHMerge(hMerge);
// Secound Row cell will be merged/"deleted"
CTHMerge hMerge1 = CTHMerge.Factory.newInstance();
hMerge.setVal(STMerge.CONTINUE);
table.getRow(0).getCell(1).getCTTc().getTcPr().setHMerge(hMerge1);
table.getRow(1).getCell(1).getCTTc().getTcPr().setHMerge(hMerge1);
b) example of a vertical merge (image with example)
// First Row
CTVMerge vmerge = CTVMerge.Factory.newInstance();
vmerge.setVal(STMerge.RESTART);
table.getRow(0).getCell(0).getCTTc().getTcPr().setVMerge(vmerge);
table.getRow(0).getCell(1).getCTTc().getTcPr().setVMerge(vmerge);
// Secound Row cell will be merged
CTVMerge vmerge1 = CTVMerge.Factory.newInstance();
vmerge.setVal(STMerge.CONTINUE);
table.getRow(1).getCell(0).getCTTc().getTcPr().setVMerge(vmerge1);
table.getRow(1).getCell(1).getCTTc().getTcPr().setVMerge(vmerge1);
Related
I'm trying to shift rows down in my excel sheet and insert new data to the top of the list.
I'm able to shift rows down as:
int row = getItemsListSize(); <- which returns size of the list which contains elements
sheet.shiftRows(1, sheet.getLastRowNum(), rows);
Then I insert new data starting from row 1 and that way newest data is always on top of the list and older data gets moved down.
My problem is that I have second table also on that sheet and amount of items inside it isn't same as in first table. Currently also contents of this table gets moved down although no new items aren't added. Because of that it looks weird.
Is there way to only shift rows inside specific columns? For example: Move rows only in columns A-D.
No; from an excel perspective, there is no such thing as two separate tables. It's just one big sheet with rows and columns. Apache POI is a library that gives you access to excel's data structures. It's not a generalized 'mess with sheets' library. You'd have to write the code for such such a thing yourself.
More generally you're misusing excel a bit. The right move usually is to treat data, as, well, data. You should not be having 2 unrelated tables on the same sheet. Use the + button at the bottom left (or the appropriate POI option, which also supports sheets) to add another sheet.
If you want a sheet for visual reasons, make a third sheet, and that is a sheet that your POI code should never touch - it will render the data from the other (data) sheets. You can now add graphs, logos, whatever you want, and you won't run into issues where moving things around a bit 'because it looks nicer visually' then results in your POI-based code being broken.
I have a simple table in excel which has a column serial no with formula =ROW()-ROW(Member_Data[[#Headers],[S. No.]]). Whenever I add new row to the table serial no is calculated automatically.when i added new row using Apache POI it didn't copied formula to the next row also style should be copied.
I have set AreaReference source = new AreaReference("A1:C8", SpreadsheetVersion.EXCEL2007);, which specifies my table range till 8th row.But its not working in any row between 1 to 8.
Currently I am copying 1st row formulas to new row using setcellformula as workaround which is working. I need alternate solution which is not dependent on 1st row. My task is only for read operation.
I'm trying to draw a table like the one in the image and I can't split a cell into two columns like the last two columns of the table. Please help me with some ideas. I'm using Itext api for creating the PDF file.
Instead of trying to split the Din care column, have that data in two columns (so 4 columns in total for the entire table) and then merge only the top cell by letting it span two columns. You do this with setting setColspan(2) on the Din care cell.
Additionally you may want to look at setRowspan() for the Categoria de Varsta and Total cells.
I have a JTable consisting of multiple rows and columns. I want to make the cells selectable, but only in one row at a time. So for example, when I click on the cell in third row and the fifth column, I can pull the mouse to the left or right and select more cells, but only in this specific row and not in the row above or below.
How can I do this?
Use the setSelectionMode() method from ListSelectionModel interface, and set the selection mode to ListSelectionModel.SINGLE_SELECTION.
This will configure JTable to work with one row at a time selection, blocking selection of multiple rows.
To select single cells, combine the above with setColumnSelectionAllowed(true) on TableColumnModel, and you should get what you need.
I populated Excel worksheet using HSSFWorkbook. I am facing a problem in formatting a cell. I have two colummns with contain varying text sizes. If the row in Column B is not empty then the text from Column A does not overlap that in Column B. However, If Column B is empty, the text in a row of column A appears to be continuing onto Column B which is not desired.
Can someone please help me how I can avoid text in Row 1 Column A not to continue onto Row 1 Column B? I do not want to use wrap because that would make the height of Row 1 inconsistent with the other rows. Also, I do not want to adjust the width of Row 1 to match the text size because that is not desired.
Thanks,
H
Described behavior
If Column B is empty, the text in a row of column A appears to be continuing onto Column B
is a standard Excel behavior and has nothing to do with POI. So try first avoiding it in the Excel itself (putting a space in B column as trashgod advices would do) and than reproduce it using POI.