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.
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'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 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);
I have an Excel template with first 5 columns left blank (to be populated from an XML). The sixth column has a drop-down list such that depending on whatever value is selected from the list, a value appears in 7th column. This is done by using INDEX-MATCH formula for the 7th column.
=INDEX(Sheet7!$B$1:$B$312,MATCH(F3,Sheet7!$A$1:$A$312,0))
The task is to take this template Excel, and using POI, populate the first 5 columns and generate a new Excel workbook. (Not changing in the existing sheet).
The problem is when I generate the new Excel workbook, the formula isn't copied over from the template. It is really important to retain the formula for the task I am working on. I read about
formulaEvaluator()
but I don't think it'll work in that case because I need the formula to be retained as it is. I cannot evaluate it before copying as there won't be any data in column 6 at that point to evaluate the formula and store the value.
I'd really appreciate if someone who has any experience in this area could guide me a bit.
If you have access to the Cell, simply call:
String formula = cell.getCellFormula();
Note though that this will only work if the cell is formula cell. IE: cell.getCellType() == Cell.CELL_TYPE_FORMULA. If the cell is not a formula cell, you're going to end up with an IllegalStateException as described in the API
I have to insert image in to the excel cell using jxl api
I have tried with some sample code but it will insert at some coordinator, but I want in to at particular cell index or inside cell.
Here is sample code that I found while searching for this.
Anyone has solution for this?
From a discussion on channel9:
No version of Excel allows you to insert a picture into a cell. Pictures are inserted into the worksheet and will always float.
One of the properties of a picture can be set to "move and size with cells" but that only moves or stretches the picture when the underlying rows and columns are inserted, deleted or sized. It does not confine a picture to a cell.
Here is a step-by-step procedure on how to do it from Excel, maybe you can mime it programmatically.