Calculate row height for HSSFRow - java

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.

Related

Get width of an Excel column in pixels instead of the default unit of measurement

It appears from some experiments I performed and from reading this page that the default units of measurement of the column width and row height in Excel are not pixels or even the standard metric such as centimeters but they are the number of characters that will fit in the space.
I am using Aspose Cells for Java. How do I get the width of a column in pixels?
Well, you may try to use Cells.getColumnWidthPixel() to get a column's width in the units of pixel, see the sample code for your reference.
e.g
Sample code:
Workbook wb = new Workbook("Book1.xlsx");
Worksheet ws = wb.getWorksheets().get(0);
Cells cells = ws.getCells();
//Get the second column's width in pixels, i.e, B.
int colwidth = cells.getColumnWidthPixel(1);
System.out.println(colwidth);
Similarly you may try to use Cells.getRowHeightPixel() to get the row height in pixel.
I am working as Support developer/ Evangelist at Aspose.

POI JAVA appending cell without affecting cell format

Im trying to append a cell in a sheet, for example the cell 1A has a border and filled with color grey and I want to insert a string "hello", but using
cell.setCellValue("hello");
destroy the cell format or rather make the cell format in default mode. I know how to use the
CellStyle cs = workbook.createCellStyle();
method but in my project I'm inserting many different data with different cell format. I googled it and no luck finding an answer.
Is there another way to solve my problem?
To elaborate my problem.
In my sheet in 1A I have a cell format
Cell format (fill with color grey and have thin border)
but when I use
cell.setCellValue("hello");
it makes the cell's format become default but I want my cell to become like this without using CellStyle cs = workbook.createCellStyle();
cell I want
Is there a way to this?
I believe you have an excel workbook and you wanna append data to it. If you wanna keep the existing format and insert data to it you can do that using below code
cell = sheet.getRow(0).getCell(0);
cell.setCellValue("Hellooooo");
but if you are create a new cell by using
cell = sh.createRow(0).createCell(0);
then you're destroying the current format, in that case you have to create all required cellStyle all over again.

How to reduce line height inside merged cell using Apache POI?

I filled text inside merged cell using Apache POI. I want to reduce line height inside cell because second text line is not visible fully. I search something like CSS style line-height but for excel.

How to set font in decimal number using HSSFFont

I want to set text font to 7.5 to a particular text in excel sheet. I am using below code to prepare the Font.
HSSFCellStyle txtStyle = (HSSFCellStyle)workbook.createCellStyle();
HSSFFont txtFont = (HSSFFont)workbook.createFont();
txtFont.setFontName("Arial");
txtFont.setFontHeightInPoints((short)7.5);
txtStyle.setFont(txtFont);
But it always takes font as 7 because of short type casting and hence targeted text in excel sheet is taking font 7 and not 7.5. I also tried method 'setFontHeight' but that also takes short as parameter. So is there any way by which I could set text font to decimal number?
Promoting comments to an answer - the POI HSSF Font class has two font size settings methods:
setFontHeight(short) - Set the font height in unit's of 1/20th of a point
setFontHeightInPoints(short) - Set the font height in point
Using setFontHeightInPoints is the easier one for most cases, and is recommended in the Javadocs. However, it'll only cope with whole-number font heights. That's most font sizes, but not all
To set a font height of 7.5, you'd need to change your code instead to be:
xtFont.setFontHeight((short)(7.5*20));
That uses the alternate one that takes 1/20 point sizes, so copes with non-integer values.

Set a specific cell width to column in XSSF Apache POI

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.

Categories

Resources