public void styleColor(Formatter out, String attr, Color color) { if (color == null) { return; } HSSFColor hSSFColor = (HSSFColor) color; short[] rgb = hSSFColor.getTriplet(); out.format(" %s: #%02x%02x%02x; %n", attr, rgb[0], rgb[1], rgb[2]); }
private void styleColor(Formatter out, String attr, short index) { HSSFColor color = colors.getColor(index); if (index == HSSF_AUTO.getIndex() || color == null) { out.format(" /* %s: index = %d */%n", attr, index); } else { short[] rgb = color.getTriplet(); out.format(" %s: #%02x%02x%02x; /* index = %d */%n", attr, rgb[0], rgb[1], rgb[2], index); } }
/** {@inheritDoc} */ public void apply(HSSFCell cell, HSSFCellStyle cellStyle, Map<String, String> style) { for (String pos : new String[] {TOP, RIGHT, BOTTOM, LEFT}) { String posName = StringUtils.capitalize(pos.toLowerCase()); // color String colorAttr = BORDER + "-" + pos + "-" + COLOR; HSSFColor poiColor = CssUtils.parseColor(cell.getSheet().getWorkbook(), style.get(colorAttr)); if (poiColor != null) { try { MethodUtils.invokeMethod(cellStyle, "set" + posName + "BorderColor", poiColor.getIndex()); } catch (Exception e) { log.error("Set Border Color Error Caused.", e); } } // width int width = CssUtils.getInt(style.get(BORDER + "-" + pos + "-" + WIDTH)); String styleAttr = BORDER + "-" + pos + "-" + STYLE; String styleValue = style.get(styleAttr); short shortValue = -1; // empty or solid if (StringUtils.isBlank(styleValue) || "solid".equals(styleValue)) { if (width > 2) { shortValue = CellStyle.BORDER_THICK; } else if (width > 1) { shortValue = CellStyle.BORDER_MEDIUM; } else { shortValue = CellStyle.BORDER_THIN; } } else if (ArrayUtils.contains(new String[] {NONE, HIDDEN}, styleValue)) { shortValue = CellStyle.BORDER_NONE; } else if (DOUBLE.equals(styleValue)) { shortValue = CellStyle.BORDER_DOUBLE; } else if (DOTTED.equals(styleValue)) { shortValue = CellStyle.BORDER_DOTTED; } else if (DASHED.equals(styleValue)) { if (width > 1) { shortValue = CellStyle.BORDER_MEDIUM_DASHED; } else { shortValue = CellStyle.BORDER_DASHED; } } // border style if (shortValue != -1) { try { MethodUtils.invokeMethod(cellStyle, "setBorder" + posName, shortValue); } catch (Exception e) { log.error("Set Border Style Error Caused.", e); } } } }
/** * Sheet Viewer Table Cell Editor -- not commented via javadoc as it nearly completely consists of * overridden methods. * * @author Jason Height */ public class SVTableCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener { private static final Color black = getAWTColor(new HSSFColor.BLACK()); private static final Color white = getAWTColor(new HSSFColor.WHITE()); private Hashtable colors = HSSFColor.getIndexHash(); private HSSFWorkbook wb; private JTextField editor; private HSSFCell editorValue; public SVTableCellEditor(HSSFWorkbook wb) { this.wb = wb; this.editor = new JTextField(); } /** * Gets the cellEditable attribute of the SVTableCellEditor object * * @return The cellEditable value */ public boolean isCellEditable(java.util.EventObject e) { if (e instanceof MouseEvent) { return ((MouseEvent) e).getClickCount() >= 2; } return false; } public boolean shouldSelectCell(EventObject anEvent) { return true; } public boolean startCellEditing(EventObject anEvent) { System.out.println("Start Cell Editing"); return true; } public boolean stopCellEditing() { System.out.println("Stop Cell Editing"); fireEditingStopped(); return true; } public void cancelCellEditing() { System.out.println("Cancel Cell Editing"); fireEditingCanceled(); } public void actionPerformed(ActionEvent e) { System.out.println("Action performed"); stopCellEditing(); } /** * Gets the cellEditorValue attribute of the SVTableCellEditor object * * @return The cellEditorValue value */ public Object getCellEditorValue() { System.out.println("GetCellEditorValue"); // JMH Look at when this method is called. Should it return a HSSFCell? return editor.getText(); } /** * Gets the tableCellEditorComponent attribute of the SVTableCellEditor object * * @return The tableCellEditorComponent value */ public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column) { System.out.println("GetTableCellEditorComponent"); HSSFCell cell = (HSSFCell) value; if (cell != null) { HSSFCellStyle style = cell.getCellStyle(); HSSFFont f = wb.getFontAt(style.getFontIndex()); boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL; boolean isitalics = f.getItalic(); int fontstyle = Font.PLAIN; if (isbold) fontstyle = Font.BOLD; if (isitalics) fontstyle = fontstyle | Font.ITALIC; int fontheight = f.getFontHeightInPoints(); if (fontheight == 9) fontheight = 10; // fix for stupid ol Windows Font font = new Font(f.getFontName(), fontstyle, fontheight); editor.setFont(font); if (style.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) { editor.setBackground(getAWTColor(style.getFillForegroundColor(), white)); } else editor.setBackground(white); editor.setForeground(getAWTColor(f.getColor(), black)); // Set the value that is rendered for the cell switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_BLANK: editor.setText(""); break; case HSSFCell.CELL_TYPE_BOOLEAN: if (cell.getBooleanCellValue()) { editor.setText("true"); } else { editor.setText("false"); } break; case HSSFCell.CELL_TYPE_NUMERIC: editor.setText(Double.toString(cell.getNumericCellValue())); break; case HSSFCell.CELL_TYPE_STRING: editor.setText(cell.getRichStringCellValue().getString()); break; case HSSFCell.CELL_TYPE_FORMULA: default: editor.setText("?"); } switch (style.getAlignment()) { case HSSFCellStyle.ALIGN_LEFT: case HSSFCellStyle.ALIGN_JUSTIFY: case HSSFCellStyle.ALIGN_FILL: editor.setHorizontalAlignment(SwingConstants.LEFT); break; case HSSFCellStyle.ALIGN_CENTER: case HSSFCellStyle.ALIGN_CENTER_SELECTION: editor.setHorizontalAlignment(SwingConstants.CENTER); break; case HSSFCellStyle.ALIGN_GENERAL: case HSSFCellStyle.ALIGN_RIGHT: editor.setHorizontalAlignment(SwingConstants.RIGHT); break; default: editor.setHorizontalAlignment(SwingConstants.LEFT); break; } } return editor; } /** This method retrieves the AWT Color representation from the colour hash table */ private final Color getAWTColor(int index, Color deflt) { HSSFColor clr = (HSSFColor) colors.get(Integer.valueOf(index)); if (clr == null) return deflt; return getAWTColor(clr); } private static final Color getAWTColor(HSSFColor clr) { short[] rgb = clr.getTriplet(); return new Color(rgb[0], rgb[1], rgb[2]); } }
private static final Color getAWTColor(HSSFColor clr) { short[] rgb = clr.getTriplet(); return new Color(rgb[0], rgb[1], rgb[2]); }
public static ColorInfo excelColor2UOF(Color color) { if (color == null) { return null; } ColorInfo ci = null; if (color instanceof XSSFColor) { // .xlsx XSSFColor xc = (XSSFColor) color; byte[] b = xc.getRgb(); if (b != null) { // 一定是argb ci = ColorInfo.fromARGB(b[0], b[1], b[2], b[3]); } } else if (color instanceof HSSFColor) { // .xls HSSFColor hc = (HSSFColor) color; short[] s = hc.getTriplet(); // 一定是rgb if (s != null) { ci = ColorInfo.fromARGB(s[0], s[1], s[2]); } } return ci; }