List of usage examples for org.apache.poi.ss.usermodel Row setHeight
void setHeight(short height);
From source file:b01.officeLink.excel.ExcelRefiller.java
License:Apache License
public void fillGroupContent(String groupStr, FocObject object) { ExcelGroupDefinition grpDef = getGroupDefinition(groupStr); Sheet srcSheet = getSourceSheet();/*from w w w . j ava 2 s. c o m*/ Sheet tarSheet = getTargetSheet(); if (grpDef != null) { for (int i = 0; i < grpDef.getRowCount(); i++) { int rowIdx = grpDef.getRowAt(i); Row sRow = srcSheet.getRow(rowIdx); if (sRow != null) { Row tRow = tarSheet.getRow(currentRow); if (tRow == null) { tRow = tarSheet.createRow(currentRow); } if (tRow != null) { tRow.setHeight(sRow.getHeight()); for (int c = 0; c < 20; c++) { Cell sCell = sRow.getCell(c + 1); if (sCell != null) { Cell tCell = tRow.getCell(c); if (tCell == null) { tCell = tRow.createCell(c); } if (tCell != null) { tCell.setCellStyle(sCell.getCellStyle()); String str = ""; if (sCell.getCellType() == Cell.CELL_TYPE_STRING) { RichTextString rts = sCell.getRichStringCellValue(); str = rts.getString(); str = analyseContent(str, object); } else if (sCell.getCellType() == Cell.CELL_TYPE_NUMERIC) { str = String.valueOf(sCell.getNumericCellValue()); } if (str != null && !str.isEmpty()) { int iVal = convertString2Integer(str); double dVal = convertString2Double(str); if (iVal != Integer.MAX_VALUE) { tCell.setCellValue(iVal); } else if (!Double.isNaN(dVal)) { tCell.setCellValue(dVal); } else { if (getFocExcelDocument() != null && getFocExcelDocument().getWorkbook() != null) { tCell.setCellValue(getFocExcelDocument().getWorkbook() .getCreationHelper().createRichTextString(str)); } } } } } } } currentRow++; } } } }
From source file:cn.afterturn.easypoi.excel.export.base.BaseExportService.java
License:Apache License
/** * ? Cells//w w w . j av a 2 s. c o m */ public int[] createCells(Drawing patriarch, int index, Object t, List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook, short rowHeight, int cellNum) { try { ExcelExportEntity entity; Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index); if (rowHeight != -1) { row.setHeight(rowHeight); } int maxHeight = 1, listMaxHeight = 1; // ???? int margeCellNum = cellNum; int indexKey = createIndexCell(row, index, excelParams.get(0)); cellNum += indexKey; for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); if (entity.getList() != null) { Collection<?> list = getListCellValue(entity, t); int listIndex = 0, tmpListHeight = 0; if (list != null && list.size() > 0) { int tempCellNum = 0; for (Object obj : list) { int[] temp = createCells(patriarch, index + listIndex, obj, entity.getList(), sheet, workbook, rowHeight, cellNum); tempCellNum = temp[1]; tmpListHeight += temp[0]; listIndex++; } cellNum = tempCellNum; listMaxHeight = Math.max(listMaxHeight, tmpListHeight); } } else { Object value = getCellValue(entity, t); if (entity.getType() == BaseEntityTypeConstants.STRING_TYPE) { createStringCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); } else if (entity.getType() == BaseEntityTypeConstants.DOUBLE_TYPE) { createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); } else { createImageCell(patriarch, entity, row, cellNum++, value == null ? "" : value.toString(), t); } if (entity.isHyperlink()) { row.getCell(cellNum - 1).setHyperlink(dataHandler.getHyperlink( row.getSheet().getWorkbook().getCreationHelper(), t, entity.getName(), value)); } } } maxHeight += listMaxHeight - 1; if (indexKey == 1 && excelParams.get(1).isNeedMerge()) { excelParams.get(0).setNeedMerge(true); } for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); if (entity.getList() != null) { margeCellNum += entity.getList().size(); } else if (entity.isNeedMerge() && maxHeight > 1) { for (int i = index + 1; i < index + maxHeight; i++) { if (sheet.getRow(i) == null) { sheet.createRow(i); } sheet.getRow(i).createCell(margeCellNum); sheet.getRow(i).getCell(margeCellNum).setCellStyle(getStyles(false, entity)); } PoiMergeCellUtil.addMergedRegion(sheet, index, index + maxHeight - 1, margeCellNum, margeCellNum); margeCellNum++; } } return new int[] { maxHeight, cellNum }; } catch (Exception e) { LOGGER.error("excel cell export error ,data is :{}", ReflectionToStringBuilder.toString(t)); LOGGER.error(e.getMessage(), e); throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); } }
From source file:cn.afterturn.easypoi.excel.export.base.BaseExportService.java
License:Apache License
/** * List??Cells/*from w w w . j a va2 s.com*/ */ public void createListCells(Drawing patriarch, int index, int cellNum, Object obj, List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook, short rowHeight) throws Exception { ExcelExportEntity entity; Row row; if (sheet.getRow(index) == null) { row = sheet.createRow(index); if (rowHeight != -1) { row.setHeight(rowHeight); } } else { row = sheet.getRow(index); if (rowHeight != -1) { row.setHeight(rowHeight); } } for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); Object value = getCellValue(entity, obj); if (entity.getType() == BaseEntityTypeConstants.STRING_TYPE) { createStringCell(row, cellNum++, value == null ? "" : value.toString(), row.getRowNum() % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); if (entity.isHyperlink()) { row.getCell(cellNum - 1).setHyperlink(dataHandler.getHyperlink( row.getSheet().getWorkbook().getCreationHelper(), obj, entity.getName(), value)); } } else if (entity.getType() == BaseEntityTypeConstants.DOUBLE_TYPE) { createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); if (entity.isHyperlink()) { row.getCell(cellNum - 1).setHyperlink(dataHandler.getHyperlink( row.getSheet().getWorkbook().getCreationHelper(), obj, entity.getName(), value)); } } else { createImageCell(patriarch, entity, row, cellNum++, value == null ? "" : value.toString(), obj); } } }
From source file:cn.afterturn.easypoi.excel.export.ExcelExportService.java
License:Apache License
/** * /*w w w . j av a 2 s.c om*/ */ private int createHeaderRow(ExportParams title, Sheet sheet, Workbook workbook, int index, List<ExcelExportEntity> excelParams, int cellIndex) { Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index); int rows = getRowNums(excelParams, true); row.setHeight(title.getHeaderHeight()); Row listRow = null; if (rows >= 2) { listRow = sheet.createRow(index + 1); listRow.setHeight(title.getHeaderHeight()); } int groupCellLength = 0; CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor()); for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) { ExcelExportEntity entity = excelParams.get(i); // ?groupName??? if (StringUtils.isBlank(entity.getGroupName()) || i == 0 || !entity.getGroupName().equals(excelParams.get(i - 1).getGroupName())) { if (groupCellLength > 1) { sheet.addMergedRegion( new CellRangeAddress(index, index, cellIndex - groupCellLength, cellIndex - 1)); } groupCellLength = 0; } if (StringUtils.isNotBlank(entity.getGroupName())) { createStringCell(row, cellIndex, entity.getGroupName(), titleStyle, entity); createStringCell(listRow, cellIndex, entity.getName(), titleStyle, entity); groupCellLength++; } else if (StringUtils.isNotBlank(entity.getName())) { createStringCell(row, cellIndex, entity.getName(), titleStyle, entity); } if (entity.getList() != null) { // ??? int tempCellIndex = cellIndex; cellIndex = createHeaderRow(title, sheet, workbook, rows == 1 ? index : index + 1, entity.getList(), cellIndex); List<ExcelExportEntity> sTitel = entity.getList(); if (StringUtils.isNotBlank(entity.getName()) && sTitel.size() > 1) { PoiMergeCellUtil.addMergedRegion(sheet, index, index, tempCellIndex, tempCellIndex + sTitel.size() - 1); } /*for (int j = 0, size = sTitel.size(); j < size; j++) { createStringCell(rows == 2 ? listRow : row, cellIndex, sTitel.get(j).getName(), titleStyle, entity); cellIndex++; }*/ cellIndex--; } else if (rows > 1 && StringUtils.isBlank(entity.getGroupName())) { createStringCell(listRow, cellIndex, "", titleStyle, entity); PoiMergeCellUtil.addMergedRegion(sheet, index, index + rows - 1, cellIndex, cellIndex); } cellIndex++; } if (groupCellLength > 1) { PoiMergeCellUtil.addMergedRegion(sheet, index, index, cellIndex - groupCellLength, cellIndex - 1); } return cellIndex; }
From source file:cn.afterturn.easypoi.excel.export.ExcelExportService.java
License:Apache License
/** * ?/*w w w. j a v a 2 s .co m*/ */ public int createTitle2Row(ExportParams entity, Sheet sheet, Workbook workbook, int fieldWidth) { Row row = sheet.createRow(0); row.setHeight(entity.getTitleHeight()); createStringCell(row, 0, entity.getTitle(), getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); for (int i = 1; i <= fieldWidth; i++) { createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); } PoiMergeCellUtil.addMergedRegion(sheet, 0, 0, 0, fieldWidth); if (entity.getSecondTitle() != null) { row = sheet.createRow(1); row.setHeight(entity.getSecondTitleHeight()); CellStyle style = workbook.createCellStyle(); style.setAlignment(HorizontalAlignment.RIGHT); createStringCell(row, 0, entity.getSecondTitle(), style, null); for (int i = 1; i <= fieldWidth; i++) { createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); } PoiMergeCellUtil.addMergedRegion(sheet, 1, 1, 0, fieldWidth); return 2; } return 1; }
From source file:cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil.java
License:Apache License
private void setForEeachRowCellValue(boolean isCreate, Row row, int columnIndex, Object t, List<ExcelForEachParams> columns, Map<String, Object> map, int rowspan, int colspan, MergedRegionHelper mergedRegionHelper) throws Exception { //cell??//from w w w . j a v a 2 s. c o m for (int i = 0; i < rowspan; i++) { int size = columns.size();//? for (int j = columnIndex, max = columnIndex + colspan; j < max; j++) { if (row.getCell(j) == null) { row.createCell(j); CellStyle style = row.getRowNum() % 2 == 0 ? getStyles(false, size <= j - columnIndex ? null : columns.get(j - columnIndex)) : getStyles(true, size <= j - columnIndex ? null : columns.get(j - columnIndex)); //styler??,?Excel,??Excel? if (style != null) { row.getCell(j).setCellStyle(style); } } } if (i < rowspan - 1) { row = row.getSheet().getRow(row.getRowNum() + 1); } } //? ExcelForEachParams params; row = row.getSheet().getRow(row.getRowNum() - rowspan + 1); for (int k = 0; k < rowspan; k++) { int ci = columnIndex; short high = columns.get(0).getHeight(); int n = k; while (n > 0) { if (columns.get(n * colspan).getHeight() == 0) { n--; } else { high = columns.get(n * colspan).getHeight(); break; } } row.setHeight(high); for (int i = 0; i < colspan && i < columns.size(); i++) { boolean isNumber = false; params = columns.get(colspan * k + i); tempCreateCellSet.add(row.getRowNum() + "_" + (ci)); if (params == null) { continue; } if (StringUtils.isEmpty(params.getName()) && StringUtils.isEmpty(params.getConstValue())) { row.getCell(ci).setCellStyle(params.getCellStyle()); ci = ci + params.getColspan(); continue; } String val = null; Object obj = null; //?? if (StringUtils.isEmpty(params.getName())) { val = params.getConstValue(); } else { String tempStr = new String(params.getName()); if (isNumber(tempStr)) { isNumber = true; tempStr = tempStr.replaceFirst(NUMBER_SYMBOL, ""); } map.put(teplateParams.getTempParams(), t); obj = eval(tempStr, map); val = obj.toString(); } if (obj != null && obj instanceof ImageEntity) { ImageEntity img = (ImageEntity) obj; row.getCell(ci).setCellValue(""); if (img.getRowspan() > 1 || img.getColspan() > 1) { img.setHeight(0); row.getCell(ci).getSheet() .addMergedRegion(new CellRangeAddress(row.getCell(ci).getRowIndex(), row.getCell(ci).getRowIndex() + img.getRowspan() - 1, row.getCell(ci).getColumnIndex(), row.getCell(ci).getColumnIndex() + img.getColspan() - 1)); } createImageCell(row.getCell(ci), img.getHeight(), img.getRowspan(), img.getColspan(), img.getUrl(), img.getData()); } else if (isNumber && StringUtils.isNotEmpty(val)) { row.getCell(ci).setCellValue(Double.parseDouble(val)); } else { try { row.getCell(ci).setCellValue(val); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } } if (params.getCellStyle() != null) { row.getCell(ci).setCellStyle(params.getCellStyle()); } //?? if (params.isNeedSum()) { templateSumHandler.addValueOfKey(params.getName(), val); } //??,????? setMergedRegionStyle(row, ci, params); //?? if ((params.getRowspan() != 1 || params.getColspan() != 1) && !mergedRegionHelper.isMergedRegion(row.getRowNum() + 1, ci) && PoiCellUtil.isMergedRegion(row.getSheet(), row.getRowNum(), ci)) { PoiMergeCellUtil.addMergedRegion(row.getSheet(), row.getRowNum(), row.getRowNum() + params.getRowspan() - 1, ci, ci + params.getColspan() - 1); } ci = ci + params.getColspan(); } row = row.getSheet().getRow(row.getRowNum() + 1); } }
From source file:cn.afterturn.easypoi.excel.html.css.impl.HeightCssConverImpl.java
License:Apache License
@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); }//from w ww . j a va 2s .c o m } }
From source file:cn.bzvs.excel.export.base.ExcelExportBase.java
License:Apache License
/** * ? Cells//from w ww.ja v a 2 s.c o m * * @param patriarch * @param index * @param t * @param excelParams * @param sheet * @param workbook * @param rowHeight * @return * @throws Exception */ public int createCells(Drawing patriarch, int index, Object t, List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook, short rowHeight) throws Exception { ExcelExportEntity entity; Row row = sheet.createRow(index); row.setHeight(rowHeight); int maxHeight = 1, cellNum = 0; int indexKey = createIndexCell(row, index, excelParams.get(0)); cellNum += indexKey; for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); if (entity.getList() != null) { Collection<?> list = getListCellValue(entity, t); int listC = 0; if (list != null && list.size() > 0) { for (Object obj : list) { createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), sheet, workbook); listC++; } } cellNum += entity.getList().size(); if (list != null && list.size() > maxHeight) { maxHeight = list.size(); } } else { Object value = getCellValue(entity, t); if (entity.getType() == BaseEntityTypeConstants.StringType) { createStringCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); if (entity.isHyperlink()) { row.getCell(cellNum - 1).setHyperlink(dataHanlder.getHyperlink( row.getSheet().getWorkbook().getCreationHelper(), t, entity.getName(), value)); } } else if (entity.getType() == BaseEntityTypeConstants.DoubleType) { createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); if (entity.isHyperlink()) { row.getCell(cellNum - 1).setHyperlink(dataHanlder.getHyperlink( row.getSheet().getWorkbook().getCreationHelper(), t, entity.getName(), value)); } } else { createImageCell(patriarch, entity, row, cellNum++, value == null ? "" : value.toString(), t); } } } // ???? cellNum = 0; for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); if (entity.getList() != null) { cellNum += entity.getList().size(); } else if (entity.isNeedMerge()) { for (int i = index + 1; i < index + maxHeight; i++) { sheet.getRow(i).createCell(cellNum); sheet.getRow(i).getCell(cellNum).setCellStyle(getStyles(false, entity)); } sheet.addMergedRegion(new CellRangeAddress(index, index + maxHeight - 1, cellNum, cellNum)); cellNum++; } } return maxHeight; }
From source file:cn.bzvs.excel.export.base.ExcelExportBase.java
License:Apache License
/** * Cell// www . ja v a2s .c om * * @param patriarch * @param entity * @param row * @param i * @param imagePath * @param obj * @throws Exception */ public void createImageCell(Drawing patriarch, ExcelExportEntity entity, Row row, int i, String imagePath, Object obj) throws Exception { row.setHeight((short) (50 * entity.getHeight())); row.createCell(i); ClientAnchor anchor; if (type.equals(ExcelType.HSSF)) { anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) i, row.getRowNum(), (short) (i + 1), row.getRowNum() + 1); } else { anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) i, row.getRowNum(), (short) (i + 1), row.getRowNum() + 1); } byte[] value = null; if (entity.getExportImageType() == 1) { if (StringUtils.isNotEmpty(imagePath)) { value = ImageCache.getImage(imagePath); } } else { value = (byte[]) (entity.getMethods() != null ? getFieldBySomeMethod(entity.getMethods(), obj) : entity.getMethod().invoke(obj, new Object[] {})); } if (value != null) { patriarch.createPicture(anchor, row.getSheet().getWorkbook().addPicture(value, getImageType(value))); } }
From source file:cn.bzvs.excel.export.base.ExcelExportBase.java
License:Apache License
/** * List??Cells//ww w .j a v a 2 s . co m * * @param patriarch * @param index * @param cellNum * @param obj * @param excelParams * @param sheet * @param workbook * @throws Exception */ public void createListCells(Drawing patriarch, int index, int cellNum, Object obj, List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook) throws Exception { ExcelExportEntity entity; Row row; if (sheet.getRow(index) == null) { row = sheet.createRow(index); row.setHeight(getRowHeight(excelParams)); } else { row = sheet.getRow(index); } for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); Object value = getCellValue(entity, obj); if (entity.getType() == BaseEntityTypeConstants.StringType) { createStringCell(row, cellNum++, value == null ? "" : value.toString(), row.getRowNum() % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); if (entity.isHyperlink()) { row.getCell(cellNum - 1).setHyperlink(dataHanlder.getHyperlink( row.getSheet().getWorkbook().getCreationHelper(), obj, entity.getName(), value)); } } else if (entity.getType() == BaseEntityTypeConstants.DoubleType) { createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); if (entity.isHyperlink()) { row.getCell(cellNum - 1).setHyperlink(dataHanlder.getHyperlink( row.getSheet().getWorkbook().getCreationHelper(), obj, entity.getName(), value)); } } else { createImageCell(patriarch, entity, row, cellNum++, value == null ? "" : value.toString(), obj); } } }