本文整理汇总了Java中org.apache.poi.hssf.usermodel.HSSFCellStyle.setDataFormat方法的典型用法代码示例。如果您正苦于以下问题:Java HSSFCellStyle.setDataFormat方法的具体用法?Java HSSFCellStyle.setDataFormat怎么用?Java HSSFCellStyle.setDataFormat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.poi.hssf.usermodel.HSSFCellStyle
的用法示例。
在下文中一共展示了HSSFCellStyle.setDataFormat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setNumericCell
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
protected void setNumericCell(HSSFCell cell, BigDecimal value, HSSFWorkbook workbook)
{
if(logger.isDebugEnabled())
logger.debug("setNumericCell(cell={}, value={}, workbook={}) - start",
new Object[] {cell, value, workbook} );
cell.setCellValue( ((BigDecimal)value).doubleValue() );
HSSFDataFormat df = workbook.createDataFormat();
int scale = ((BigDecimal)value).scale();
short format;
if(scale <= 0){
format = df.getFormat("####");
}
else {
String zeros = createZeros(((BigDecimal)value).scale());
format = df.getFormat("####." + zeros);
}
if(logger.isDebugEnabled())
logger.debug("Using format '{}' for value '{}'.", String.valueOf(format), value);
HSSFCellStyle cellStyleNumber = workbook.createCellStyle();
cellStyleNumber.setDataFormat(format);
cell.setCellStyle(cellStyleNumber);
}
示例2: writeCell
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
/**
* Write the value to the cell. Override this method if you have complex data types that may need to be exported.
* @param value the value of the cell
* @param cell the cell to write it to
*/
protected void writeCell(Object value, HSSFCell cell, HSSFWorkbook wb)
{
if (value instanceof Number)
{
Number num = (Number) value;
cell.setCellValue(num.doubleValue());
}
else if (value instanceof Date)
{
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
wb.getCreationHelper().createDataFormat().getFormat("dd/MM/yyyy HH:mm"));
cell.setCellStyle(cellStyle);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue((Date) value);
}
else if (value instanceof Calendar)
{
cell.setCellValue((Calendar) value);
}
else
{
cell.setCellValue(new HSSFRichTextString(escapeColumnValue(value)));
}
}
示例3: getHeaderStyle
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
private HSSFCellStyle getHeaderStyle(final 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;
}
示例4: addRow
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
protected void addRow(HSSFWorkbook workbook, HSSFSheet sheet, Object value, int row, int column) {
HSSFRow hssfRow = sheet.getRow(row);
hssfRow = (hssfRow == null) ? sheet.createRow(row) : hssfRow;
HSSFCell cell = hssfRow.getCell(column);
cell = (cell == null) ? hssfRow.createCell(column) : cell;
String cellValue = (value == null) ? "" : value.toString();
DecimalFormat decimalFormatter = new DecimalFormat("###,###,###,##0.00");
try {
double doubleValue = decimalFormatter.parse(cellValue).doubleValue();
DataFormat dataFormat = workbook.createDataFormat();
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(dataFormat.getFormat("###,###,###,##0.00"));
cell.setCellValue(doubleValue);
} catch (ParseException e) {
cell.setCellValue(cellValue);
}
formatCell(workbook, cell);
}
示例5: setDoubleNegativeStyle
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
private void setDoubleNegativeStyle(HSSFWorkbook wb) {
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 8);
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
style.setDataFormat(wb.createDataFormat().getFormat("#,##0.00"));
font.setColor(HSSFColor.RED.index);
doubleNegativeStyle = style;
}
示例6: setCell
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
/**
* 设置单元格
*
* @param index
* 列号
* @param value
* 单元格填充值
*/
@SuppressWarnings("deprecation")
public void setCell(int index, Calendar value) {
HSSFCell cell = this.row.createCell((short) index);
cell.setCellValue(value.getTime());
HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); // 设置cell样式为定制的日期格式
cell.setCellStyle(cellStyle); // 设置该cell日期的显示格式
}
示例7: copyCellStyle
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
public static HSSFCellStyle copyCellStyle(final HSSFWorkbook workbook, final HSSFCellStyle style) {
final HSSFCellStyle newCellStyle = workbook.createCellStyle();
newCellStyle.setAlignment(style.getAlignment());
newCellStyle.setBorderBottom(style.getBorderBottom());
newCellStyle.setBorderLeft(style.getBorderLeft());
newCellStyle.setBorderRight(style.getBorderRight());
newCellStyle.setBorderTop(style.getBorderTop());
newCellStyle.setBottomBorderColor(style.getBottomBorderColor());
newCellStyle.setDataFormat(style.getDataFormat());
newCellStyle.setFillBackgroundColor(style.getFillBackgroundColor());
newCellStyle.setFillForegroundColor(style.getFillForegroundColor());
newCellStyle.setFillPattern(style.getFillPattern());
newCellStyle.setHidden(style.getHidden());
newCellStyle.setIndention(style.getIndention());
newCellStyle.setLeftBorderColor(style.getLeftBorderColor());
newCellStyle.setLocked(style.getLocked());
newCellStyle.setRightBorderColor(style.getRightBorderColor());
newCellStyle.setRotation(style.getRotation());
newCellStyle.setTopBorderColor(style.getTopBorderColor());
newCellStyle.setVerticalAlignment(style.getVerticalAlignment());
newCellStyle.setWrapText(style.getWrapText());
final HSSFFont font = workbook.getFontAt(style.getFontIndex());
newCellStyle.setFont(font);
return newCellStyle;
}
示例8: createCelStyleForTime
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
public static HSSFCellStyle createCelStyleForTime(HSSFWorkbook workBook)
{
HSSFCellStyle cellStyle = workBook.createCellStyle();
CreationHelper createHelper = workBook.getCreationHelper();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("m/d/yy h:mm:ss"));
return cellStyle;
}
示例9: copyCellStyle
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
public static HSSFCellStyle copyCellStyle(HSSFWorkbook workbook,
HSSFCellStyle style) {
HSSFCellStyle newCellStyle = workbook.createCellStyle();
newCellStyle.setAlignment(style.getAlignment());
newCellStyle.setBorderBottom(style.getBorderBottom());
newCellStyle.setBorderLeft(style.getBorderLeft());
newCellStyle.setBorderRight(style.getBorderRight());
newCellStyle.setBorderTop(style.getBorderTop());
newCellStyle.setBottomBorderColor(style.getBottomBorderColor());
newCellStyle.setDataFormat(style.getDataFormat());
newCellStyle.setFillBackgroundColor(style.getFillBackgroundColor());
newCellStyle.setFillForegroundColor(style.getFillForegroundColor());
newCellStyle.setFillPattern(style.getFillPattern());
newCellStyle.setHidden(style.getHidden());
newCellStyle.setIndention(style.getIndention());
newCellStyle.setLeftBorderColor(style.getLeftBorderColor());
newCellStyle.setLocked(style.getLocked());
newCellStyle.setRightBorderColor(style.getRightBorderColor());
newCellStyle.setRotation(style.getRotation());
newCellStyle.setTopBorderColor(style.getTopBorderColor());
newCellStyle.setVerticalAlignment(style.getVerticalAlignment());
newCellStyle.setWrapText(style.getWrapText());
HSSFFont font = workbook.getFontAt(style.getFontIndex());
newCellStyle.setFont(font);
return newCellStyle;
}
示例10: getOrCreateCellStyle
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
/**
* Get or create the cell style objects - POI seemingly has a limit of 44 (?)
* so these are built and cached on a per-generate-command basis to ensure that
* all dates, numbers, etc get formatted correctly.
*
* @param pExcelFormatPattern the format pattern to use
* @return cell style instance
*/
private HSSFCellStyle getOrCreateCellStyle (String pExcelFormatPattern) {
if (!mCellStyleMap.containsKey(pExcelFormatPattern)) {
HSSFCellStyle lCellStyle = mWorkbook.createCellStyle();
short lStyle = mWorkbook.createDataFormat().getFormat(pExcelFormatPattern);
lCellStyle.setDataFormat(lStyle);
mCellStyleMap.put(pExcelFormatPattern, lCellStyle);
return lCellStyle;
}
else {
return mCellStyleMap.get(pExcelFormatPattern);
}
}
示例11: getStyle
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
private HSSFCellStyle getStyle(final int row, final 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))
{
final DecimalFormat df = DisplayType.getNumberFormat(displayType, getLanguage());
final String format = getFormatString(df, isHighlightNegativeNumbers);
cs.setDataFormat(m_dataFormat.getFormat(format));
}
m_styles.put(key, cs);
}
return cs;
}
示例12: createDateCellStyle
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
protected static HSSFCellStyle createDateCellStyle(HSSFWorkbook workbook) {
HSSFDataFormat format = workbook.createDataFormat();
short dateFormatCode = format.getFormat(DATE_FORMAT_AS_NUMBER_DBUNIT);
HSSFCellStyle dateCellStyle = workbook.createCellStyle();
dateCellStyle.setDataFormat(dateFormatCode);
return dateCellStyle;
}
示例13: initCellStyles
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
/**
* We cache the styles; they are expensive to construct.
* @param properties props for this run
*/
public void initCellStyles(TableProperties properties)
{
// Integer
HSSFCellStyle style = getNewCellStyle();
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_INTEGER)));
cellStyles.put(STYLE_INTEGER, style);
// NUMBER
style = getNewCellStyle();
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_NUMBER)));
cellStyles.put(STYLE_NUMBER, style);
// style = HSSFDataFormat.getBuiltinFormat("0.00%");
// Date
style = getNewCellStyle();
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_DATE)));
style.setAlignment(CellStyle.ALIGN_RIGHT);
cellStyles.put(STYLE_DATE, style);
// Long text
style = getNewCellStyle(); // http://jakarta.apache.org/poi/hssf/quick-guide.html#NewLinesInCells
style.setWrapText(true);
cellStyles.put(STYLE_LONGSTRING, style);
// Regular text
cellStyles.put(STYLE_STRING, getNewCellStyle());
wrapAt = Integer.valueOf(properties.getProperty(ExcelUtils.EXCEL_WRAPAT));
}
示例14: cloneStyleRelations
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
public static void cloneStyleRelations(HSSFCellStyle source, HSSFCellStyle target) {
//First we need to clone the extended format record
getFormatFromStyle(target).cloneStyleFrom(getFormatFromStyle(source));
//Handle matching things if we cross workbooks
InternalWorkbook sourceWorkbook = getWorkbookFromStyle(source);
InternalWorkbook targetWorkbook = getWorkbookFromStyle(target);
if (targetWorkbook != sourceWorkbook) {
//Then we need to clone the format string, and update the format record for this
short fmt = sourceWorkbook.getFormat(source.getDataFormatString(), true);
target.setDataFormat(fmt);
}
}
示例15: setDoubleStyle
import org.apache.poi.hssf.usermodel.HSSFCellStyle; //导入方法依赖的package包/类
private void setDoubleStyle(HSSFWorkbook wb) {
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 8);
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
style.setDataFormat(wb.createDataFormat().getFormat("#,##0.00"));
doubleStyle = style;
}