/** * 设置行高,值为一个点的高度 * * @param rownum 行号(从0开始计数,-1表示所有行的默认高度) * @param height 高度 * @return this * @since 4.0.8 */ public ExcelWriter setRowHeight(int rownum, int height) { if (rownum < 0) { this.sheet.setDefaultRowHeightInPoints(height); } else { this.sheet.getRow(rownum).setHeightInPoints(height); } return this; }
/** * 设置行高,值为一个点的高度 * * @param rownum 行号(从0开始计数,-1表示所有行的默认高度) * @param height 高度 * @return this * @since 4.0.8 */ public ExcelWriter setRowHeight(int rownum, int height) { if (rownum < 0) { this.sheet.setDefaultRowHeightInPoints(height); } else { this.sheet.getRow(rownum).setHeightInPoints(height); } return this; }
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();
@Override public void accept(final Row row) { row.setHeightInPoints(this.value); } }
void setHeaderRow() { curRow.setHeightInPoints((short)21); }
/** * 设置行高,值为一个点的高度 * * @param rownum 行号(从0开始计数,-1表示所有行的默认高度) * @param height 高度 * @return this * @since 4.0.8 */ public ExcelWriter setRowHeight(int rownum, int height) { if (rownum < 0) { this.sheet.setDefaultRowHeightInPoints(height); } else { this.sheet.getRow(rownum).setHeightInPoints(height); } return this; }
protected void initRow(Row row, int index, String header) { CustomRowStyle rowStyle = config.getRowStyle(index, header); if (rowStyle != null) { row.setHeightInPoints((float) rowStyle.getHeight()); } }
protected void initRow(Row row, int index, String header) { CustomRowStyle rowStyle = config.getRowStyle(index, header); if (rowStyle != null) { row.setHeightInPoints((float) rowStyle.getHeight()); } }
/** * 创建新行,在使用时只要添加完一行,需要调用该方法创建 */ public void createNewRow() { if(lastRowIndex>curRowIndex&&curRowIndex!=initRowIndex) { sheet.shiftRows(curRowIndex, lastRowIndex, 1,true,true); lastRowIndex++; } curRow = sheet.createRow(curRowIndex); curRow.setHeightInPoints(rowHeight); curRowIndex++; curColIndex = initColIndex; }
/** * Define row height as follows: * - row height set explicitly by user: value defined by user; * - multiline output and no row height defined: default height * number of lines; * - otherwise: auto row height (by setting height to a magic value of #ROW_HEIGHT_AUTOMATIC); */ private void assignRowHeight(int rowHeightMultiplier) { if (rowHeightMultiplier > 1 && rowHeight == ROW_HEIGHT_AUTOMATIC) { row.setHeightInPoints(row.getHeightInPoints() * rowHeightMultiplier); } else { row.setHeight(rowHeight); } }
private void writeHeader(CellStyle cellStyle, Sheet sheet, String title, int maxColIndex) { Row titleRow = sheet.createRow(0); titleRow.setHeightInPoints(50); for (int i = 0; i <= maxColIndex; i++) { Cell cell = titleRow.createCell(i); if (i == 0) { cell.setCellValue(title); } cell.setCellStyle(cellStyle); } sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, maxColIndex)); }
/** * @param title 标题 * @param titleStyle 标题样式 */ public void createTitle(String title, CellStyle titleStyle) { Assert.hasText(title, "title不能为空"); Row titleRow = sheet.createRow(rownum++); titleRow.setHeightInPoints(30); Cell titleCell = titleRow.createCell(0); titleCell.setCellStyle(titleStyle); titleCell.setCellValue(title); sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), 0, excelFieldDescriptions.size() - 1)); }
/** * 初始化标题 * @param maxColNumber 最大列数 */ protected void initTitle(int maxColNumber) { int rowNum = this.currentRowNum++; Row titleRow = POIUtils.createRow(sheet, rowNum); titleRow.setHeightInPoints(30); // 合并单元格 POIUtils.mergeCellRangeInRow(sheet, excelStyle.getTitleStyle(), rowNum, 0, maxColNumber); Cell cell = titleRow.getCell(0); cell.setCellValue(title); cell.setCellStyle(excelStyle.getTitleStyle()); }
/** * 初始化Excel标题 * */ protected void initHead(int currentRowNum) { Row headerRow = sheet.createRow(currentRowNum); headerRow.setHeightInPoints(25); for (ExcelFieldRule excelFieldRule : excelClassRule.getFieldRuleList()) { Cell cell = headerRow.createCell(excelFieldRule.getIndex()); cell.setCellValue(excelFieldRule.getTitle()); cell.setCellStyle(excelStyle.getHeaderStyle()); } }
private void writeHeaders(HSSFSheet sheet, String title, List<XlsColumn> columns) { Cell titleCell = sheet.createRow(0).createCell(0); titleCell.setCellValue(book.getCreationHelper().createRichTextString(title)); titleCell.setCellStyle(titleStyle); Row columnHeaderRow = sheet.createRow(1); columnHeaderRow.setHeightInPoints(HEADER_CELL_HEIGHT); int columnIndex = 0; for (int i = 0; i < columns.size(); i++) { XlsColumn column = columns.get(i); Cell cell = columnHeaderRow.createCell(columnIndex); cell.setCellStyle(headerStyle); cell.setCellValue(column.getHeading()); sheet.setColumnWidth(columnIndex, width(column.getHeading())); columnIndex++; } }
/** * 初始化单元格 */ private void ini(){ log.debug("初始化单元格总共:{}行,{}列",tableBean.getRowCount(),tableBean.getColumnCount()); for(int r=0;r<tableBean.getRowCount();r++){ Row row = sheet.createRow(r); if(tableBean.getRowHeight()!=null){ row.setHeightInPoints(tableBean.getRowHeight()); } for(int c=0;c<tableBean.getColumnCount();c++){ row.createCell(c); } } }
private static void createTitleRow(Workbook workbook, Row row, List<ExportCell> exportCells, Sheet sheet) { CellStyle style = createHeadStyle(workbook); row.setHeightInPoints(25.0F); Font font = workbook.createFont(); font.setColor((short) 12); // font.setBoldweight((short) 700); style.setFont(font); style.setFillBackgroundColor((short) 13); int i = 0; for (ExportCell exportCell : exportCells) { sheet.setColumnWidth(i, 3200); Cell cell = row.createCell(i); cell.setCellValue(exportCell.getTitle()); cell.setCellStyle(style); ++i; } }
private void writeColumnNames(int rowIndex, CellStyle headerStyle) { Row rowHead = sheet.createRow(rowIndex); rowHead.setHeightInPoints(30); for (ExcelColumn column : columns) { Cell cell = rowHead.createCell(column.index()); if (null != headerStyle) { cell.setCellStyle(headerStyle); } cell.setCellValue(column.title()); if (column.width() > 0) { sheet.setColumnWidth(column.index(), column.width()); } else { sheet.setColumnWidth(column.index(), Constant.DEFAULT_COLUMN_WIDTH); } } }
@Override public void nextRow(Cell cell) { if (nowRow == null) { //首次渲染行,则将表达式所在行替换为数据行 nowRow = cell.getRow(); } else { //创建下一行 int rowNum = nowRow.getRowNum() + 1; //将最后一行移动到当前行,以实现插入行效果 sheet.shiftRows(rowNum,sheet.getLastRowNum(),1,true,false); Row tmp = sheet.createRow(rowNum); tmp.setHeight(nowRow.getHeight()); tmp.setHeightInPoints(nowRow.getHeightInPoints()); nowRow = tmp; } }
@Override public void nextRow(Cell cell) { if (nowRow == null) { //首次渲染行,则将表达式所在行替换为数据行 nowRow = cell.getRow(); } else { //创建下一行 int rowNum = nowRow.getRowNum() + 1; //将最后一行移动到当前行,以实现插入行效果 sheet.shiftRows(rowNum,sheet.getLastRowNum(),1,true,false); Row tmp = sheet.createRow(rowNum); tmp.setHeight(nowRow.getHeight()); tmp.setHeightInPoints(nowRow.getHeightInPoints()); nowRow = tmp; } }