Search in sources :

Example 6 with HSSFCellStyle

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

the class NewLinesInCells method main.

public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    HSSFRow r = null;
    HSSFCell c = null;
    HSSFCellStyle cs = wb.createCellStyle();
    HSSFFont f2 = wb.createFont();
    cs = wb.createCellStyle();
    cs.setFont(f2);
    // Word Wrap MUST be turned on
    cs.setWrapText(true);
    r = s.createRow(2);
    r.setHeight((short) 0x349);
    c = r.createCell(2);
    c.setCellType(CellType.STRING);
    c.setCellValue("Use \n with word wrap on to create a new line");
    c.setCellStyle(cs);
    s.setColumnWidth(2, (int) ((50 * 8) / ((double) 1 / 20)));
    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 7 with HSSFCellStyle

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

the class InCellLists method multiLevelBulletedListInCell.

/**
     * Insert a bulleted multi-level list into a cell.
     *
     * @param workbook A reference to the HSSFWorkbook that 'contains' the
     *                 cell.
     * @param multiLevelListItems An ArrayList whose elements contain instances
     *                            of the MultiLevelListItem class. Each element
     *                            encapsulates the text for the high level item
     *                            along with an ArrayList. Each element of this
     *                            ArrayList encapsulates the text for a lower
     *                            level item.
     * @param cell An instance of the HSSFCell class that encapsulates a
     *             reference to the spreadsheet cell into which the list
     *             will be written.
     */
public void multiLevelBulletedListInCell(HSSFWorkbook workbook, ArrayList<MultiLevelListItem> multiLevelListItems, HSSFCell cell) {
    StringBuilder buffer = new StringBuilder();
    // Note that again, an HSSFCellStye object is required and that
    // it's wrap text property should be set to 'true'
    HSSFCellStyle wrapStyle = workbook.createCellStyle();
    wrapStyle.setWrapText(true);
    // Step through the ArrayList of MultilLevelListItem instances.
    for (MultiLevelListItem multiLevelListItem : multiLevelListItems) {
        // For each element in the ArrayList, get the text for the high
        // level list item......
        buffer.append(InCellLists.BULLET_CHARACTER);
        buffer.append(" ");
        buffer.append(multiLevelListItem.getItemText());
        buffer.append("\n");
        // and then an ArrayList whose elements encapsulate the text
        // for the lower level list items.
        ArrayList<String> lowerLevelItems = multiLevelListItem.getLowerLevelItems();
        if (!(lowerLevelItems == null) && !(lowerLevelItems.isEmpty())) {
            for (String item : lowerLevelItems) {
                buffer.append(InCellLists.TAB);
                buffer.append(InCellLists.BULLET_CHARACTER);
                buffer.append(" ");
                buffer.append(item);
                buffer.append("\n");
            }
        }
    }
    // The StringBuffer's contents are the source for the contents
    // of the cell.
    cell.setCellValue(new HSSFRichTextString(buffer.toString().trim()));
    cell.setCellStyle(wrapStyle);
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Example 8 with HSSFCellStyle

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

the class InCellLists method multiLevelListInCell.

/**
     * Insert a multi-level list into a cell.
     *
     * @param workbook A reference to the HSSFWorkbook that 'contains' the
     *                 cell.
     * @param multiLevelListItems An ArrayList whose elements contain instances
     *                            of the MultiLevelListItem class. Each element
     *                            encapsulates the text for the high level item
     *                            along with an ArrayList. Each element of this
     *                            ArrayList encapsulates the text for a lower
     *                            level item.
     * @param cell An instance of the HSSFCell class that encapsulates a
     *             reference to the spreadsheet cell into which the list
     *             will be written.
     */
public void multiLevelListInCell(HSSFWorkbook workbook, ArrayList<MultiLevelListItem> multiLevelListItems, HSSFCell cell) {
    StringBuilder buffer = new StringBuilder();
    // Note that again, an HSSFCellStye object is required and that
    // it's wrap text property should be set to 'true'
    HSSFCellStyle wrapStyle = workbook.createCellStyle();
    wrapStyle.setWrapText(true);
    // Step through the ArrayList of MultilLevelListItem instances.
    for (MultiLevelListItem multiLevelListItem : multiLevelListItems) {
        // For each element in the ArrayList, get the text for the high
        // level list item......
        buffer.append(multiLevelListItem.getItemText());
        buffer.append("\n");
        // and then an ArrayList whose elements encapsulate the text
        // for the lower level list items.
        ArrayList<String> lowerLevelItems = multiLevelListItem.getLowerLevelItems();
        if (!(lowerLevelItems == null) && !(lowerLevelItems.isEmpty())) {
            for (String item : lowerLevelItems) {
                buffer.append(InCellLists.TAB);
                buffer.append(item);
                buffer.append("\n");
            }
        }
    }
    // The StringBuffer's contents are the source for the contents
    // of the cell.
    cell.setCellValue(new HSSFRichTextString(buffer.toString().trim()));
    cell.setCellStyle(wrapStyle);
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Example 9 with HSSFCellStyle

use of org.apache.poi.hssf.usermodel.HSSFCellStyle 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 10 with HSSFCellStyle

use of org.apache.poi.hssf.usermodel.HSSFCellStyle 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)

Aggregations

HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)35 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)19 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)18 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)16 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)16 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)12 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)12 FileOutputStream (java.io.FileOutputStream)9 Region (org.apache.poi.hssf.util.Region)2 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)2 Element (org.w3c.dom.Element)2 Text (org.w3c.dom.Text)2 Color (java.awt.Color)1 Font (java.awt.Font)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Timestamp (java.sql.Timestamp)1 DecimalFormat (java.text.DecimalFormat)1 Date (java.util.Date)1 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)1