Search in sources :

Example 11 with HSSFCellStyle

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

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

the class InCellLists method bulletedListInCell.

/**
     * Insert a bulleted list into a cell.
     *
     * @param workbook A reference to the HSSFWorkbook that 'contains' the
     *                 cell.
     * @param listItems An ArrayList whose elements encapsulate the text for
     *                  the lists items.
     * @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 bulletedListInCell(HSSFWorkbook workbook, ArrayList<String> listItems, 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);
    // with one difference; the bullet character prefixed to the items text.
    for (String listItem : listItems) {
        buffer.append(InCellLists.BULLET_CHARACTER + " ");
        buffer.append(listItem);
        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 13 with HSSFCellStyle

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

the class InCellLists method bulletedItemInCell.

/**
     * Inserts a single bulleted item into a cell.
     *
     * @param workbook A reference to the HSSFWorkbook that 'contains' the
     *                 cell.
     * @param listItem An instance of the String class encapsulating the
     *                 items text.
     * @param cell An instance of the HSSFCell class that encapsulates a
     *             reference to the spreadsheet cell into which the list item
     *             will be written.
     */
public void bulletedItemInCell(HSSFWorkbook workbook, String listItem, HSSFCell cell) {
    // A format String must be built to ensure that the contents of the
    // cell appear as a bulleted item.
    HSSFDataFormat format = workbook.createDataFormat();
    String formatString = InCellLists.BULLET_CHARACTER + " @";
    int formatIndex = format.getFormat(formatString);
    // Construct an HSSFCellStyle and set it's data formt to use the
    // object created above.
    HSSFCellStyle bulletStyle = workbook.createCellStyle();
    bulletStyle.setDataFormat((short) formatIndex);
    // Set the cells contents and style.
    cell.setCellValue(new HSSFRichTextString(listItem));
    cell.setCellStyle(bulletStyle);
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFDataFormat(org.apache.poi.hssf.usermodel.HSSFDataFormat) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Example 14 with HSSFCellStyle

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

the class TestXSSFCellStyle method testDefaultStyles.

@Test
public void testDefaultStyles() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFCellStyle style1 = wb1.createCellStyle();
    assertEquals(IndexedColors.AUTOMATIC.getIndex(), style1.getFillBackgroundColor());
    assertNull(style1.getFillBackgroundXSSFColor());
    assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb1));
    wb1.close();
    //compatibility with HSSF
    HSSFWorkbook wb2 = new HSSFWorkbook();
    HSSFCellStyle style2 = wb2.createCellStyle();
    assertEquals(style2.getFillBackgroundColor(), style1.getFillBackgroundColor());
    assertEquals(style2.getFillForegroundColor(), style1.getFillForegroundColor());
    assertEquals(style2.getFillPatternEnum(), style1.getFillPatternEnum());
    assertEquals(style2.getFillPattern(), style1.getFillPattern());
    assertEquals(style2.getLeftBorderColor(), style1.getLeftBorderColor());
    assertEquals(style2.getTopBorderColor(), style1.getTopBorderColor());
    assertEquals(style2.getRightBorderColor(), style1.getRightBorderColor());
    assertEquals(style2.getBottomBorderColor(), style1.getBottomBorderColor());
    assertEquals(style2.getBorderBottomEnum(), style1.getBorderBottomEnum());
    assertEquals(style2.getBorderLeftEnum(), style1.getBorderLeftEnum());
    assertEquals(style2.getBorderRightEnum(), style1.getBorderRightEnum());
    assertEquals(style2.getBorderTopEnum(), style1.getBorderTopEnum());
    wb2.close();
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 15 with HSSFCellStyle

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

the class ExcelToFoConverter method processCell.

protected boolean processCell(HSSFWorkbook workbook, HSSFCell cell, Element tableCellElement, int normalWidthPx, int maxSpannedWidthPx, float normalHeightPt) {
    final HSSFCellStyle cellStyle = cell.getCellStyle();
    String value;
    switch(cell.getCellTypeEnum()) {
        case STRING:
            // XXX: enrich
            value = cell.getRichStringCellValue().getString();
            break;
        case FORMULA:
            switch(cell.getCachedFormulaResultTypeEnum()) {
                case STRING:
                    HSSFRichTextString str = cell.getRichStringCellValue();
                    if (str != null && str.length() > 0) {
                        value = (str.toString());
                    } else {
                        value = ExcelToHtmlUtils.EMPTY;
                    }
                    break;
                case NUMERIC:
                    double nValue = cell.getNumericCellValue();
                    short df = cellStyle.getDataFormat();
                    String dfs = cellStyle.getDataFormatString();
                    value = _formatter.formatRawCellContents(nValue, df, dfs);
                    break;
                case BOOLEAN:
                    value = Boolean.toString(cell.getBooleanCellValue());
                    break;
                case ERROR:
                    value = ErrorEval.getText(cell.getErrorCellValue());
                    break;
                default:
                    logger.log(POILogger.WARN, "Unexpected cell cachedFormulaResultType (" + cell.getCachedFormulaResultTypeEnum() + ")");
                    value = ExcelToHtmlUtils.EMPTY;
                    break;
            }
            break;
        case BLANK:
            value = ExcelToHtmlUtils.EMPTY;
            break;
        case NUMERIC:
            value = _formatter.formatCellValue(cell);
            break;
        case BOOLEAN:
            value = Boolean.toString(cell.getBooleanCellValue());
            break;
        case ERROR:
            value = ErrorEval.getText(cell.getErrorCellValue());
            break;
        default:
            logger.log(POILogger.WARN, "Unexpected cell type (" + cell.getCellTypeEnum() + ")");
            return true;
    }
    final boolean noText = ExcelToHtmlUtils.isEmpty(value);
    final boolean wrapInDivs = !noText && !cellStyle.getWrapText();
    final boolean emptyStyle = isEmptyStyle(cellStyle);
    if (!emptyStyle && noText) {
        /*
             * if cell style is defined (like borders, etc.) but cell text
             * is empty, add "&nbsp;" to output, so browser won't collapse
             * and ignore cell
             */
        value = " ";
    }
    if (isOutputLeadingSpacesAsNonBreaking() && value.startsWith(" ")) {
        StringBuilder builder = new StringBuilder();
        for (int c = 0; c < value.length(); c++) {
            if (value.charAt(c) != ' ') {
                break;
            }
            builder.append(' ');
        }
        if (value.length() != builder.length()) {
            builder.append(value.substring(builder.length()));
        }
        value = builder.toString();
    }
    Text text = foDocumentFacade.createText(value);
    Element block = foDocumentFacade.createBlock();
    if (wrapInDivs) {
        block.setAttribute("absolute-position", "fixed");
        block.setAttribute("left", "0px");
        block.setAttribute("top", "0px");
        block.setAttribute("bottom", "0px");
        block.setAttribute("min-width", normalWidthPx + "px");
        if (maxSpannedWidthPx != Integer.MAX_VALUE) {
            block.setAttribute("max-width", maxSpannedWidthPx + "px");
        }
        block.setAttribute("overflow", "hidden");
        block.setAttribute("height", normalHeightPt + "pt");
        block.setAttribute("keep-together.within-line", "always");
        block.setAttribute("wrap-option", "no-wrap");
    }
    processCellStyle(workbook, cell.getCellStyle(), tableCellElement, block);
    block.appendChild(text);
    tableCellElement.appendChild(block);
    return ExcelToHtmlUtils.isEmpty(value) && emptyStyle;
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

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