Java Code Examples for org.apache.poi.xssf.usermodel.XSSFCellStyle#getDataFormat()

The following examples show how to use org.apache.poi.xssf.usermodel.XSSFCellStyle#getDataFormat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HSSFCellStyleProducer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected HSSFCellStyleKey( final XSSFCellStyle style ) {
  this.color = style.getFillForegroundColor();
  this.colorTop = style.getTopBorderColor();
  this.colorLeft = style.getLeftBorderColor();
  this.colorBottom = style.getBottomBorderColor();
  this.colorRight = style.getRightBorderColor();

  this.xColor = createColor( style.getFillBackgroundXSSFColor() );
  this.xColorTop = createColor( style.getTopBorderXSSFColor() );
  this.xColorLeft = createColor( style.getLeftBorderXSSFColor() );
  this.xColorBottom = createColor( style.getBottomBorderXSSFColor() );
  this.xColorRight = createColor( style.getRightBorderXSSFColor() );

  this.borderStrokeTop = style.getBorderTopEnum();
  this.borderStrokeLeft = style.getBorderLeftEnum();
  this.borderStrokeBottom = style.getBorderBottomEnum();
  this.borderStrokeRight = style.getBorderRightEnum();

  this.dataStyle = style.getDataFormat();
  this.font = style.getFontIndex();
  this.horizontalAlignment = style.getAlignmentEnum();
  this.verticalAlignment = style.getVerticalAlignmentEnum();
  this.wrapText = style.getWrapText();
  this.textRotation = TextRotation.getInstance( style.getRotation() );
}
 
Example 2
Source File: XlsxHandler.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
/**
 * 处理数据类型
 * 
 * @param attributes
 */
private void setNextDataType(Attributes attributes) {
	nextDataType = CellDataType.NUMBER;
	formatIndex = -1;
	formatString = null;
	String cellType = attributes.getValue("t");
	String cellStyleStr = attributes.getValue("s");
	String columData = attributes.getValue("r");

	if ("b".equals(cellType)) {
		nextDataType = CellDataType.BOOL;
	} else if ("e".equals(cellType)) {
		nextDataType = CellDataType.ERROR;
	} else if ("inlineStr".equals(cellType)) {
		nextDataType = CellDataType.INLINESTR;
	} else if ("s".equals(cellType)) {
		nextDataType = CellDataType.SSTINDEX;
	} else if ("str".equals(cellType)) {
		nextDataType = CellDataType.FORMULA;
	}

	if (cellStyleStr != null) {
		int styleIndex = Integer.parseInt(cellStyleStr);
		XSSFCellStyle style = stylesTable.getStyleAt(styleIndex);
		formatIndex = style.getDataFormat();
		formatString = style.getDataFormatString();

		if ("m/d/yy" == formatString) {
			nextDataType = CellDataType.DATE;
			formatString = "yyyy-MM-dd hh:mm:ss.SSS";
		}

		if (formatString == null) {
			nextDataType = CellDataType.NULL;
			formatString = BuiltinFormats.getBuiltinFormat(formatIndex);
		}
	}
}
 
Example 3
Source File: CellTagHandler.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) {
    XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
    xlsxReadSheetHolder.setColumnIndex(PositionUtils.getCol(attributes.getValue(ExcelXmlConstants.ATTRIBUTE_R),
        xlsxReadSheetHolder.getColumnIndex()));

    // t="s" ,it's means String
    // t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml'
    // t="inlineStr" ,it's means String
    // t="b" ,it's means Boolean
    // t="e" ,it's means Error
    // t="n" ,it's means Number
    // t is null ,it's means Empty or Number
    CellDataTypeEnum type = CellDataTypeEnum.buildFromCellType(attributes.getValue(ExcelXmlConstants.ATTRIBUTE_T));
    xlsxReadSheetHolder.setTempCellData(new CellData(type));
    xlsxReadSheetHolder.setTempData(new StringBuilder());

    // Put in data transformation information
    String dateFormatIndex = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_S);
    Integer dateFormatIndexInteger;
    if (StringUtils.isEmpty(dateFormatIndex)) {
        dateFormatIndexInteger = DEFAULT_FORMAT_INDEX;
    } else {
        dateFormatIndexInteger = Integer.parseInt(dateFormatIndex);
    }
    XSSFCellStyle xssfCellStyle =
        xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable().getStyleAt(dateFormatIndexInteger);
    int dataFormat = xssfCellStyle.getDataFormat();
    xlsxReadSheetHolder.getTempCellData().setDataFormat(dataFormat);
    xlsxReadSheetHolder.getTempCellData().setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat,
        xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale()));
}
 
Example 4
Source File: XLSX2CSV.java    From DBus with Apache License 2.0 4 votes vote down vote up
public void startElement(String uri, String localName, String name,
                         Attributes attributes) throws SAXException {

    if ("inlineStr".equals(name) || "v".equals(name)) {
        vIsOpen = true;
        // Clear contents cache
        value.setLength(0);
    }
    // c => cell
    else if ("c".equals(name)) {
        // Get the cell reference
        String r = attributes.getValue("r");
        int firstDigit = -1;
        for (int c = 0; c < r.length(); ++c) {
            if (Character.isDigit(r.charAt(c))) {
                firstDigit = c;
                break;
            }
        }
        thisColumn = nameToColumn(r.substring(0, firstDigit));

        // Set up defaults.
        this.nextDataType = xssfDataType.NUMBER;
        this.formatIndex = -1;
        this.formatString = null;
        String cellType = attributes.getValue("t");
        String cellStyleStr = attributes.getValue("s");
        if ("b".equals(cellType))
            nextDataType = xssfDataType.BOOL;
        else if ("e".equals(cellType))
            nextDataType = xssfDataType.ERROR;
        else if ("inlineStr".equals(cellType))
            nextDataType = xssfDataType.INLINESTR;
        else if ("s".equals(cellType))
            nextDataType = xssfDataType.SSTINDEX;
        else if ("str".equals(cellType))
            nextDataType = xssfDataType.FORMULA;
        else if (cellStyleStr != null) {
            // It's a number, but almost certainly one
            // with a special style or format
            int styleIndex = Integer.parseInt(cellStyleStr);
            XSSFCellStyle style = stylesTable.getStyleAt(styleIndex);
            this.formatIndex = style.getDataFormat();
            this.formatString = style.getDataFormatString();
            if (this.formatString == null)
                this.formatString = BuiltinFormats
                        .getBuiltinFormat(this.formatIndex);
        }
    }

}
 
Example 5
Source File: XLSX2CSV.java    From bdf3 with Apache License 2.0 4 votes vote down vote up
public void startElement(String uri, String localName, String name,
        Attributes attributes) throws SAXException {

    if ("inlineStr".equals(name) || "v".equals(name)) {
        vIsOpen = true;
        // Clear contents cache
        value.setLength(0);
    }
    // c => cell
    else if ("c".equals(name)) {
        // Get the cell reference
        String r = attributes.getValue("r");
        int firstDigit = -1;
        for (int c = 0; c < r.length(); ++c) {
            if (Character.isDigit(r.charAt(c))) {
                firstDigit = c;
                break;
            }
        }
        thisColumn = nameToColumn(r.substring(0, firstDigit));

        // Set up defaults.
        this.nextDataType = xssfDataType.NUMBER;
        this.formatIndex = -1;
        this.formatString = null;
        String cellType = attributes.getValue("t");
        String cellStyleStr = attributes.getValue("s");
        if ("b".equals(cellType))
            nextDataType = xssfDataType.BOOL;
        else if ("e".equals(cellType))
            nextDataType = xssfDataType.ERROR;
        else if ("inlineStr".equals(cellType))
            nextDataType = xssfDataType.INLINESTR;
        else if ("s".equals(cellType))
            nextDataType = xssfDataType.SSTINDEX;
        else if ("str".equals(cellType))
            nextDataType = xssfDataType.FORMULA;
        else if (cellStyleStr != null) {
            // It's a number, but almost certainly one
            // with a special style or format
            int styleIndex = Integer.parseInt(cellStyleStr);
            XSSFCellStyle style = stylesTable.getStyleAt(styleIndex);
            this.formatIndex = style.getDataFormat();
            this.formatString = style.getDataFormatString();
            if (this.formatString == null)
                this.formatString = BuiltinFormats
                        .getBuiltinFormat(this.formatIndex);
        }
    }

}
 
Example 6
Source File: XlsxFileReader.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
public void startElement(String uri, String localName, String qName,
		Attributes attributes) throws SAXException {
	if (qName.equals("c")) {
		String vCellType = attributes.getValue("t");
		String cellS = attributes.getValue("s");
		if ("b".equals(vCellType))
			cellDataType = cDataType.BOOL;
		else if ("e".equals(vCellType))
			cellDataType = cDataType.FORMULA;
		else if ("s".equals(vCellType))
			cellDataType = cDataType.SSTINDEX;
		else if("str".equals(vCellType))
			cellDataType =  cDataType.STATIC;
        else if (cellS != null) {
              //number with formatting or date
            int styleIndex = Integer.parseInt(cellS);
            XSSFCellStyle style = st.getStyleAt(styleIndex);
            short formatIndex = style.getDataFormat();
            String formatString = style.getDataFormatString();

            if (formatString == null)
                   formatString = BuiltinFormats.getBuiltinFormat(formatIndex);

			if (org.apache.poi.ss.usermodel.DateUtil.isADateFormat(
					formatIndex, formatString) ) {
            	cellDataType =  cDataType.DATETIME;
            }else{
            	cellDataType = cDataType.NUMBER;
            }
        }
		else
			cellDataType = cDataType.NUMBER;

              String r = attributes.getValue("r");

              currentColumn = getColumnNumber( r );
              //expand the number of columns if needed in existing rows
              if( currentColumn+1 > columnCount){
              	callback.columnExpansion(currentColumn+1);
              	
              	//clean up current row
              	int newvals = (currentColumn+1) - columnCount;
              	for( int ii=0; ii<newvals;ii++){
              		values.add(ExcelODAConstants.EMPTY_STRING);
              	}               	
              	
          		columnCount = currentColumn+1;
              }

	}

	//empty cells are not in the xml so we have
	//create them in the row
	if (qName.equals("row")) {
		for( int i=0;i<columnCount; i++){
			values.add(i, ExcelODAConstants.EMPTY_STRING);
		}
	}
	lastContents = ExcelODAConstants.EMPTY_STRING;
}
 
Example 7
Source File: XSSFSheetXMLPoijiHandler.java    From poiji with MIT License 3 votes vote down vote up
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

    super.startElement(uri, localName, qName, attributes);

    if (this.cellFormat == null) {
        return;
    }
    if (uri != null && !uri.equals(NS_SPREADSHEETML)) {
        return;
    }

    if ("c".equals(localName)) {
        // Set up defaults.
        String cellRef = attributes.getValue("r");
        String cellType = attributes.getValue("t");
        String cellStyleStr = attributes.getValue("s");
        CellAddress cellAddress = new CellAddress(cellRef);
        if ("b".equals(cellType) || "e".equals(cellType) || "inlineStr".equals(cellType)) {
            this.cellFormat.addFormat(cellAddress, (short) 0, null, cellType, cellStyleStr);
            return;
        }
        if ("s".equals(cellType) || "str".equals(cellType)) {
            this.cellFormat.addFormat(cellAddress, (short) 0, null, cellType, cellStyleStr);
            return;
        }
        // Number, but almost certainly with a special style or format
        XSSFCellStyle style = null;
        if (stylesTable != null) {
            if (cellStyleStr != null) {
                int styleIndex = Integer.parseInt(cellStyleStr);
                style = stylesTable.getStyleAt(styleIndex);
            } else if (stylesTable.getNumCellStyles() > 0) {
                style = stylesTable.getStyleAt(0);
            }
        }
        if (style != null) {
            short formatIndex = style.getDataFormat();
            String formatString = style.getDataFormatString();
            if (formatString == null)
                formatString = BuiltinFormats.getBuiltinFormat(formatIndex);

            this.cellFormat.addFormat(cellAddress, formatIndex, formatString, cellType, cellStyleStr);
        }

    }
}