Package org.apache.poi.ss.usermodel

Examples of org.apache.poi.ss.usermodel.DataFormat


    // we style the second cell as a date (and time). It is important to
    // create a new cell style from the workbook
    // otherwise you can end up modifying the built in style and effecting
    // not only this cell but other cells.
    HSSFCellStyle cellStyle = wb.createCellStyle();
    DataFormat df = wb.createDataFormat();
    // cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
    cellStyle.setDataFormat(df.getFormat("YYYY-MM-DD HH:MM:SS"));
    cell = row.createCell(1);
    cell.setCellValue(new Date());
    cell.setCellStyle(cellStyle);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("src/test/resources/workbook.xls");
View Full Code Here


        optionsStyle.cloneStyleFrom(commonStyle);
        optionsStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        optionsStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());

        CreationHelper helper = workbook.getCreationHelper();
        DataFormat df = helper.createDataFormat();

        dataStyle = workbook.createCellStyle();
        dataStyle.cloneStyleFrom(commonStyle);

        dateDataStyle = workbook.createCellStyle();
        dateDataStyle.cloneStyleFrom(commonStyle);
        dateDataStyle.setDataFormat(df.getFormat("yyyy-mm-dd"));

        timeDataStyle = workbook.createCellStyle();
        timeDataStyle.cloneStyleFrom(commonStyle);
        timeDataStyle.setDataFormat(df.getFormat("hh:mm:ss"));

        datetimeDataStyle = workbook.createCellStyle();
        datetimeDataStyle.cloneStyleFrom(commonStyle);
        datetimeDataStyle.setDataFormat(df.getFormat("yyyy-mm-dd hh:mm:ss"));
    }
View Full Code Here

        titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        titleStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
        titleStyle.setAlignment(CellStyle.ALIGN_CENTER);

        CreationHelper helper = workbook.getCreationHelper();
        DataFormat df = helper.createDataFormat();

        dataStyle = workbook.createCellStyle();
        dataStyle.cloneStyleFrom(commonStyle);

        dateDataStyle = workbook.createCellStyle();
        dateDataStyle.cloneStyleFrom(commonStyle);
        dateDataStyle.setDataFormat(df.getFormat("yyyy-mm-dd"));

        timeDataStyle = workbook.createCellStyle();
        timeDataStyle.cloneStyleFrom(commonStyle);
        timeDataStyle.setDataFormat(df.getFormat("hh:mm:ss"));

        datetimeDataStyle = workbook.createCellStyle();
        datetimeDataStyle.cloneStyleFrom(commonStyle);
        datetimeDataStyle.setDataFormat(df.getFormat("yyyy-mm-dd hh:mm:ss"));
    }
View Full Code Here

        centerAlignFixedValueStyle = workbook.createCellStyle();
        centerAlignFixedValueStyle.cloneStyleFrom(fixedValueStyle);
        centerAlignFixedValueStyle.setAlignment(CellStyle.ALIGN_CENTER);

        CreationHelper helper = workbook.getCreationHelper();
        DataFormat df = helper.createDataFormat();

        dateTimeStyle = workbook.createCellStyle();
        dateTimeStyle.cloneStyleFrom(commonStyle);
        dateTimeStyle.setDataFormat(df.getFormat("yyyy-mm-dd hh:mm:ss"));

        dateStyle = workbook.createCellStyle();
        dateStyle.cloneStyleFrom(commonStyle);
        dateStyle.setDataFormat(df.getFormat("yyyy-mm-dd"));
    }
View Full Code Here

            return integerStyle;
        }

        private short getFormat(String datetimeFormatPattern) {
            CreationHelper creationHelper = workbook.getCreationHelper();
            DataFormat dataFormat = creationHelper.createDataFormat();
            return dataFormat.getFormat(datetimeFormatPattern);
        }
View Full Code Here

    if (borderBottom != null) {
      cellStyle.setBorderBottom(borderBottom);
    }
   
    if(dataFormat!=null){
      DataFormat format = wb.createDataFormat();
      cellStyle.setDataFormat(format.getFormat(dataFormat));
    }
    return cellStyle;

  }
View Full Code Here

                CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
   
                String javaDateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern();
                String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern);
   
                DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat();
                cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
                row.createCell(3).setCellValue(dateFormat.format(date));
   
                cell.setCellStyle(cellStyle);
   
                // the formula returns TRUE is the formatted date in column C equals to the string in column D
View Full Code Here

    }

    @Test
    public void stackoverflow23114397() throws Exception {
        Workbook wb = new HSSFWorkbook();
        DataFormat format = wb.getCreationHelper().createDataFormat();
       
        // How close the sizing should be, given that not all
        //  systems will have quite the same fonts on them
        float fontAccuracy = 0.22f;
       
        // x%
        CellStyle iPercent = wb.createCellStyle();
        iPercent.setDataFormat(format.getFormat("0%"));
        // x.x%
        CellStyle d1Percent = wb.createCellStyle();
        d1Percent.setDataFormat(format.getFormat("0.0%"));
        // x.xx%
        CellStyle d2Percent = wb.createCellStyle();
        d2Percent.setDataFormat(format.getFormat("0.00%"));
       
        Sheet s = wb.createSheet();
        Row r1 = s.createRow(0);
       
        for (int i=0; i<3; i++) {
View Full Code Here

        Cell c = null;
        // create 3 cell styles
        CellStyle cs = wb.createCellStyle();
        CellStyle cs2 = wb.createCellStyle();
        CellStyle cs3 = wb.createCellStyle();
        DataFormat df = wb.createDataFormat();
        // create 2 fonts objects
        Font f = wb.createFont();
        Font f2 = wb.createFont();

        //set font 1 to 12 point type
        f.setFontHeightInPoints((short) 12);
        //make it blue
        f.setColor((short) 0xc);
        // make it bold
        //arial is the default font
        f.setBoldweight(Font.BOLDWEIGHT_BOLD);

        //set font 2 to 10 point type
        f2.setFontHeightInPoints((short) 10);
        //make it red
        f2.setColor(Font.COLOR_RED);
        //make it bold
        f2.setBoldweight(Font.BOLDWEIGHT_BOLD);

        f2.setStrikeout(true);

        //set cell stlye
        cs.setFont(f);
        //set the cell format
        cs.setDataFormat(df.getFormat("#,##0.0"));

        //set a thin border
        cs2.setBorderBottom(CellStyle.BORDER_THIN);
        //fill w fg fill color
        cs2.setFillPattern(CellStyle.SOLID_FOREGROUND);
View Full Code Here

    if (result != null) {
     
      if (formatter.format() != null) {
       
        //create data format
        DataFormat df = workbook.createDataFormat();
        short sdf = df.getFormat(formatter.format());
       
        //set format
        CellStyle style = cell.getCellStyle();
        if (style == null) {
          style = workbook.createCellStyle();
View Full Code Here

TOP

Related Classes of org.apache.poi.ss.usermodel.DataFormat

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.