Row sqlRow = sqlSheet.createRow(0); short height = 5000; sqlRow.setHeight(height); Cell cell = sqlRow.createCell(0); cell.setCellStyle(sqlCellStyle);
/** * 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); } }
@Override public void accept(final Row row) { row.setHeight(this.value); } }
@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; } }
static void copyPositiveRowHeight(Sheet sheet, int startRow, int endRow, int shiftNum, short[] rowHeights) { for (int i = startRow; i <= endRow; i++) { org.apache.poi.ss.usermodel.Row destRow = sheet.getRow(i + shiftNum); if (destRow != null && rowHeights[i - startRow] >= 0) { destRow.setHeight(rowHeights[i - startRow]); } // Row srcRow = sheet.getRow(i); // if( srcRow != null && destRow != null ){ // if( srcRow.getHeight() >= 0 ){ // destRow.setHeight( srcRow.getHeight() ); // } // } } }
@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; } }
static void copyPositiveRowHeight(Sheet sheet, int startRow, int endRow, int shiftNum, short[] rowHeights) { for (int i = startRow; i <= endRow; i++) { org.apache.poi.ss.usermodel.Row destRow = sheet.getRow(i + shiftNum); if (destRow != null && rowHeights[i - startRow] >= 0) { destRow.setHeight(rowHeights[i - startRow]); } // Row srcRow = sheet.getRow(i); // if( srcRow != null && destRow != null ){ // if( srcRow.getHeight() >= 0 ){ // destRow.setHeight( srcRow.getHeight() ); // } // } } }
/*** * 将本次导出数据的hash密码写在Excel上 * @param sheet * @param hashCode */ public static void setHashVal(Sheet sheet,long hashCode){ Row sheetRow = sheet.getRow(HASH_ROW); Cell cell = sheetRow.createCell(0); cell.setCellValue(hashCode); sheetRow.setHeight(Short.valueOf("0")); }
row.setHeight((short)UnitUtils.pointToTwip(r.getHeight())); rowNumber++;
row.setHeight((short)UnitUtils.pointToTwip(r.getRealHeight())); rowNumber++;
/** * 生成表头 */ private void createHead(){ log.debug("生成表头"); Row hashRow = sheet.createRow(HASH_ROW); addTitle(sheet, TITLE_ROW, fieldNames.length, language.translate(title)); Row row = createRow(sheet, HIDDEN_FIELD_HEAD, fieldNames.length); List<String> columns=new ArrayList(fieldNames.length); for (String fieldName : fieldNames) { columns.add(language.translate(fieldName)); } addRow(sheet, COLUMN_ROW, columns.toArray(new String[]{})); hashRow.setHeight(Short.valueOf("0")); row.setHeight(Short.valueOf("0")); log.debug("表头生成完毕"); }
Workbook wb = new HSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); CreationHelper createHelper = wb.getCreationHelper(); Sheet sheet = wb.createSheet("new sheet"); Row row = sheet.createRow((short) 0); row.setHeight((short) (2*sheet.getDefaultRowHeight())); CellStyle cs = wb.createCellStyle(); cs.setWrapText(true); Cell cell = row.createCell(0); cell.setCellStyle(cs); cell.setCellValue( createHelper.createRichTextString("This is \n a string")); wb.write(fileOut); fileOut.close();
@Override public void updateRowHeight(String srcSheetName, int srcRowNum, String targetSheetName, int targetRowNum) { if (isSXSSF) return; SheetData sheetData = sheetMap.get(srcSheetName); RowData rowData = sheetData.getRowData(srcRowNum); Sheet sheet = workbook.getSheet(targetSheetName); if (sheet == null) { sheet = workbook.createSheet(targetSheetName); } Row targetRow = sheet.getRow(targetRowNum); if (targetRow == null) { targetRow = sheet.createRow(targetRowNum); } short srcHeight = rowData != null ? (short) rowData.getHeight() : sheet.getDefaultRowHeight(); targetRow.setHeight(srcHeight); } }
@Override public void convertToExcel(Cell cell, CellStyle cellStyle, CellStyleEntity style) { if (StringUtils.isNoneBlank(style.getHeight())) { int height = Math.round(PoiCssUtils.getInt(style.getHeight()) * 255 / 12.75F); Row row = cell.getRow(); if (height > row.getHeight()) { row.setHeight((short) height); } } }
@Override public void convertToExcel(Cell cell, CellStyle cellStyle, CellStyleEntity style) { if (StringUtils.isNoneBlank(style.getHeight())) { int height = Math.round(PoiCssUtils.getInt(style.getHeight()) * 255 / 12.75F); Row row = cell.getRow(); if (height > row.getHeight()) { row.setHeight((short) height); } } }
private void setHeightsAndWidths(Sheet reportSheet, ReportType type) { reportSheet.setColumnWidth(0, 15000); reportSheet.setColumnWidth(1, 3000); reportSheet.setColumnWidth(2, 3000); reportSheet.setColumnWidth(3, 3000); reportSheet.setColumnWidth(4, 7000); reportSheet.setColumnWidth(5, 4000); reportSheet.setColumnWidth(6, 4000); for (int i = 7; i < 15; i++) { reportSheet.setColumnWidth(i, 3000); } reportSheet.getRow(7).setHeight((short) 1000); }
// Create Font object with Font attribute (e.g. Font family, Font size, etc) for calculation java.awt.Font currFont = new java.awt.Font(fontName, 0, fontSize); AttributedString attrStr = new AttributedString(cellValue); attrStr.addAttribute(TextAttribute.FONT, currFont); // Use LineBreakMeasurer to count number of lines needed for the text FontRenderContext frc = new FontRenderContext(null, true, true); LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc); int nextPos = 0; int lineCnt = 0; while (measurer.getPosition() < cellValue.length()) { nextPos = measurer.nextOffset(mergedCellWidth); // mergedCellWidth is the max width of each line lineCnt++; measurer.setPosition(nextPos); } Row currRow = currSht.getRow(rowNum); currRow.setHeight((short)(currRow.getHeight() * lineCnt)); // The above solution doesn't handle the newline character, i.e. "\n", and only // tested under horizontal merged cells.
public ExcelWrite<T> list(List<T> list) { // 当前总数 excelWrite.setTotal(excelWrite.getTotal() + list.size()); createSheet(list, true); for (T t : list) { createSheet(list, false); // 创建一行 Row dataRow = excelWrite.getCurrentSheet().createRow(excelWrite.getCurrentRow()); dataRow.setHeight(excelWrite.getDeal().setHeight()); // 得到要插入的每一条记录 String [] data = excelWrite.getDeal().dealBean(t); for (int k = 0; k < data.length; k++) { // 在一行内循环 Cell cell = dataRow.createCell(k); cell.setCellValue(setRichTextString(excelWrite.getLargeDataMode(), excelWrite.getType(), data[k])); cell.setCellStyle(noColor); } excelWrite.setCurrentRow(excelWrite.getCurrentRow() + 1); } return this; }
/** * 图片类型的Cell */ public void createImageCell(Cell cell, double height, String imagePath, byte[] data) throws Exception { if (height > cell.getRow().getHeight()) { cell.getRow().setHeight((short) height); } ClientAnchor anchor; if (type.equals(ExcelType.HSSF)) { anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), cell.getRow().getRowNum() + 1); } else { anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), cell.getRow().getRowNum() + 1); } if (StringUtils.isNotEmpty(imagePath)) { data = ImageCache.getImage(imagePath); } if (data != null) { PoiExcelGraphDataUtil.getDrawingPatriarch(cell.getSheet()).createPicture(anchor, cell.getSheet().getWorkbook().addPicture(data, getImageType(data))); } }