protected static void fixFonts(Workbook workbook) { if (!JvmBugs.hasLineBreakMeasurerBug()) return; for (int i = workbook.getNumberOfFonts() - 1; i >= 0; i--) { Font f = workbook.getFontAt((short) 0); if ("Calibri".equals(f.getFontName())) { f.setFontName("Lucida Sans"); } else if ("Cambria".equals(f.getFontName())) { f.setFontName("Lucida Bright"); } } }
@Test public void stringCells() throws Exception { Workbook workbook = _testDataProvider.createWorkbook(); fixFonts(workbook); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Font defaultFont = workbook.getFontAt((short) 0); CellStyle style1 = workbook.createCellStyle(); Font font1 = workbook.createFont(); font1.setFontHeight((short) (2 * defaultFont.getFontHeight())); style1.setFont(font1); row.createCell(0).setCellValue("x"); row.createCell(1).setCellValue("xxxx"); row.createCell(2).setCellValue("xxxxxxxxxxxx"); row.createCell(3) .setCellValue("Apache\nSoftware Foundation"); // the text is splitted into two lines row.createCell(4).setCellValue("Software Foundation"); Cell cell5 = row.createCell(5); cell5.setCellValue("Software Foundation"); cell5.setCellStyle( style1); // same as in column 4 but the font is twice larger than the default font for (int i = 0; i < 10; i++) sheet.autoSizeColumn(i); assertTrue( 2 * sheet.getColumnWidth(0) < sheet.getColumnWidth(1)); // width is roughly proportional to the number of characters assertTrue(2 * sheet.getColumnWidth(1) < sheet.getColumnWidth(2)); assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(3)); boolean ignoreFontSizeX2 = JvmBugs.hasLineBreakMeasurerBug(); assertTrue( ignoreFontSizeX2 || sheet.getColumnWidth(5) > sheet.getColumnWidth(4)); // larger font results in a wider column width workbook.close(); }