public static double getRowHeightInPixels(Sheet sheet, int rowNum) { Row r = sheet.getRow(rowNum); double points = (r == null) ? sheet.getDefaultRowHeightInPoints() : r.getHeightInPoints(); return Units.toEMU(points)/(double)EMU_PER_PIXEL; } }
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); Sheet sheet = wb.createSheet(); Row row = sheet.createRow(2); Cell cell = row.createCell(2); cell.setCellValue("Use \n with word wrap on to create a new line"); //to enable newlines you need set a cell styles with wrap=true CellStyle cs = wb.createCellStyle(); cs.setWrapText(true); cell.setCellStyle(cs); //increase row height to accomodate two lines of text row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints())); //adjust column width to fit the content sheet.autoSizeColumn((short)2); FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx"); wb.write(fileOut); fileOut.close();
/** * Returns row height measured in point size. If the height is not set, the default worksheet value is returned, * See {@link Sheet#getDefaultRowHeightInPoints()} * * @return row height measured in point size * @see Sheet#getDefaultRowHeightInPoints() */ public float getHeightInPoints() { return (float)(_height==-1?getSheet().getDefaultRowHeightInPoints():(float)_height/20.0); }
/** * Get the row's height measured in twips (1/20th of a point). If the height is not set, the default worksheet value is returned, * See {@link Sheet#getDefaultRowHeightInPoints()} * * @return row height measured in twips (1/20th of a point) */ public short getHeight() { return (short)(_height==-1?getSheet().getDefaultRowHeightInPoints()*20:_height); }
public static double getRowHeightInPixels(Sheet sheet, int rowNum) { Row r = sheet.getRow(rowNum); double points = (r == null) ? sheet.getDefaultRowHeightInPoints() : r.getHeightInPoints(); return Units.toEMU(points)/(double)EMU_PER_PIXEL; } }
public static void main(String[]args) throws IOException { try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); Sheet sheet = wb.createSheet(); Row row = sheet.createRow(2); Cell cell = row.createCell(2); cell.setCellValue("Use \n with word wrap on to create a new line"); //to enable newlines you need set a cell styles with wrap=true CellStyle cs = wb.createCellStyle(); cs.setWrapText(true); cell.setCellStyle(cs); //increase row height to accommodate two lines of text row.setHeightInPoints(2 * sheet.getDefaultRowHeightInPoints()); //adjust column width to fit the content sheet.autoSizeColumn(2); try (FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx")) { wb.write(fileOut); } } } }