List of usage examples for org.apache.poi.xssf.usermodel XSSFCellStyle setBorderColor
public void setBorderColor(BorderSide side, XSSFColor color)
From source file:com.github.igor_kudryashov.utils.excel.ExcelWriter.java
License:Apache License
/** * Returns a style of cell//from ww w . jav a2 s .co m * * @param rownum * the number of row for count odd/even rows * @param entry * value of cell * @param header * <code>true</code> if this row is the header, otherwise * <code>false</code> * @return the cell style */ private XSSFCellStyle getCellStyle(int rownum, Object entry, boolean header) { XSSFCellStyle style; String name = entry.getClass().getName(); if (header) { name += "_header"; } else if ((rownum % 2) == 0) { name += "_even"; } if (styles.containsKey(name)) { // if we already have a style for this class, return it style = styles.get(name); } else { // create new style style = (XSSFCellStyle) workbook.createCellStyle(); style.setVerticalAlignment(VerticalAlignment.TOP); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, colorBorder); style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderColor(XSSFCellBorder.BorderSide.LEFT, colorBorder); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderColor(XSSFCellBorder.BorderSide.RIGHT, colorBorder); // format data XSSFDataFormat fmt = (XSSFDataFormat) workbook.createDataFormat(); short format = 0; if (name.contains("Date")) { format = fmt.getFormat(BuiltinFormats.getBuiltinFormat(0xe)); style.setAlignment(CellStyle.ALIGN_LEFT); } else if (name.contains("Double")) { format = fmt.getFormat(BuiltinFormats.getBuiltinFormat(2)); style.setAlignment(CellStyle.ALIGN_RIGHT); } else if (name.contains("Integer")) { format = fmt.getFormat(BuiltinFormats.getBuiltinFormat(1)); style.setAlignment(CellStyle.ALIGN_RIGHT); } else { style.setAlignment(CellStyle.ALIGN_LEFT); if (!header) { style.setWrapText(true); } } if (header) { // for header style.setFillForegroundColor(colorHeaderBackground); style.setFillPattern(CellStyle.SOLID_FOREGROUND); } else if (name.contains("_even")) { // for even rows style.setFillForegroundColor(colorEvenCellBackground); style.setFillPattern(CellStyle.SOLID_FOREGROUND); } style.setDataFormat(format); // keep the style for reuse styles.put(name, style); } return style; }
From source file:de.symeda.sormas.api.doc.UserRightsGenerator.java
License:Open Source License
@Test public void generateUserRights() throws FileNotFoundException, IOException { XSSFWorkbook workbook = new XSSFWorkbook(); // Create User Rights sheet String safeName = WorkbookUtil.createSafeSheetName("User Rights"); XSSFSheet sheet = workbook.createSheet(safeName); // Initialize cell styles // Authorized style XSSFCellStyle authorizedStyle = workbook.createCellStyle(); authorizedStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); authorizedStyle.setFillForegroundColor(new XSSFColor(new Color(0, 153, 0))); authorizedStyle.setBorderBottom(BorderStyle.THIN); authorizedStyle.setBorderLeft(BorderStyle.THIN); authorizedStyle.setBorderTop(BorderStyle.THIN); authorizedStyle.setBorderRight(BorderStyle.THIN); authorizedStyle.setBorderColor(BorderSide.BOTTOM, new XSSFColor(Color.BLACK)); authorizedStyle.setBorderColor(BorderSide.LEFT, new XSSFColor(Color.BLACK)); authorizedStyle.setBorderColor(BorderSide.TOP, new XSSFColor(Color.BLACK)); authorizedStyle.setBorderColor(BorderSide.RIGHT, new XSSFColor(Color.BLACK)); // Unauthorized style XSSFCellStyle unauthorizedStyle = workbook.createCellStyle(); unauthorizedStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); unauthorizedStyle.setFillForegroundColor(new XSSFColor(Color.RED)); unauthorizedStyle.setBorderBottom(BorderStyle.THIN); unauthorizedStyle.setBorderLeft(BorderStyle.THIN); unauthorizedStyle.setBorderTop(BorderStyle.THIN); unauthorizedStyle.setBorderRight(BorderStyle.THIN); unauthorizedStyle.setBorderColor(BorderSide.BOTTOM, new XSSFColor(Color.BLACK)); unauthorizedStyle.setBorderColor(BorderSide.LEFT, new XSSFColor(Color.BLACK)); unauthorizedStyle.setBorderColor(BorderSide.TOP, new XSSFColor(Color.BLACK)); unauthorizedStyle.setBorderColor(BorderSide.RIGHT, new XSSFColor(Color.BLACK)); // Bold style XSSFFont boldFont = workbook.createFont(); boldFont.setBold(true);//from w w w .j a va2s . c o m XSSFCellStyle boldStyle = workbook.createCellStyle(); boldStyle.setFont(boldFont); int rowCounter = 0; // Header Row headerRow = sheet.createRow(rowCounter++); Cell userRightHeadlineCell = headerRow.createCell(0); userRightHeadlineCell.setCellValue("User Right"); userRightHeadlineCell.setCellStyle(boldStyle); Cell descHeadlineCell = headerRow.createCell(1); descHeadlineCell.setCellValue("Description"); descHeadlineCell.setCellStyle(boldStyle); sheet.setColumnWidth(0, 256 * 35); sheet.setColumnWidth(1, 256 * 50); for (UserRole userRole : UserRole.values()) { String columnCaption = userRole.toString(); Cell headerCell = headerRow.createCell(userRole.ordinal() + 2); headerCell.setCellValue(columnCaption); headerCell.setCellStyle(boldStyle); sheet.setColumnWidth(userRole.ordinal() + 2, 256 * 14); } // User right rows for (UserRight userRight : UserRight.values()) { Row row = sheet.createRow(rowCounter++); // User right name Cell nameCell = row.createCell(0); nameCell.setCellValue(userRight.name()); nameCell.setCellStyle(boldStyle); // User right description Cell descCell = row.createCell(1); descCell.setCellValue(userRight.toString()); // Add styled cells for all user roles for (UserRole userRole : UserRole.values()) { Cell roleRightCell = row.createCell(userRole.ordinal() + 2); if (userRole.hasDefaultRight(userRight)) { roleRightCell.setCellStyle(authorizedStyle); roleRightCell.setCellValue("Yes"); } else { roleRightCell.setCellStyle(unauthorizedStyle); roleRightCell.setCellValue("No"); } } } XssfHelper.addAboutSheet(workbook); String filePath = "src/main/resources/doc/SORMAS_User_Rights.xlsx"; try (OutputStream fileOut = new FileOutputStream(filePath)) { workbook.write(fileOut); } workbook.close(); // Desktop.getDesktop().open(new File(filePath)); }
From source file:org.cgiar.ccafs.ap.summaries.projects.xlsx.BaseXLS.java
License:Open Source License
private void setBottomBorderCell(XSSFCellStyle cellStyle, Color color) { // Create the border cellStyle.setBorderBottom(CELL_BORDER_TYPE_BOTTOM); // Set color border cellStyle.setBorderColor(BorderSide.BOTTOM, new XSSFColor(color)); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); }
From source file:org.pentaho.reporting.engine.classic.core.modules.output.table.xls.helper.ExcelCellStyleBuilder.java
License:Open Source License
void xlsx_backgroundStyle(final CellBackground bg, final HSSFCellStyleProducer.HSSFCellStyleKey styleKey) { final XSSFCellStyle xssfCellStyle = (XSSFCellStyle) hssfCellStyle; if (BorderStyle.NONE.equals(bg.getBottom().getBorderStyle()) == false) { hssfCellStyle.setBorderBottom(styleKey.getBorderStrokeBottom()); xssfCellStyle.setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, new XSSFColor(styleKey.getExtendedColorBottom())); }/*ww w .j a v a2s . co m*/ if (BorderStyle.NONE.equals(bg.getTop().getBorderStyle()) == false) { hssfCellStyle.setBorderTop(styleKey.getBorderStrokeTop()); xssfCellStyle.setBorderColor(XSSFCellBorder.BorderSide.TOP, new XSSFColor(styleKey.getExtendedColorTop())); } if (BorderStyle.NONE.equals(bg.getLeft().getBorderStyle()) == false) { hssfCellStyle.setBorderLeft(styleKey.getBorderStrokeLeft()); xssfCellStyle.setBorderColor(XSSFCellBorder.BorderSide.LEFT, new XSSFColor(styleKey.getExtendedColorLeft())); } if (BorderStyle.NONE.equals(bg.getRight().getBorderStyle()) == false) { hssfCellStyle.setBorderRight(styleKey.getBorderStrokeRight()); xssfCellStyle.setBorderColor(XSSFCellBorder.BorderSide.RIGHT, new XSSFColor(styleKey.getExtendedColorRight())); } if (bg.getBackgroundColor() != null) { xssfCellStyle.setFillForegroundColor(new XSSFColor(styleKey.getExtendedColor())); hssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); } }