Search in sources :

Example 6 with HSSFFont

use of org.apache.poi.hssf.usermodel.HSSFFont in project poi by apache.

the class SVTableCellEditor method getTableCellEditorComponent.

/**
   *  Gets the tableCellEditorComponent attribute of the SVTableCellEditor object
   *
   * @return             The tableCellEditorComponent value
   */
@Override
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.getBold();
        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) {
            //fix for stupid ol Windows
            fontheight = 10;
        }
        Font font = new Font(f.getFontName(), fontstyle, fontheight);
        editor.setFont(font);
        if (style.getFillPatternEnum() == FillPatternType.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.getCellTypeEnum()) {
            case BLANK:
                editor.setText("");
                break;
            case BOOLEAN:
                if (cell.getBooleanCellValue()) {
                    editor.setText("true");
                } else {
                    editor.setText("false");
                }
                break;
            case NUMERIC:
                editor.setText(Double.toString(cell.getNumericCellValue()));
                break;
            case STRING:
                editor.setText(cell.getRichStringCellValue().getString());
                break;
            case FORMULA:
            default:
                editor.setText("?");
        }
        switch(style.getAlignmentEnum()) {
            case LEFT:
            case JUSTIFY:
            case FILL:
                editor.setHorizontalAlignment(SwingConstants.LEFT);
                break;
            case CENTER:
            case CENTER_SELECTION:
                editor.setHorizontalAlignment(SwingConstants.CENTER);
                break;
            case GENERAL:
            case RIGHT:
                editor.setHorizontalAlignment(SwingConstants.RIGHT);
                break;
            default:
                editor.setHorizontalAlignment(SwingConstants.LEFT);
                break;
        }
    }
    return editor;
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) Font(java.awt.Font) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont)

Example 7 with HSSFFont

use of org.apache.poi.hssf.usermodel.HSSFFont in project poi by apache.

the class SVTableCellRenderer method getTableCellRendererComponent.

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    boolean isBorderSet = false;
    //If the JTables default cell renderer has been setup correctly the
    //value will be the HSSFCell that we are trying to render
    HSSFCell c = (HSSFCell) value;
    if (c != null) {
        HSSFCellStyle s = c.getCellStyle();
        HSSFFont f = wb.getFontAt(s.getFontIndex());
        setFont(SVTableUtils.makeFont(f));
        if (s.getFillPatternEnum() == FillPatternType.SOLID_FOREGROUND) {
            setBackground(SVTableUtils.getAWTColor(s.getFillForegroundColor(), SVTableUtils.white));
        } else
            setBackground(SVTableUtils.white);
        setForeground(SVTableUtils.getAWTColor(f.getColor(), SVTableUtils.black));
        cellBorder.setBorder(SVTableUtils.getAWTColor(s.getTopBorderColor(), SVTableUtils.black), SVTableUtils.getAWTColor(s.getRightBorderColor(), SVTableUtils.black), SVTableUtils.getAWTColor(s.getBottomBorderColor(), SVTableUtils.black), SVTableUtils.getAWTColor(s.getLeftBorderColor(), SVTableUtils.black), s.getBorderTopEnum(), s.getBorderRightEnum(), s.getBorderBottomEnum(), s.getBorderLeftEnum(), hasFocus);
        setBorder(cellBorder);
        isBorderSet = true;
        //Set the value that is rendered for the cell
        switch(c.getCellTypeEnum()) {
            case BLANK:
                setValue("");
                break;
            case BOOLEAN:
                if (c.getBooleanCellValue()) {
                    setValue("true");
                } else {
                    setValue("false");
                }
                break;
            case NUMERIC:
                short format = s.getDataFormat();
                double numericValue = c.getNumericCellValue();
                if (cellFormatter.useRedColor(format, numericValue))
                    setForeground(Color.red);
                else
                    setForeground(null);
                setValue(cellFormatter.format(format, c.getNumericCellValue()));
                break;
            case STRING:
                setValue(c.getRichStringCellValue().getString());
                break;
            case FORMULA:
            default:
                setValue("?");
        }
        //Set the text alignment of the cell
        switch(s.getAlignmentEnum()) {
            case LEFT:
            case JUSTIFY:
            case FILL:
                setHorizontalAlignment(SwingConstants.LEFT);
                break;
            case CENTER:
            case CENTER_SELECTION:
                setHorizontalAlignment(SwingConstants.CENTER);
                break;
            case GENERAL:
            case RIGHT:
                setHorizontalAlignment(SwingConstants.RIGHT);
                break;
            default:
                setHorizontalAlignment(SwingConstants.LEFT);
                break;
        }
    } else {
        setValue("");
        setBackground(SVTableUtils.white);
    }
    if (hasFocus) {
        if (!isBorderSet) {
            //This is the border to paint when there is no border
            //and the cell has focus
            cellBorder.setBorder(SVTableUtils.black, SVTableUtils.black, SVTableUtils.black, SVTableUtils.black, BorderStyle.NONE, BorderStyle.NONE, BorderStyle.NONE, BorderStyle.NONE, isSelected);
            setBorder(cellBorder);
        }
        if (table.isCellEditable(row, column)) {
            setForeground(UIManager.getColor("Table.focusCellForeground"));
            setBackground(UIManager.getColor("Table.focusCellBackground"));
        }
    } else if (!isBorderSet) {
        setBorder(noFocusBorder);
    }
    // ---- begin optimization to avoid painting background ----
    Color back = getBackground();
    boolean colorMatch = (back != null) && (back.equals(table.getBackground())) && table.isOpaque();
    setOpaque(!colorMatch);
    // ---- end optimization to aviod painting background ----
    return this;
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) Color(java.awt.Color) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont)

Example 8 with HSSFFont

use of org.apache.poi.hssf.usermodel.HSSFFont in project poi by apache.

the class WorkingWithFonts method main.

public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow(1);
    // Create a new font and alter it.
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 24);
    font.setFontName("Courier New");
    font.setItalic(true);
    font.setStrikeout(true);
    // Fonts are set into a style so create a new one to use.
    HSSFCellStyle style = wb.createCellStyle();
    style.setFont(font);
    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell(1);
    cell.setCellValue("This is a test of fonts");
    cell.setCellStyle(style);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) FileOutputStream(java.io.FileOutputStream) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 9 with HSSFFont

use of org.apache.poi.hssf.usermodel.HSSFFont in project poi by apache.

the class CellStyleDetails method main.

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        throw new IllegalArgumentException("Filename must be given");
    }
    Workbook wb = WorkbookFactory.create(new File(args[0]));
    DataFormatter formatter = new DataFormatter();
    for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) {
        Sheet sheet = wb.getSheetAt(sn);
        System.out.println("Sheet #" + sn + " : " + sheet.getSheetName());
        for (Row row : sheet) {
            System.out.println("  Row " + row.getRowNum());
            for (Cell cell : row) {
                CellReference ref = new CellReference(cell);
                System.out.print("    " + ref.formatAsString());
                System.out.print(" (" + cell.getColumnIndex() + ") ");
                CellStyle style = cell.getCellStyle();
                System.out.print("Format=" + style.getDataFormatString() + " ");
                System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");
                System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");
                Font font = wb.getFontAt(style.getFontIndex());
                System.out.print("Font=" + font.getFontName() + " ");
                System.out.print("FontColor=");
                if (font instanceof HSSFFont) {
                    System.out.print(renderColor(((HSSFFont) font).getHSSFColor((HSSFWorkbook) wb)));
                }
                if (font instanceof XSSFFont) {
                    System.out.print(renderColor(((XSSFFont) font).getXSSFColor()));
                }
                System.out.println();
                System.out.println("        " + formatter.formatCellValue(cell));
            }
        }
        System.out.println();
    }
    wb.close();
}
Also used : CellReference(org.apache.poi.ss.util.CellReference) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) Font(org.apache.poi.ss.usermodel.Font) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) File(java.io.File) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter)

Example 10 with HSSFFont

use of org.apache.poi.hssf.usermodel.HSSFFont in project poi by apache.

the class ExcelToFoConverter method processCellStyle.

protected void processCellStyle(HSSFWorkbook workbook, HSSFCellStyle cellStyle, Element cellTarget, Element blockTarget) {
    blockTarget.setAttribute("white-space-collapse", "false");
    {
        String textAlign = ExcelToFoUtils.getAlign(cellStyle.getAlignment());
        if (ExcelToFoUtils.isNotEmpty(textAlign))
            blockTarget.setAttribute("text-align", textAlign);
    }
    if (cellStyle.getFillPattern() == 0) {
    // no fill
    } else if (cellStyle.getFillPattern() == 1) {
        final HSSFColor foregroundColor = cellStyle.getFillForegroundColorColor();
        if (foregroundColor != null)
            cellTarget.setAttribute("background-color", ExcelToFoUtils.getColor(foregroundColor));
    } else {
        final HSSFColor backgroundColor = cellStyle.getFillBackgroundColorColor();
        if (backgroundColor != null)
            cellTarget.setAttribute("background-color", ExcelToHtmlUtils.getColor(backgroundColor));
    }
    processCellStyleBorder(workbook, cellTarget, "top", cellStyle.getBorderTopEnum(), cellStyle.getTopBorderColor());
    processCellStyleBorder(workbook, cellTarget, "right", cellStyle.getBorderRightEnum(), cellStyle.getRightBorderColor());
    processCellStyleBorder(workbook, cellTarget, "bottom", cellStyle.getBorderBottomEnum(), cellStyle.getBottomBorderColor());
    processCellStyleBorder(workbook, cellTarget, "left", cellStyle.getBorderLeftEnum(), cellStyle.getLeftBorderColor());
    HSSFFont font = cellStyle.getFont(workbook);
    processCellStyleFont(workbook, blockTarget, font);
}
Also used : HSSFColor(org.apache.poi.hssf.util.HSSFColor) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Aggregations

HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)21 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)16 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)12 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)10 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)9 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)9 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)8 FileOutputStream (java.io.FileOutputStream)7 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)2 HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)2 HSSFColor (org.apache.poi.hssf.util.HSSFColor)2 Region (org.apache.poi.hssf.util.Region)2 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)2 Color (java.awt.Color)1 Font (java.awt.Font)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 DecimalFormat (java.text.DecimalFormat)1 HSSFComment (org.apache.poi.hssf.usermodel.HSSFComment)1