This is my code (Java)
sheet.setColumnWidth(0, 100 * 256);
I expect the width of column 1 of the generated excel to be 100. But the actual width in excel is 99.22 . The search engine did not provide a reasonable explanation, can anyone help.
In Excel the column width 100 means that 100 default characters in default font and default font size fit into the cell. So how width 100 exactly is varies with default font and default font size. The default font and default font size is given by cell style Normal given in Styles group in Home tab.
In storage Excel stores the width having additional padding. So coulmn widt 100 is not exactly stored as 100 but as 100 + padding. But also the padding differs for different default fonts and default font sizes used.
Apache POI using XSSF (Office Open XML format) takes font Calibri and font size 11 as the defaults. Those are the defaults used by Excel too. So Units.DEFAULT_CHARACTER_WIDTH of Apache POI is default character width of that default font in size 11.
So using following column width setting:
...
int widthExcel = 100;
int width256 = (int)Math.round((widthExcel*Units.DEFAULT_CHARACTER_WIDTH+5f)/Units.DEFAULT_CHARACTER_WIDTH*256f);
sheet.setColumnWidth(0, width256);
...
should set column width exactly 100 in Excel if that Excel uses font Calibri and font size 11 as the defaults in cell style Normal.
Using other defaults the Units.DEFAULT_CHARACTER_WIDTH in the calculation must be replaced by other default character widths.
I modified my code(I got it from here https://blog.csdn.net/ab411919134/article/details/17606399):
sheet.setColumnWidth(0, (int) ((100 + 0.73) * 256));
The post mentioned that it should be 0.72, but 0.73 is correct for me
This excel column width meets expectations, but I still don’t know why
Related
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)
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.
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.
I'm using a fixed cell height to create a table.
If the font size is too large, the text is not visible in the table.
Is there a built-in function in iText that automatically reduces the font size to the maximum possible size, or do I have to implement this by myself?
Automatic font size is only possible in the context of AcroForm text fields. When you define the font size of a text field as 0, then a font size is chosen that fits the rectangle. In the case of a fixed cell height in a table, you are responsible to make sure that the text fits.
If you're concerned about the height, please take a look at the FitTextInRectangle example:
BaseFont bf = BaseFont.createFont();
int textHeightInGlyphSpace = bf.getAscent(text) - bf.getDescent(text);
float fontSize = 1000f * fixedHeight / textHeightInGlyphSpace;
This example was written in answer to Correct text position center in rectangle iText
If you're concerned about the width, then you need to use the getWidthPoint() method as explained here: How to calculate the string width in iText?
BaseFont bf = BaseFont.createFont();
float width = bf.getWidthPoint("My text", myFontSize);
You'll need to make sure that width doesn't exceed the width of the cell. To achieve this, you'll need to adjust myFontSize.
See my answer to this question: How to choose the optimal size for a font?
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.