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

The following examples show how to use org.apache.poi.xssf.usermodel.XSSFCellStyle#getDataFormatString() . 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: StreamingSheetReader.java    From components with Apache License 2.0 6 votes vote down vote up
/**
 * Read the numeric format string out of the styles table for this cell. Stores the result in the Cell.
 *
 * @param startElement
 * @param cell
 */
void setFormatString(StartElement startElement, StreamingCell cell) {
    Attribute cellStyle = startElement.getAttributeByName(new QName("s"));
    String cellStyleString = (cellStyle != null) ? cellStyle.getValue() : null;
    XSSFCellStyle style = null;

    if (cellStyleString != null) {
        style = stylesTable.getStyleAt(Integer.parseInt(cellStyleString));
    } else if (stylesTable.getNumCellStyles() > 0) {
        style = stylesTable.getStyleAt(0);
    }

    if (style != null) {
        cell.setNumericFormatIndex(style.getDataFormat());
        String formatString = style.getDataFormatString();

        if (formatString != null) {
            cell.setNumericFormat(formatString);
        } else {
            cell.setNumericFormat(BuiltinFormats.getBuiltinFormat(cell.getNumericFormatIndex()));
        }
    } else {
        cell.setNumericFormatIndex(null);
        cell.setNumericFormat(null);
    }
}
 
Example 2
Source File: StreamingSheetReader.java    From data-prep with Apache License 2.0 6 votes vote down vote up
/**
 * Read the numeric format string out of the styles table for this cell. Stores the result in the Cell.
 *
 * @param startElement
 * @param cell
 */
void setFormatString(StartElement startElement, StreamingCell cell) {
    Attribute cellStyle = startElement.getAttributeByName(new QName("s"));
    String cellStyleString = (cellStyle != null) ? cellStyle.getValue() : null;
    XSSFCellStyle style = null;

    if (cellStyleString != null) {
        style = stylesTable.getStyleAt(Integer.parseInt(cellStyleString));
    } else if (stylesTable.getNumCellStyles() > 0) {
        style = stylesTable.getStyleAt(0);
    }

    if (style != null) {
        cell.setNumericFormatIndex(style.getDataFormat());
        String formatString = style.getDataFormatString();

        if (formatString != null) {
            cell.setNumericFormat(formatString);
        } else {
            cell.setNumericFormat(BuiltinFormats.getBuiltinFormat(cell.getNumericFormatIndex()));
        }
    } else {
        cell.setNumericFormatIndex(null);
        cell.setNumericFormat(null);
    }
}
 
Example 3
Source File: StreamingSheetReader.java    From excel-streaming-reader with Apache License 2.0 6 votes vote down vote up
/**
 * Read the numeric format string out of the styles table for this cell. Stores
 * the result in the Cell.
 *
 * @param startElement
 * @param cell
 */
void setFormatString(StartElement startElement, StreamingCell cell) {
  Attribute cellStyle = startElement.getAttributeByName(new QName("s"));
  String cellStyleString = (cellStyle != null) ? cellStyle.getValue() : null;
  XSSFCellStyle style = null;

  if(cellStyleString != null) {
    style = stylesTable.getStyleAt(Integer.parseInt(cellStyleString));
  } else if(stylesTable.getNumCellStyles() > 0) {
    style = stylesTable.getStyleAt(0);
  }

  if(style != null) {
    cell.setNumericFormatIndex(style.getDataFormat());
    String formatString = style.getDataFormatString();

    if(formatString != null) {
      cell.setNumericFormat(formatString);
    } else {
      cell.setNumericFormat(BuiltinFormats.getBuiltinFormat(cell.getNumericFormatIndex()));
    }
  } else {
    cell.setNumericFormatIndex(null);
    cell.setNumericFormat(null);
  }
}
 
Example 4
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 5
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 6
Source File: XSSFSheetHandler.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)) {
    	context.setvIsOpen(true);
        context.clearContents();
    } else if ("c".equals(name)) {
        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;
            }
        }
        int col = nameToColumn(r.substring(0, firstDigit));
        int row = Integer.parseInt(r.substring(firstDigit));
        context.setCurrentCell(new Cell(row, col));
        context.setNextDataType(XSSFDataType.NUMBER);
        context.setFormatIndex((short) -1);
        context.setFormatString(null);

        String cellType = attributes.getValue("t");
        String cellStyleStr = attributes.getValue("s");
        if ("b".equals(cellType))
            context.setNextDataType(XSSFDataType.BOOL);
        else if ("e".equals(cellType))
            context.setNextDataType(XSSFDataType.ERROR);
        else if ("inlineStr".equals(cellType))
        	context.setNextDataType(XSSFDataType.INLINESTR);
        else if ("s".equals(cellType))
        	context.setNextDataType(XSSFDataType.SSTINDEX);
        else if ("str".equals(cellType))
        	context.setNextDataType(XSSFDataType.FORMULA);
        else if (cellStyleStr != null) {
            int styleIndex = Integer.parseInt(cellStyleStr);
            XSSFCellStyle style = context.getStyles().getStyleAt(styleIndex);
            context.setFormatIndex(style.getDataFormat());
            String formatString = style.getDataFormatString();
            if (formatString == null) {
            	formatString = BuiltinFormats.getBuiltinFormat(context.getFormatIndex());
            }
            context.setFormatString(formatString);
        }
    }

}
 
Example 7
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 8
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 9
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);
        }

    }
}