List of usage examples for org.apache.poi.ss.usermodel Row setHeight
void setHeight(short height);
From source file:cn.bzvs.excel.export.ExcelExportServer.java
License:Apache License
/** * ?//from ww w. j a va 2 s. c o m * * @param entity * @param sheet * @param workbook * @param feildWidth */ public int createHeaderRow(ExportParams entity, Sheet sheet, Workbook workbook, int feildWidth) { Row row = sheet.createRow(0); row.setHeight(entity.getTitleHeight()); createStringCell(row, 0, entity.getTitle(), getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); for (int i = 1; i <= feildWidth; i++) { createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); } sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, feildWidth)); if (entity.getSecondTitle() != null) { row = sheet.createRow(1); row.setHeight(entity.getSecondTitleHeight()); CellStyle style = workbook.createCellStyle(); style.setAlignment(CellStyle.ALIGN_RIGHT); createStringCell(row, 0, entity.getSecondTitle(), style, null); for (int i = 1; i <= feildWidth; i++) { createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); } sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, feildWidth)); return 2; } return 1; }
From source file:cn.bzvs.excel.export.ExcelExportServer.java
License:Apache License
/** * /*from w w w .jav a2s . c o m*/ * * @param title * @param index */ private int createTitleRow(ExportParams title, Sheet sheet, Workbook workbook, int index, List<ExcelExportEntity> excelParams) { Row row = sheet.createRow(index); int rows = getRowNums(excelParams); row.setHeight((short) 450); Row listRow = null; if (rows == 2) { listRow = sheet.createRow(index + 1); listRow.setHeight((short) 450); } int cellIndex = 0; CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor()); for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) { ExcelExportEntity entity = excelParams.get(i); if (StringUtils.isNotBlank(entity.getName())) { createStringCell(row, cellIndex, entity.getName(), titleStyle, entity); } if (entity.getList() != null) { List<ExcelExportEntity> sTitel = entity.getList(); if (StringUtils.isNotBlank(entity.getName())) { sheet.addMergedRegion( new CellRangeAddress(index, index, cellIndex, cellIndex + 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 == 2) { createStringCell(listRow, cellIndex, "", titleStyle, entity); sheet.addMergedRegion(new CellRangeAddress(index, index + 1, cellIndex, cellIndex)); } cellIndex++; } return rows; }
From source file:cn.bzvs.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 www .jav a 2 s. co 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;//cell?? row.setHeight(columns.get(0 * colspan).getHeight()); 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(columns.get(i).getCellStyle()); ci = ci + columns.get(i).getColspan(); continue; } String val = 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); val = eval(tempStr, map).toString(); } if (isNumber && StringUtils.isNotEmpty(val)) { row.getCell(ci).setCellValue(Double.parseDouble(val)); row.getCell(ci).setCellType(Cell.CELL_TYPE_NUMERIC); } else { try { row.getCell(ci).setCellValue(val); } catch (Exception e) { e.getMessage(); } } row.getCell(ci).setCellStyle(columns.get(i).getCellStyle()); //?? if (params.isNeedSum()) { templateSumHanlder.addValueOfKey(params.getName(), val); } //??,????? setMergedRegionStyle(row, ci, columns.get(i)); //?? if ((params.getRowspan() != 1 || params.getColspan() != 1) && !mergedRegionHelper.isMergedRegion(row.getRowNum() + 1, ci)) { row.getSheet().addMergedRegion(new CellRangeAddress(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:com.actelion.research.spiritapp.ui.util.POIUtils.java
License:Open Source License
public static void autoSizeColumns(Sheet sheet, int maxColWidth, boolean resizeHeight) { ListHashMap<Integer, Integer> col2lens = new ListHashMap<>(); for (int row = sheet.getFirstRowNum(); row <= sheet.getLastRowNum(); row++) { Row r = sheet.getRow(row); if (r == null || r.getFirstCellNum() < 0) continue; short maxH = 0; for (int col = r.getFirstCellNum(); col <= r.getLastCellNum(); col++) { Cell c = r.getCell(col);/*from ww w . j a va 2 s . com*/ if (c == null || (c.getCellType() != Cell.CELL_TYPE_STRING && c.getCellType() != Cell.CELL_TYPE_NUMERIC)) continue; Font font = sheet.getWorkbook().getFontAt(c.getCellStyle().getFontIndex()); String s = c.getCellType() == Cell.CELL_TYPE_STRING ? c.getStringCellValue() : "" + c.getNumericCellValue(); String[] lines = MiscUtils.split(s, "\n"); int maxLen = 1; for (int i = 0; i < lines.length; i++) { maxLen = Math.max(lines[i].length(), maxLen); } if (font.getFontHeightInPoints() < 12) { col2lens.add(col, 700 + maxLen * (font.getFontHeightInPoints() + (font.getBoldweight() > 500 ? 1 : 0)) * 20); } maxH = (short) Math.max(maxH, 50 + lines.length * (font.getFontHeight() * 1.2)); } if (resizeHeight) r.setHeight(maxH); } for (int col : col2lens.keySet()) { List<Integer> lens = col2lens.get(col); Collections.sort(lens); int len = lens.get(lens.size() - 1); if (lens.size() > 10 && lens.get(lens.size() - 1) > 2 * lens.get(lens.size() - 2)) { len = lens.get(lens.size() - 2); } sheet.setColumnWidth(col, Math.max(Math.min((int) (len * 1.25), maxColWidth > 0 ? maxColWidth : 300000), 1500)); } }
From source file:com.ben12.reta.util.RETAAnalysis.java
License:Open Source License
public void writeExcel(Window parent) throws IOException, InvalidFormatException { logger.info("Start write excel output"); Path outputFile = Paths.get(output); if (!outputFile.isAbsolute()) { Path root = config.getAbsoluteFile().getParentFile().toPath(); outputFile = root.resolve(outputFile); }/* w ww .j a v a 2 s . co m*/ // test using template InputStream is = getClass().getResourceAsStream("/com/ben12/reta/resources/template/template.xlsx"); ExcelTransformer transformer = new ExcelTransformer(); List<String> sheetNames = new ArrayList<>(); List<String> sheetTemplateNames = new ArrayList<>(); for (InputRequirementSource requirementSource : requirementSources.values()) { sheetTemplateNames.add("DOCUMENT"); sheetTemplateNames.add("COVERAGE"); sheetNames.add(requirementSource.getName()); sheetNames.add(requirementSource.getName() + " coverage"); } List<Map<String, Object>> sheetValues = new ArrayList<>(); for (InputRequirementSource source : requirementSources.values()) { Map<String, Object> values = new HashMap<>(); values.put("source", source); values.put("null", null); values.put("line", "\n"); Set<String> attributes = new LinkedHashSet<>(); attributes.add(Requirement.ATTRIBUTE_ID); if (source.getAttributesGroup().containsKey(Requirement.ATTRIBUTE_VERSION)) { attributes.add(Requirement.ATTRIBUTE_VERSION); } attributes.addAll(source.getAttributesGroup().keySet()); attributes.remove(Requirement.ATTRIBUTE_TEXT); values.put("attributes", attributes); Set<String> refAttributes = new LinkedHashSet<>(); refAttributes.add(Requirement.ATTRIBUTE_ID); if (source.getRefAttributesGroup().containsKey(Requirement.ATTRIBUTE_VERSION)) { refAttributes.add(Requirement.ATTRIBUTE_VERSION); } refAttributes.addAll(source.getRefAttributesGroup().keySet()); refAttributes.remove(Requirement.ATTRIBUTE_TEXT); values.put("refAttributes", refAttributes); sheetValues.add(values); sheetValues.add(values); } Workbook wb = transformer.transform(is, sheetTemplateNames, sheetNames, sheetValues); int sheetCount = wb.getNumberOfSheets(); for (int i = 0; i < sheetCount; i++) { Sheet sheet = wb.getSheetAt(i); int columns = 0; for (int j = 0; j <= sheet.getLastRowNum(); j++) { Row row = sheet.getRow(j); if (row != null) { row.setHeight((short) -1); columns = Math.max(columns, row.getLastCellNum() + 1); } } for (int j = 0; j < columns; j++) { sheet.autoSizeColumn(j); } } try (FileOutputStream fos = new FileOutputStream(outputFile.toFile())) { wb.write(fos); } catch (FileNotFoundException e) { int confirm = MessageDialog.showQuestionMessage(null, "Excel output file must be closed."); if (confirm == MessageDialog.OK_OPTION) { try (FileOutputStream fos = new FileOutputStream(outputFile.toFile())) { wb.write(fos); } catch (IOException e2) { throw e2; } } else { throw e; } } logger.info("End write excel output"); }
From source file:com.biomeris.i2b2.export.engine.ExcelCreator.java
License:Open Source License
public void writeHeader(List<String> headerElements) { Row headerRow_XLSX = currentSheet.createRow(currentSheetRow++); headerRow_XLSX.setHeight((short) 350); for (int i = 0; i < headerElements.size(); i++) { Cell c = headerRow_XLSX.createCell(i); c.setCellStyle(headerStyle);// w w w. java2 s. co m c.setCellValue(headerElements.get(i)); } }
From source file:com.dituiba.excel.BaseExcelService.java
License:Apache License
/*** * ?hash?Excel// w ww.j a va 2s . com * @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")); }
From source file:com.dituiba.excel.ExcelUtility.java
License:Apache License
public static void copyRows(Sheet st, int startRow, int endRow, int pPosition) { int pStartRow = startRow; int pEndRow = endRow; int targetRowFrom; int targetRowTo; int columnCount; CellRangeAddress region = null;//from w w w.j a va2 s . co m int i; int j; for (i = 0; i < st.getNumMergedRegions(); i++) { region = st.getMergedRegion(i); if ((region.getFirstRow() >= pStartRow) && (region.getLastRow() <= pEndRow)) { targetRowFrom = region.getFirstRow() - pStartRow + pPosition; targetRowTo = region.getLastRow() - pStartRow + pPosition; CellRangeAddress newRegion = region.copy(); newRegion.setFirstRow(targetRowFrom); newRegion.setFirstColumn(region.getFirstColumn()); newRegion.setLastRow(targetRowTo); newRegion.setLastColumn(region.getLastColumn()); st.addMergedRegion(newRegion); } } //set the column height and value for (i = pStartRow; i <= pEndRow; i++) { Row sourceRow = st.getRow(i); columnCount = sourceRow.getLastCellNum(); if (sourceRow != null) { Row newRow = st.createRow(pPosition + i); newRow.setHeight(sourceRow.getHeight()); for (j = 0; j < columnCount; j++) { Cell templateCell = sourceRow.getCell(j); if (templateCell != null) { Cell newCell = newRow.createCell(j); copyCell(templateCell, newCell); } } } } }
From source file:com.dituiba.excel.ExportExcelService.java
License:Apache License
/** * ?//w w w.j a v a 2 s. c o m * @return */ private int createHead() { List<String> columnBeanJson = new ArrayList<String>(0); List<String> columns = new ArrayList<String>(); for (Field field : dataBean.getFiledList()) { if (field.isAnnotationPresent(ExcelColumnGroup.class)) { ExcelColumnGroup columnGroup = field.getAnnotation(ExcelColumnGroup.class); GroupConfig group = groupConfig.get(field.getName()); if (!BaseExcelVo.class.isAssignableFrom(columnGroup.type())) {//List? for (int i = 0; i < group.getLength(); i++) { ColumnBean columnBean = new ColumnBean(); columnBean.setColumnName(field.getName()); columnBean.setSize(1); columnBean.setLength(group.getLength()); TookPairs tookPairs = tookMap.get(field.getName() + i); if (tookPairs != null) { String took = tookPairs.getValue(); if (!ObjectHelper.isNullOrEmptyString(took)) { columnBean.setTookValue(took); } } columnBeanJson.add(JsonUtil.toJSON(columnBean)); columns.add(group.getLangName(0, i)); } } else {//List??? if (ObjectHelper.isEmpty(group.getFieldNames())) { String[] filedNames = dataBean.getChildDataBean(field.getName()).getFiledNames(); group.setFieldNames(Arrays.asList(filedNames)); } for (int i = 0; i < group.getLength(); i++) { for (int j = 0; j < group.getFieldNames().size(); j++) { ColumnBean columnBean = new ColumnBean(); columnBean.setColumnName(field.getName()); columnBean.setSize(group.getGroupSize()); columnBean.setLength(group.getLength()); columnBean.setInnerColumn(group.getFieldNames().get(j)); TookPairs tookPairs = tookMap.get(columnBean.getInnerColumn() + i); if (tookPairs != null && !ObjectHelper.isNullOrEmptyString(tookPairs.getValue())) { columnBean.setTookName(tookPairs.getSourceField()); columnBean.setTookValue(tookPairs.getValue()); } columnBeanJson.add(JsonUtil.toJSON(columnBean)); columns.add(group.getLangName(j, i)); } } } } else { ColumnBean columnBean = new ColumnBean(); columnBean.setColumnName(field.getName()); TookPairs tookPairs = tookMap.get(field.getName() + 0); if (tookPairs != null && !ObjectHelper.isNullOrEmptyString(tookPairs.getValue())) { columnBean.setTookValue(tookPairs.getValue()); } columnBeanJson.add(JsonUtil.toJSON(columnBean)); columns.add(language.translate(field)); } } sheet.createRow(HASH_ROW); addTitle(sheet, TITLE_ROW, columnBeanJson.size(), title); Row row = addRow(sheet, HIDDEN_FIELD_HEAD, columnBeanJson.toArray(new String[] {})); addRow(sheet, COLUMN_ROW, columns.toArray(new String[] {})); row.setHeight(Short.valueOf("0")); return columnBeanJson.size(); }
From source file:com.dituiba.excel.SimpleExportService.java
License:Apache License
/** * ?// w w w.j a v a 2s . c om */ 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("?"); }