本文整理汇总了Java中org.apache.poi.ss.usermodel.DataFormat类的典型用法代码示例。如果您正苦于以下问题:Java DataFormat类的具体用法?Java DataFormat怎么用?Java DataFormat使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DataFormat类属于org.apache.poi.ss.usermodel包,在下文中一共展示了DataFormat类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createHeadDateStyle
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
CellStyle createHeadDateStyle() {
if (headDateCellStyle != null) {
return headDateCellStyle;
}
CellStyle cellStyle = wb.createCellStyle();
DataFormat format = wb.createDataFormat();
cellStyle.setDataFormat(format.getFormat("m/d/yy h:mm"));
// 设置这些样式
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
// 生成一个字体
Font font = wb.createFont();
font.setColor(IndexedColors.BLACK.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
cellStyle.setFont(font);
headDateCellStyle = cellStyle;
return cellStyle;
}
示例2: getDecimalStyle
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
/**
* 小数格式
*
* @return CellStyle
*/
public CellStyle getDecimalStyle() {
if (buildInStyleMap.containsKey(DECIMAL_STYLE_KEY)) {
return buildInStyleMap.get(DECIMAL_STYLE_KEY);
}
CellStyle decimalStyle = workbook.createCellStyle();//小数样式
if (!buildInFormatMap.containsKey(DATA_FORMAT_KEY)) {
DataFormat dataFormat = workbook.createDataFormat();
buildInFormatMap.put(DATA_FORMAT_KEY, dataFormat);
}
decimalStyle.setDataFormat(buildInFormatMap.get(DATA_FORMAT_KEY).getFormat("0.00"));
this.setCommonStyle(decimalStyle);
buildInStyleMap.put(DECIMAL_STYLE_KEY, decimalStyle);
return decimalStyle;
}
示例3: getDateStyle
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
/**
* 日期样式 yyyy-MM-dd HH:mm
*
* @return CellStyle
*/
public CellStyle getDateStyle() {
if (buildInStyleMap.containsKey(DATE_STYLE_KEY)) {
return buildInStyleMap.get(DATE_STYLE_KEY);
}
CellStyle dateStyle = workbook.createCellStyle();//日期样式
if (!buildInFormatMap.containsKey(DATA_FORMAT_KEY)) {
DataFormat dataFormat = workbook.createDataFormat();
buildInFormatMap.put(DATA_FORMAT_KEY, dataFormat);
}
dateStyle.setDataFormat(buildInFormatMap.get(DATA_FORMAT_KEY).getFormat("yyyy-MM-dd HH:mm"));
this.setCommonStyle(dateStyle);
buildInStyleMap.put(DATE_STYLE_KEY, dateStyle);
return dateStyle;
}
示例4: getDate8Style
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
/**
* 日期样式 yyyy/MM/dd
*
* @return CellStyle
*/
public CellStyle getDate8Style() {
if (buildInStyleMap.containsKey(DATE_8_STYLE_KEY)) {
return buildInStyleMap.get(DATE_8_STYLE_KEY);
}
CellStyle date8Style = workbook.createCellStyle();//年月日样式
if (!buildInFormatMap.containsKey(DATA_FORMAT_KEY)) {
DataFormat dataFormat = workbook.createDataFormat();
buildInFormatMap.put(DATA_FORMAT_KEY, dataFormat);
}
date8Style.setDataFormat(buildInFormatMap.get(DATA_FORMAT_KEY).getFormat("yyyy/MM/dd"));
this.setCommonStyle(date8Style);
buildInStyleMap.put(DATE_8_STYLE_KEY, date8Style);
return date8Style;
}
示例5: getCustomFormatStyle
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
/**
* 根据格式,创建返回样式对象
*
* @param format 格式
* @return 样式对象
*/
public CellStyle getCustomFormatStyle(String format) {
//存在对应格式直接返回
if (customFormatStyleMap.containsKey(format)) {
return customFormatStyleMap.get(format);
}
CellStyle customDateStyle = workbook.createCellStyle();
if (!buildInFormatMap.containsKey(DATA_FORMAT_KEY)) {
DataFormat dataFormat = workbook.createDataFormat();
buildInFormatMap.put(DATA_FORMAT_KEY, dataFormat);
}
customDateStyle.setDataFormat(buildInFormatMap.get(DATA_FORMAT_KEY).getFormat(format));
this.setCommonStyle(customDateStyle);
//放入map缓存
customFormatStyleMap.put(format, customDateStyle);
return customDateStyle;
}
示例6: getDateStyle
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
public CellStyle getDateStyle() {
CellStyle dateStyle = workbook.createCellStyle();
// 设置单元格居中对齐
dateStyle.setAlignment(CellStyle.ALIGN_CENTER);
// 设置单元格垂直居中对齐
dateStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
DataFormat format = workbook.createDataFormat();
dateStyle.setDataFormat(format.getFormat("yyyy-MM-dd HH:mm"));
// 创建单元格内容显示不下时自动换行
dateStyle.setWrapText(true);
// 设置单元格字体样式
Font font = workbook.createFont();
// 设置字体加粗
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 200);
dateStyle.setFont(font);
setBorder(dateStyle);
return dateStyle;
}
示例7: addRow
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的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);
}
示例8: setCellValue
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
protected static void setCellValue(Workbook wb, Cell cell, Object value) {
if (cell == null) {
return;
}
if (value instanceof Boolean) {
cell.setCellValue((Boolean)value);
} else if (value instanceof Number) {
cell.setCellValue(((Number)value).doubleValue());
} else if (value instanceof Date) {
CellStyle cellStyle = wb.createCellStyle();
DataFormat poiFormat = wb.createDataFormat();
// Format: 0x16, "m/d/yy h:mm"
final short format = poiFormat.getFormat(BuiltinFormats.getBuiltinFormat(0x16));
cellStyle.setDataFormat(format);
cell.setCellValue(((Date)value));
cell.setCellStyle(cellStyle);
} else if (value instanceof Calendar) {
cell.setCellValue(((Calendar)value));
} else if (value != null) {
cell.setCellValue(value.toString());
} else {
cell.setCellValue("");
cell.setCellType(Cell.CELL_TYPE_BLANK);
}
}
示例9: addCell
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
/**
* 添加一个单元格
* @param row 添加的行
* @param column 添加列号
* @param val 添加值
* @param align 对齐方式(1:靠左;2:居中;3:靠右)
* @return 单元格对象
*/
public Cell addCell(Row row, int column, Object val, int align, Class<?> fieldType){
Cell cell = row.createCell(column);
CellStyle style = styles.get("data"+(align>=1&&align<=3?align:""));
try {
if (val == null){
cell.setCellValue("");
} else if (val instanceof String) {
cell.setCellValue((String) val);
} else if (val instanceof Integer) {
cell.setCellValue((Integer) val);
} else if (val instanceof Long) {
cell.setCellValue((Long) val);
} else if (val instanceof Double) {
cell.setCellValue((Double) val);
} else if (val instanceof Float) {
cell.setCellValue((Float) val);
} else if (val instanceof Date) {
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("yyyy-MM-dd"));
cell.setCellValue((Date) val);
} else {
if (fieldType != Class.class){
cell.setCellValue((String)fieldType.getMethod("setValue", Object.class).invoke(null, val));
}else{
cell.setCellValue((String)Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
"fieldtype."+val.getClass().getSimpleName()+"Type")).getMethod("setValue", Object.class).invoke(null, val));
}
}
} catch (Exception ex) {
log.info("Set cell value ["+row.getRowNum()+","+column+"] error: " + ex.toString());
cell.setCellValue(val.toString());
}
cell.setCellStyle(style);
return cell;
}
示例10: ExcelTestHelper
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
ExcelTestHelper(final String parent, boolean generateXls) throws Exception {
this.xls = generateXls;
// Create a test Excel sheet with all types of supported data
Workbook wb = generateXls ? new HSSFWorkbook() : new XSSFWorkbook();
CreationHelper creationHelper = wb.getCreationHelper();
DataFormat dataFormat = creationHelper.createDataFormat();
short fmt = dataFormat.getFormat("yyyy-mm-dd hh:mm:ss");
CellStyle style = wb.createCellStyle();
style.setDataFormat(fmt);
Sheet sheetWithHeader = wb.createSheet("Sheet 1");
// Create header row
Row headerRow = sheetWithHeader.createRow((short) 0);
headerRow.createCell(0).setCellValue("Number");
headerRow.createCell(1).setCellValue("String1");
headerRow.createCell(2).setCellValue("String2");
headerRow.createCell(3).setCellValue("MyTime");
headerRow.createCell(4).setCellValue("Formula");
headerRow.createCell(5).setCellValue("Boolean");
headerRow.createCell(6).setCellValue("Error");
generateSheetData(sheetWithHeader, style, (short)1);
Sheet sheetWithoutHeader = wb.createSheet("Sheet 2");
generateSheetData(sheetWithoutHeader, style, (short)0);
testFilePath = new File(parent, "excelTestFile").getPath();
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream(testFilePath);
wb.write(fileOut);
fileOut.close();
}
示例11: addCell
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
/**
* 添加一个单元格
* @param row 添加的行
* @param column 添加列号
* @param val 添加值
* @param align 对齐方式(1:靠左;2:居中;3:靠右)
* @return 单元格对象
*/
public Cell addCell(Row row, int column, Object val, int align, Class<?> fieldType){
Cell cell = row.createCell(column);
CellStyle style = styles.get("data"+(align>=1&&align<=3?align:""));
try {
if (val == null){
cell.setCellValue("");
} else if (val instanceof String) {
cell.setCellValue((String) val);
} else if (val instanceof Integer) {
cell.setCellValue((Integer) val);
} else if (val instanceof Long) {
cell.setCellValue((Long) val);
} else if (val instanceof Double) {
cell.setCellValue((Double) val);
} else if (val instanceof Float) {
cell.setCellValue((Float) val);
} else if (val instanceof Date) {
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("yyyy-MM-dd"));
cell.setCellValue((Date) val);
} else {
if (fieldType != Class.class){
cell.setCellValue((String)fieldType.getMethod("setValue", Object.class).invoke(null, val));
}else{
cell.setCellValue((String)Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
"fieldtype."+val.getClass().getSimpleName()+"Type")).getMethod("setValue", Object.class).invoke(null, val));
}
}
} catch (Exception ex) {
log.info("Set cell value ["+row.getRowNum()+","+column+"] error: " + ex.toString());
cell.setCellValue(val.toString());
}
cell.setCellStyle(style);
return cell;
}
示例12: createDateStyle
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
CellStyle createDateStyle() {
if (dateCellStyle != null) {
return dateCellStyle;
}
CellStyle cellStyle = wb.createCellStyle();
DataFormat format = wb.createDataFormat();
cellStyle.setDataFormat(format.getFormat("m/d/yy h:mm"));
dateCellStyle = cellStyle;
return cellStyle;
}
示例13: getFormatPattern
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
@Override
public String getFormatPattern() {
final DataFormat dataFormat = cell.getSheet().getWorkbook().createDataFormat();
final short formatIndex = getFormatIndex();
String formatPattern = dataFormat.getFormat(formatIndex);
if(formatPattern == null) {
formatPattern = "";
}
return formatPattern;
}
示例14: setStyles
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
private void setStyles() {
csNormal = wb.createCellStyle();
csUeberschrift = wb.createCellStyle();
csTitel = wb.createCellStyle();
DataFormat dataFormat = wb.createDataFormat();
// Schriftart Schüler
Font fNormal = wb.createFont();
fNormal.setFontHeightInPoints((short) 10); // Schriftgröße auf 10pt setzen
fNormal.setColor(Font.COLOR_NORMAL); // Schriftfarbe schwarz
fNormal.setBoldweight(Font.BOLDWEIGHT_NORMAL);
// CellStyle für Schüler
csNormal.setFont(fNormal); // Schriftart
csNormal.setAlignment(CellStyle.ALIGN_CENTER_SELECTION); // Mittige Anordnung
csNormal.setBorderBottom(CellStyle.BORDER_THIN); // Umrandung unten einschalten
csNormal.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung unten in schwarz
csNormal.setBorderLeft(CellStyle.BORDER_THIN); // Umrandung links einschalten
csNormal.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung links in schwarz
csNormal.setBorderTop(CellStyle.BORDER_THIN); // Umrandung oben einschalten
csNormal.setTopBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung oben in schwarz
csNormal.setBorderRight(CellStyle.BORDER_THIN); // Umrandung rechts einschalten
csNormal.setRightBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung rechts in schwarz
csNormal.setDataFormat(dataFormat.getFormat("text")); // Datenformat Text
// Fett
Font fUeberschrift = wb.createFont();
fUeberschrift.setFontHeightInPoints((short) 11);
fUeberschrift.setColor(Font.COLOR_NORMAL);
fUeberschrift.setBoldweight(Font.BOLDWEIGHT_BOLD);
csUeberschrift.setFont(fUeberschrift);
csUeberschrift.setAlignment(CellStyle.ALIGN_CENTER_SELECTION); // Mittige Anordnung
csUeberschrift.setBorderBottom(CellStyle.BORDER_THIN); // Umrandung unten einschalten
csUeberschrift.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung unten in schwarz
csUeberschrift.setBorderLeft(CellStyle.BORDER_THIN); // Umrandung links einschalten
csUeberschrift.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung links in schwarz
csUeberschrift.setBorderTop(CellStyle.BORDER_THIN); // Umrandung oben einschalten
csUeberschrift.setTopBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung oben in schwarz
csUeberschrift.setBorderRight(CellStyle.BORDER_THIN); // Umrandung rechts einschalten
csUeberschrift.setRightBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung rechts in schwarz
csUeberschrift.setDataFormat(dataFormat.getFormat("text")); // Datenformat Text
// Schriftart für Titel
Font fTitel = wb.createFont();
fTitel.setFontHeightInPoints((short) 24); // größe 24pt
fTitel.setColor(Font.COLOR_NORMAL); // schwarz
fTitel.setBoldweight(Font.BOLDWEIGHT_BOLD); // fett
// CellStyle für den Titel
csTitel.setFont(fTitel); // Schrift setzen
csTitel.setAlignment(CellStyle.ALIGN_CENTER_SELECTION); // Mittige Anordnung
csTitel.setDataFormat(dataFormat.getFormat("text")); // Datenformat Text
}
示例15: setStyles
import org.apache.poi.ss.usermodel.DataFormat; //导入依赖的package包/类
private void setStyles() {
csSpieleEtc = wb.createCellStyle(); // Stil für Spiele, Schiris, Zeiten etc.
csTitel = wb.createCellStyle(); // Stil für Titel
csUntertitel = wb.createCellStyle(); // Stil für Untertitel
csTabellenueberschrift = wb.createCellStyle(); // Stil für die Tabellenüberschriften (wie für Spiele, nur fett)
DataFormat dataFormat = wb.createDataFormat(); // Um das Datenformat auf Text zu setzen
// Schriftart für Titel
Font fTitel = wb.createFont();
fTitel.setFontHeightInPoints((short) 24); // größe 24pt
fTitel.setColor(Font.COLOR_NORMAL); // schwarz
fTitel.setBoldweight(Font.BOLDWEIGHT_BOLD); // fett
// CellStyle für den Titel
csTitel.setFont(fTitel); // Schrift setzen
csTitel.setAlignment(CellStyle.ALIGN_CENTER_SELECTION); // Mittige Anordnung
csTitel.setDataFormat(dataFormat.getFormat("text")); // Datenformat Text
// Schriftart für den Untertitel
Font fUntertitel = wb.createFont();
fUntertitel.setFontHeightInPoints((short) 10); // Schriftgröße auf 10pt setzen
fUntertitel.setColor(Font.COLOR_NORMAL); // Schriftfarbe schwarz
fUntertitel.setBoldweight(Font.BOLDWEIGHT_NORMAL);
// CellStyle für den Untertitel
csUntertitel.setFont(fUntertitel); // Schriftart
csUntertitel.setAlignment(CellStyle.ALIGN_CENTER_SELECTION); // Mittige Anordnung
csUntertitel.setDataFormat(dataFormat.getFormat("text")); // Datenformat Text
// Schriftart für Tabellenüberschriften
Font fTabellenueberschrift = wb.createFont();
fTabellenueberschrift.setFontHeightInPoints((short) 11); // größe 11pt
fTabellenueberschrift.setColor(Font.COLOR_NORMAL); // schwarz
fTabellenueberschrift.setBoldweight(Font.BOLDWEIGHT_BOLD); // fett
// CellStyle für die Tabellenüberschriften
csTabellenueberschrift.setFont(fTabellenueberschrift); // Schriftart
csTabellenueberschrift.setAlignment(CellStyle.ALIGN_CENTER_SELECTION); // Mittige Anordnung
csTabellenueberschrift.setBorderBottom(CellStyle.BORDER_THIN); // Umrandung unten einschalten
csTabellenueberschrift.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung unten in schwarz
csTabellenueberschrift.setBorderLeft(CellStyle.BORDER_THIN); // Umrandung links einschalten
csTabellenueberschrift.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung links in schwarz
csTabellenueberschrift.setBorderTop(CellStyle.BORDER_THIN); // Umrandung oben einschalten
csTabellenueberschrift.setTopBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung oben in schwarz
csTabellenueberschrift.setBorderRight(CellStyle.BORDER_THIN); // Umrandung rechts einschalten
csTabellenueberschrift.setRightBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung rechts in schwarz
csTabellenueberschrift.setDataFormat(dataFormat.getFormat("text")); // Datenformat Text
// Schriftart für Spiele, Schiris, Zeiten etc.
Font fSpieleEtc = wb.createFont();
fSpieleEtc.setFontHeightInPoints((short) 10); // Schriftgröße auf 10pt setzen
fSpieleEtc.setColor(Font.COLOR_NORMAL); // Schriftfarbe schwarz
fSpieleEtc.setBoldweight(Font.BOLDWEIGHT_NORMAL);
// CellStyle für Spiele, Schiris, Zeiten etc.
csSpieleEtc.setFont(fSpieleEtc); // Schriftart
csSpieleEtc.setAlignment(CellStyle.ALIGN_CENTER_SELECTION); // Mittige Anordnung
csSpieleEtc.setBorderBottom(CellStyle.BORDER_THIN); // Umrandung unten einschalten
csSpieleEtc.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung unten in schwarz
csSpieleEtc.setBorderLeft(CellStyle.BORDER_THIN); // Umrandung links einschalten
csSpieleEtc.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung links in schwarz
csSpieleEtc.setBorderTop(CellStyle.BORDER_THIN); // Umrandung oben einschalten
csSpieleEtc.setTopBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung oben in schwarz
csSpieleEtc.setBorderRight(CellStyle.BORDER_THIN); // Umrandung rechts einschalten
csSpieleEtc.setRightBorderColor(IndexedColors.BLACK.getIndex()); // Umrandung rechts in schwarz
csSpieleEtc.setDataFormat(dataFormat.getFormat("text")); // Datenformat Text
}