Search in sources :

Example 1 with HSSFCellStyle

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

the class AbstractExcelExporter method getStyle.

private HSSFCellStyle getStyle(int row, int col) {
    int displayType = getDisplayType(row, col);
    String key = "cell-" + col + "-" + displayType;
    HSSFCellStyle cs = m_styles.get(key);
    if (cs == null) {
        boolean isHighlightNegativeNumbers = true;
        cs = m_workbook.createCellStyle();
        HSSFFont font = getFont(false);
        cs.setFont(font);
        // Border
        cs.setBorderLeft((short) 1);
        cs.setBorderTop((short) 1);
        cs.setBorderRight((short) 1);
        cs.setBorderBottom((short) 1);
        //
        if (DisplayType.isDate(displayType)) {
            cs.setDataFormat(m_dataFormat.getFormat("DD.MM.YYYY"));
        } else if (DisplayType.isNumeric(displayType)) {
            DecimalFormat df = DisplayType.getNumberFormat(displayType, getLanguage());
            String format = getFormatString(df, isHighlightNegativeNumbers);
            cs.setDataFormat(m_dataFormat.getFormat(format));
        }
        m_styles.put(key, cs);
    }
    return cs;
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) DecimalFormat(java.text.DecimalFormat) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Example 2 with HSSFCellStyle

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

the class AbstractExcelExporter method getHeaderStyle.

private HSSFCellStyle getHeaderStyle(int col) {
    String key = "header-" + col;
    HSSFCellStyle cs_header = m_styles.get(key);
    if (cs_header == null) {
        HSSFFont font_header = getFont(true);
        cs_header = m_workbook.createCellStyle();
        cs_header.setFont(font_header);
        cs_header.setBorderLeft((short) 2);
        cs_header.setBorderTop((short) 2);
        cs_header.setBorderRight((short) 2);
        cs_header.setBorderBottom((short) 2);
        cs_header.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
        cs_header.setWrapText(true);
        m_styles.put(key, cs_header);
    }
    return cs_header;
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Example 3 with HSSFCellStyle

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

the class BigExample method main.

public static void main(String[] args) throws IOException {
    int rownum;
    // create a new workbook
    HSSFWorkbook wb = new HSSFWorkbook();
    try {
        // create a new sheet
        HSSFSheet s = wb.createSheet();
        // declare a row object reference
        HSSFRow r = null;
        // declare a cell object reference
        HSSFCell c = null;
        // create 3 cell styles
        HSSFCellStyle cs = wb.createCellStyle();
        HSSFCellStyle cs2 = wb.createCellStyle();
        HSSFCellStyle cs3 = wb.createCellStyle();
        // create 2 fonts objects
        HSSFFont f = wb.createFont();
        HSSFFont f2 = wb.createFont();
        //set font 1 to 12 point type
        f.setFontHeightInPoints((short) 12);
        //make it red
        f.setColor(HSSFColorPredefined.RED.getIndex());
        // make it bold
        //arial is the default font
        f.setBold(true);
        //set font 2 to 10 point type
        f2.setFontHeightInPoints((short) 10);
        //make it the color at palette index 0xf (white)
        f2.setColor(HSSFColorPredefined.WHITE.getIndex());
        //make it bold
        f2.setBold(true);
        //set cell stlye
        cs.setFont(f);
        //set the cell format see HSSFDataFromat for a full list
        cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
        //set a thin border
        cs2.setBorderBottom(BorderStyle.THIN);
        //fill w fg fill color
        cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // set foreground fill to red
        cs2.setFillForegroundColor(HSSFColorPredefined.RED.getIndex());
        // set the font
        cs2.setFont(f2);
        // set the sheet name to HSSF Test
        wb.setSheetName(0, "HSSF Test");
        // create a sheet with 300 rows (0-299)
        for (rownum = 0; rownum < 300; rownum++) {
            // create a row
            r = s.createRow(rownum);
            // on every other row
            if ((rownum % 2) == 0) {
                // make the row height bigger  (in twips - 1/20 of a point)
                r.setHeight((short) 0x249);
            }
            // create 50 cells (0-49) (the += 2 becomes apparent later
            for (int cellnum = 0; cellnum < 50; cellnum += 2) {
                // create a numeric cell
                c = r.createCell(cellnum);
                // do some goofy math to demonstrate decimals
                c.setCellValue(rownum * 10000 + cellnum + (((double) rownum / 1000) + ((double) cellnum / 10000)));
                // on every other row
                if ((rownum % 2) == 0) {
                    // set this cell to the first cell style we defined
                    c.setCellStyle(cs);
                }
                // create a string cell (see why += 2 in the
                c = r.createCell(cellnum + 1);
                // set the cell's string value to "TEST"
                c.setCellValue("TEST");
                // make this column a bit wider
                s.setColumnWidth(cellnum + 1, (int) ((50 * 8) / ((double) 1 / 20)));
                // on every other row
                if ((rownum % 2) == 0) {
                    // set this to the white on red cell style
                    // we defined above
                    c.setCellStyle(cs2);
                }
            }
        }
        //draw a thick black border on the row at the bottom using BLANKS
        // advance 2 rows
        rownum++;
        rownum++;
        r = s.createRow(rownum);
        // define the third style to be the default
        // except with a thick black border at the bottom
        cs3.setBorderBottom(BorderStyle.THICK);
        //create 50 cells
        for (int cellnum = 0; cellnum < 50; cellnum++) {
            //create a blank type cell (no value)
            c = r.createCell(cellnum);
            // set it to the thick black border style
            c.setCellStyle(cs3);
        }
        //end draw thick black border
        // demonstrate adding/naming and deleting a sheet
        // create a sheet, set its title then delete it
        wb.createSheet();
        wb.setSheetName(1, "DeletedSheet");
        wb.removeSheetAt(1);
        //end deleted sheet
        // create a new file
        FileOutputStream out = new FileOutputStream("workbook.xls");
        // write the workbook to the output stream
        // close our file (don't blow out our file handles
        wb.write(out);
        out.close();
    } finally {
        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 4 with HSSFCellStyle

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

the class Hyperlinks method main.

public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFCreationHelper helper = wb.getCreationHelper();
    //cell style for hyperlinks
    //by default hyperlinks are blue and underlined
    HSSFCellStyle hlink_style = wb.createCellStyle();
    HSSFFont hlink_font = wb.createFont();
    hlink_font.setUnderline(Font.U_SINGLE);
    hlink_font.setColor(HSSFColorPredefined.BLUE.getIndex());
    hlink_style.setFont(hlink_font);
    HSSFCell cell;
    HSSFSheet sheet = wb.createSheet("Hyperlinks");
    //URL
    cell = sheet.createRow(0).createCell(0);
    cell.setCellValue("URL Link");
    HSSFHyperlink link = helper.createHyperlink(HyperlinkType.URL);
    link.setAddress("http://poi.apache.org/");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);
    //link to a file in the current directory
    cell = sheet.createRow(1).createCell(0);
    cell.setCellValue("File Link");
    link = helper.createHyperlink(HyperlinkType.FILE);
    link.setAddress("link1.xls");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);
    //e-mail link
    cell = sheet.createRow(2).createCell(0);
    cell.setCellValue("Email Link");
    link = helper.createHyperlink(HyperlinkType.EMAIL);
    //note, if subject contains white spaces, make sure they are url-encoded
    link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);
    //link to a place in this workbook
    //create a target sheet and cell
    HSSFSheet sheet2 = wb.createSheet("Target Sheet");
    sheet2.createRow(0).createCell(0).setCellValue("Target Cell");
    cell = sheet.createRow(3).createCell(0);
    cell.setCellValue("Worksheet Link");
    link = helper.createHyperlink(HyperlinkType.DOCUMENT);
    link.setAddress("'Target Sheet'!A1");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);
    FileOutputStream out = new FileOutputStream("hssf-links.xls");
    wb.write(out);
    out.close();
    wb.close();
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFCreationHelper(org.apache.poi.hssf.usermodel.HSSFCreationHelper) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) FileOutputStream(java.io.FileOutputStream) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFHyperlink(org.apache.poi.hssf.usermodel.HSSFHyperlink)

Example 5 with HSSFCellStyle

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

the class InCellLists method multiLevelNumberedListInCell.

/**
     * 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.
     * @param highLevelStartingValue A primitive int containing the number
     *                               for the first high level item in the list.
     * @param highLevelIncrement A primitive int containing the value that
     *                           should be used to calculate the number of
     *                           subsequent high level item.
     * @param lowLevelStartingValue A primitive int will containing the number
     *                              for the first low level item associated
     *                              with a high level item.
     * @param lowLevelIncrement A primitive int containing the value that
     *                          should be used to calculate the number of
     *                          subsequent low level item.
     */
public void multiLevelNumberedListInCell(HSSFWorkbook workbook, ArrayList<MultiLevelListItem> multiLevelListItems, HSSFCell cell, int highLevelStartingValue, int highLevelIncrement, int lowLevelStartingValue, int lowLevelIncrement) {
    StringBuilder buffer = new StringBuilder();
    int highLevelItemNumber = highLevelStartingValue;
    // 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(highLevelItemNumber);
        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())) {
            int lowLevelItemNumber = lowLevelStartingValue;
            for (String item : lowerLevelItems) {
                buffer.append(InCellLists.TAB);
                buffer.append(highLevelItemNumber);
                buffer.append(".");
                buffer.append(lowLevelItemNumber);
                buffer.append(" ");
                buffer.append(item);
                buffer.append("\n");
                lowLevelItemNumber += lowLevelIncrement;
            }
        }
        highLevelItemNumber += highLevelIncrement;
    }
    // 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)

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