Set a specific cell width to column in XSSF Apache POI - java

I am trying to export a table as xlsx using Apache POI XSSF. Table has 5 columns.
First row has title block merging 5 columns. In the second row, 5 headings for the table. Remaining rows are data. I want to set width of the column as max width of each heading blocks.
I tried mySheet.autoSizeColumn(colnum) and mySheet.setColumnWidth(columnIndex, width). Problem with AutosizeColumn, it is returning the highest width of the data in all the rows. So If width of some data in table is more, table header width is becoming very large.
And for the second one, setColumnWidth, I need to know width of the header cell so I can set it to the sheet. But how to find the width of a specific cell? Had no luck yet in figuring out how to do it. Any idea on how to that?

I would suggest simple solution that I have used.
Use a condition and after writing the 2nd row, i.e. the heading row, use AutosizeColumn() it will change the cell width according to the header width and then the width will remain as it is.

Related

JTable cell data showing "..." on column resize

Two questions:
1.) How do I put a min limit on column resize in JTable?
2.) How do I stop data from showing a "..." on column resize when data can't fit into cell?
//Minimum width 100 for first column
table.getColumnModel().getColumn(0).setMinWidth(100);
About the ..., I am not sure you can do anything but keeping the minimum width of the cell according to its value.

How to change the column width of a Individual column in excel in excel through Apache-POI

I want to change the column width of a column I have seen many answers suggesting the use of "AutoSizeColumn(int)" but the problem with that is that all columns have different sizes and it creates kind of a mess so i want to set all columns ( except one ) to one single width how do i do this??
You can set a default column width that controls all columns in the sheet that don't have their own custom width set. Use the setDefaultColumnWidth method.
Set the default column width for the sheet (if the columns do not define their own width) in characters
sheet.setDefaultColumnWidth(numChars);
Individual columns can override this default with the setColumnWidth method. The width here is in 1/256ths of a character, different from the default width units.
Set the width (in units of 1/256th of a character width)

Apache POI (DOCX) XWPFTable asymmetric cell

I want to create a table looks like
this.
but the only way to to this is with an horrible merge of cells.
i try to make two separate table but the top table, follow the cell width of bottom table like this.
There is a way to make an asymmetric table without merge cells?
You don't have to 'merge' cells, but you do have to tell it how many grid columns or rows the cell spans. Picture a grid that has a column that matches every column position, and a row that matches every row position. In your example, you would have a grid with 18 columns and 3 rows. Then for each cell that spans one or more grid column or row, you must have a span attribute that tells how many grid positions are spanned by the cell. The result is the same as merging, and it might even be what you meant by merging, but that is the way the spec reads.

Calculate row height for HSSFRow

I'm writing an Excel (xls) sheet with Apache POI 3.13. I set the column width manually. If the content of a cell is too long, I want it to be wrapped and the column height to be adjusted.
If I set the wrapText property of the CellStyle to true, the text does not "flow" out of the cell any more, but how do I set the height of the row to a fitting value?
All approaches I've seen calculate the newline characters in the string. This is not working for me, since my text does not contain manual newlines.
This seems to be a LibreOffice bug, because with Excel on a Windows system it looks as expected.

how to convert the width of column of html table to the width of excel column in java

I have width of the column value as a percentage and I want to convert the same to excel width to set the width of the column. How do we do this in java, based on what the column width is calculated in excel.?
I use the following:
HSSFSheet sheet = hwb.createSheet(reportName);
sheet.setColumnWidth((short)columnIterator, (short)6000);
In this method, the first argument is the column id and the second one is the width of the column. I want to set this width dynamically using the html width.
Example:
If the the column width in html is 10% then what is the equelant width in excel width.?
I want to set the column width of excel dynamically like setAutoColumnwidth().
UPDATE :
i found this, is this correct.
Regards,
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html#autoSizeColumn%28int%29
Does this do what you want?

Categories

Resources