下面列出了org.apache.poi.ss.usermodel.Color#org.apache.poi.ss.usermodel.Font 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private CellStyle baseCellStyle(){
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setWrapText(true);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
Font font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//font.setFontName("宋体");
font.setFontHeight((short) 200);
cellStyle.setFont(font);
return cellStyle;
}
private CellStyle createContentStyle(){
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
cellStyle.setWrapText(false);// 指定单元格自动换行
// 设置单元格字体
Font font = wb.createFont();
//font.setFontName("微软雅黑");
font.setFontHeight((short) 200);
cellStyle.setFont(font);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
return cellStyle ;
}
/**
* 首列样式
* @return
*/
private CellStyle createFirstCellStyle(){
CellStyle cellStyle = baseCellStyle();
Font font = wb.createFont();
//font.setFontName("微软雅黑");
font.setFontHeight((short) 180);
cellStyle.setFont(font);
cellStyle.setWrapText(false);
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
return cellStyle;
}
/**
* create a library of cell styles
*/
private static Map<String, CellStyle> createStyles(Workbook wb){
Map<String, CellStyle> styles = new HashMap<>();
CellStyle style;
Font headerFont = wb.createFont();
headerFont.setBold(true);
style = createBorderedStyle(wb);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setVerticalAlignment(VerticalAlignment.TOP);
style.setWrapText(true);
style.setFont(headerFont);
styles.put("header", style);
style = createBorderedStyle(wb);
style.setVerticalAlignment(VerticalAlignment.TOP);
style.setWrapText(true);
styles.put("body", style);
return styles;
}
/**
* create a library of cell styles
*/
private static Map<String, CellStyle> createStyles(Workbook wb){
Map<String, CellStyle> styles = new HashMap<>();
CellStyle style;
Font headerFont = wb.createFont();
headerFont.setBold(true);
style = createBorderedStyle(wb);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setVerticalAlignment(VerticalAlignment.TOP);
style.setWrapText(true);
style.setFont(headerFont);
styles.put("header", style);
style = createBorderedStyle(wb);
style.setVerticalAlignment(VerticalAlignment.TOP);
style.setWrapText(true);
styles.put("body", style);
return styles;
}
@Override
public Font correctFontColorIfBackground( FontManager fm, Workbook wb, BirtStyle birtStyle, Font font ) {
CSSValue bgColour = birtStyle.getProperty( StyleConstants.STYLE_BACKGROUND_COLOR );
int bgRgb[] = parseColour( bgColour == null ? null : bgColour.getCssText(), "white" );
XSSFColor colour = ((XSSFFont)font).getXSSFColor();
int fgRgb[] = rgbOnly( colour.getARgb() );
if( ( fgRgb[0] == 255 ) && ( fgRgb[1] == 255 ) && ( fgRgb[2] == 255 ) ) {
fgRgb[0]=fgRgb[1]=fgRgb[2]=0;
} else if( ( fgRgb[0] == 0 ) && ( fgRgb[1] == 0 ) && ( fgRgb[2] == 0 ) ) {
fgRgb[0]=fgRgb[1]=fgRgb[2]=255;
}
if( ( bgRgb[ 0 ] == fgRgb[ 0 ] ) && ( bgRgb[ 1 ] == fgRgb[ 1 ] ) && ( bgRgb[ 2 ] == fgRgb[ 2 ] ) ) {
IStyle addedStyle = new AreaStyle( fm.getCssEngine() );
addedStyle.setColor( contrastColour( bgRgb ) );
return fm.getFontWithExtraStyle( font, addedStyle );
} else {
return font;
}
}
/**
* create the styles in the workbook
*/
private void createStyles(Workbook wb) {
// create the styles
this.checkboxStyle = wb.createCellStyle();
this.checkboxStyle.setAlignment(CellStyle.ALIGN_CENTER);
this.checkboxStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
this.checkboxStyle.setBorderBottom(CellStyle.BORDER_THIN);
this.checkboxStyle.setBorderLeft(CellStyle.BORDER_THIN);
this.checkboxStyle.setBorderRight(CellStyle.BORDER_THIN);
this.checkboxStyle.setBorderTop(CellStyle.BORDER_THIN);
Font checkboxFont = wb.createFont();
checkboxFont.setFontHeight(FONT_SIZE);
checkboxFont.setFontName(CHECKBOX_FONT_NAME);
this.checkboxStyle.setFont(checkboxFont);
this.dateStyle = wb.createCellStyle();
DataFormat df = wb.createDataFormat();
this.dateStyle.setDataFormat(df.getFormat("m/d/yy h:mm"));
}
private void makeHeader ( final List<Field> columns, final HSSFSheet sheet )
{
final Font font = sheet.getWorkbook ().createFont ();
font.setFontName ( "Arial" );
font.setBoldweight ( Font.BOLDWEIGHT_BOLD );
font.setColor ( HSSFColor.WHITE.index );
final CellStyle style = sheet.getWorkbook ().createCellStyle ();
style.setFont ( font );
style.setFillForegroundColor ( HSSFColor.BLACK.index );
style.setFillPattern ( PatternFormatting.SOLID_FOREGROUND );
final HSSFRow row = sheet.createRow ( 0 );
for ( int i = 0; i < columns.size (); i++ )
{
final Field field = columns.get ( i );
final HSSFCell cell = row.createCell ( i );
cell.setCellValue ( field.getHeader () );
cell.setCellStyle ( style );
}
}
SerialWriteWorkbook() {
this.swb = new SXSSFWorkbook(100);
this.sh = this.swb.createSheet();
this.rowIndex = 0;
this.headerStyle = (XSSFCellStyle)swb.createCellStyle();
this.headerStyle.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
//solid fill
this.headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = swb.createFont();
headerFont.setFontHeightInPoints((short)14);
headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex());
this.headerStyle.setFont(headerFont);
}
private static Font setFontColor(Font font, CustomColor customColor, String fontColor) {
Short colorPredefined = ColorUtil.getPredefinedColorIndex(fontColor);
if (colorPredefined != null) {
font.setColor(colorPredefined);
return font;
}
int[] rgb = ColorUtil.getRGBByColor(fontColor);
if (rgb == null) {
return null;
}
if (customColor.isXls()) {
short index = ColorUtil.getCustomColorIndex(customColor, rgb);
font.setColor(index);
} else {
((XSSFFont) font).setColor(new XSSFColor(new Color(rgb[0], rgb[1], rgb[2]), customColor.getDefaultIndexedColorMap()));
}
return font;
}
/**
* 获取模板指定位置上的已转为本工作薄的字体
*
* 目前看,只用于2003的版本(*.xls),2007的版本是可以直接 setFont() 方法设置字体的。
*
* @author ZhengWei(HY)
* @createDate 2017-03-18
* @version v1.0
*
* @param i_RTemplate 模板对象
* @param i_IDX 字体在模板中的索引位置
* @return
*/
public synchronized Font getFont(RTemplate i_RTemplate ,int i_IDX)
{
Font v_FromFont = i_RTemplate.getTemplateSheet().getWorkbook().getFontAt((short)i_IDX);
String v_FontID = makeFontID(v_FromFont);
Font v_ToFont = this.fonts.get(v_FontID);
if ( v_ToFont == null )
{
v_ToFont = this.workbook.createFont();
ExcelHelp.copyFont(v_FromFont ,v_ToFont);
this.fonts.put(v_FontID ,v_ToFont);
}
return v_ToFont;
}
/**
* 创建一个新的样式,样式从i_DataCell中克隆出来。
*
* @author ZhengWei(HY)
* @createDate 2017-09-11
* @version v1.0
*
* @param i_ID 标记ID。由调用者设定
* @param i_DataCell 被克隆的单元格样式
* @return
*/
public synchronized CellStyle getCellStyleByCopy(String i_ID ,Cell i_DataCell ,RTemplate i_RTemplate)
{
CellStyle v_NewCellStyle = this.cellStylesByCopy.get(i_ID);
if ( v_NewCellStyle == null )
{
v_NewCellStyle = this.workbook.createCellStyle();
ExcelHelp.copyCellStyle(i_DataCell.getCellStyle(), v_NewCellStyle);
Font v_FromFont = this.workbook.getFontAt(i_DataCell.getCellStyle().getFontIndex());
Font v_NewFont = this.workbook.createFont();
ExcelHelp.copyFont(v_FromFont ,v_NewFont);
v_NewCellStyle.setFont(v_NewFont);
this.cellStylesByCopy.put(i_ID ,v_NewCellStyle);
}
return v_NewCellStyle;
}
@Test
public void test_FontOne() throws IOException
{
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("sheet 01");
Row row = sheet.createRow(1);
Font font = wb.createFont();
font.setBold(true);
font.setColor((short) 13);
font.setFontHeightInPoints((short) 24);
font.setFontName("宋体");
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
Cell cell = row.createCell(1);
cell.setCellValue("这是测试字体格式的");
cell.setCellStyle(cellStyle);
FileOutputStream fileOutputStream = new FileOutputStream("D://font.xlsx");
wb.write(fileOutputStream);
wb.close();
}
/**
* 设置通用的对齐居中、边框等
*
* @param style 样式
*/
private void setCommonStyle(CellStyle style) {
// 设置单元格居中对齐、自动换行
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setWrapText(true);
//设置单元格字体
if (!buildInFontMap.containsKey(FONT_KEY)) {
Font font = workbook.createFont();
//通用字体
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 200);
buildInFontMap.put(FONT_KEY, font);
}
style.setFont(buildInFontMap.get(FONT_KEY));
// 设置单元格边框为细线条
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setBorderTop(CellStyle.BORDER_THIN);
}
public void setCellStyleFont(Workbook workbook, CellStyle style, int i) {
Font font = workbook.createFont();
if (i == 0) {
// 正常
} else if (i == 4) {
// 下划线
font.setUnderline(Font.U_SINGLE);
style.setFont(font);
} else if (i == 2) {
// 倾斜
font.setItalic(true);
style.setFont(font);
} else if (i == 1) {
// 加粗
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
style.setFont(font);
}
}
/**
* Get a Font matching the BIRT style, either from the cache or by creating a new one.
* @param birtStyle
* The BIRT style to base the Font upon.
* @return
* A Font whose attributes are described by the BIRT style.
*/
public Font getFont( BirtStyle birtStyle ) {
if( birtStyle == null ) {
return getDefaultFont();
}
if( ( birtStyle.getProperty( StyleConstants.STYLE_FONT_FAMILY ) == null )
&& ( birtStyle.getProperty( StyleConstants.STYLE_FONT_SIZE ) == null )
&& ( birtStyle.getProperty( StyleConstants.STYLE_FONT_WEIGHT ) == null )
&& ( birtStyle.getProperty( StyleConstants.STYLE_FONT_STYLE ) == null )
&& ( birtStyle.getProperty( StyleConstants.STYLE_TEXT_UNDERLINE ) == null )
&& ( birtStyle.getProperty( StyleConstants.STYLE_COLOR ) == null )
) {
return getDefaultFont();
}
for(FontPair fontPair : fonts) {
if(fontsEquivalent(birtStyle, fontPair.birtStyle)) {
return fontPair.poiFont;
}
}
return createFont(birtStyle);
}
private HSSFCellStyle createHSSFCellStyle(Workbook wb, int[] bgColor, int[] fontColor, int fontSize) {
HSSFWorkbook workbook = (HSSFWorkbook) wb;
HSSFPalette palette = workbook.getCustomPalette();
palette.setColorAtIndex((short) 9, (byte) fontColor[0], (byte) fontColor[1], (byte) fontColor[2]);
palette.setColorAtIndex((short) 10, (byte) bgColor[0], (byte) bgColor[1], (byte) bgColor[2]);
HSSFFont titleFont = workbook.createFont();
titleFont.setCharSet(HSSFFont.DEFAULT_CHARSET);
titleFont.setFontName("宋体");
titleFont.setColor((short) 9);
titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
titleFont.setFontHeightInPoints((short) fontSize);
HSSFCellStyle titleStyle = (HSSFCellStyle) createBorderCellStyle(workbook, true);
titleStyle.setFont(titleFont);
titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
titleStyle.setFillForegroundColor((short) 10);
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
return titleStyle;
}
private XSSFCellStyle createXSSFCellStyle(Workbook wb, int[] bgColor, int[] fontColor, int fontSize) {
SXSSFWorkbook workbook = (SXSSFWorkbook) wb;
XSSFFont titleFont = (XSSFFont) workbook.createFont();
titleFont.setCharSet(HSSFFont.DEFAULT_CHARSET);
titleFont.setFontName("宋体");
XSSFColor color9 = new XSSFColor(new java.awt.Color(fontColor[0], fontColor[1], fontColor[2]));
XSSFColor color10 = new XSSFColor(new java.awt.Color(bgColor[0], bgColor[1], bgColor[2]));
if (!(fontColor[0] == 0 && fontColor[1] == 0 && fontColor[2] == 0)) {
titleFont.setColor(color9);
}
titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
titleFont.setFontHeightInPoints((short) fontSize);
XSSFCellStyle titleStyle = (XSSFCellStyle) createBorderCellStyle(workbook, true);
titleStyle.setFont(titleFont);
titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
titleStyle.setFillForegroundColor(color10);
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
return titleStyle;
}
/**
* 创建表格样式
*
* @param wb 工作薄对象
* @return 样式列表
*/
private Map<String, CellStyle> createStyles(Workbook wb)
{
// 写入各条记录,每条记录对应excel表中的一行
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
Font dataFont = wb.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
style.setFont(dataFont);
styles.put("data", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = wb.createFont();
headerFont.setFontName("Arial");
headerFont.setFontHeightInPoints((short) 10);
headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex());
style.setFont(headerFont);
styles.put("header", style);
return styles;
}
private Font registerFont(SpreadsheetFont font) {
Font poiFont = fontMap.get(font);
if (poiFont == null) {
poiFont = createNewFont(font);
fontMap.put(font, poiFont);
}
return poiFont;
}
/**
* 首列样式
* @return
*/
@SuppressWarnings("deprecation")
private CellStyle createFirstCellStyle(){
CellStyle cellStyle = baseCellStyle();
Font font = wb.createFont();
font.setFontHeight((short) 180);
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
return cellStyle;
}
/**
* 内容样式
* @return
*/
@SuppressWarnings("deprecation")
private CellStyle createContentCellStyle(){
CellStyle cellStyle = baseCellStyle();
Font font = wb.createFont();
font.setFontHeight((short) 200);
font.setBoldweight((short)0);
cellStyle.setFont(font);
return cellStyle;
}
private void prontFonts(Workbook wb) {
for (short i = 0, le = wb.getNumberOfFonts(); i <= le; i++) {
Font font = wb.getFontAt(i);
out.format(".%s .%s {%n", DEFAULTS_CLASS, "font_" + i + "_" + cssRandom);
fontStyle(font);
out.format("}%n");
}
}
private Color getColor(Font font) {
if (helper instanceof HSSFHtmlHelper) {
return ((HSSFWorkbook) sheet.getWorkbook()).getCustomPalette().getColor(font.getColor());
} else {
return ((XSSFFont) font).getXSSFColor();
}
}
@Override
public CellStyle getHeaderStyle(short color) {
CellStyle titleStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
titleStyle.setFont(font);
titleStyle.setBorderLeft((short) 1); // 左边框
titleStyle.setBorderRight((short) 1); // 右边框
titleStyle.setBorderBottom((short) 1);
titleStyle.setBorderTop((short) 1);
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
return titleStyle;
}
@Override
public CellStyle getHeaderStyle(short headerColor) {
CellStyle titleStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 24);
titleStyle.setFont(font);
titleStyle.setFillForegroundColor(headerColor);
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
return titleStyle;
}
/**
* 设置表头
*
* @param wb
* @param sheet
* @param titles
* @return
*/
private static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, List<String> titles) {
int rowIndex = 0;
int colIndex = 0;
Font titleFont = wb.createFont();
//设置字体
titleFont.setFontName("simsun");
//设置粗体
titleFont.setBoldweight(Short.MAX_VALUE);
//设置字号
titleFont.setFontHeightInPoints((short) 14);
//设置颜色
titleFont.setColor(IndexedColors.BLACK.index);
XSSFCellStyle titleStyle = wb.createCellStyle();
//水平居中
titleStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
//垂直居中
titleStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
//设置图案颜色
titleStyle.setFillForegroundColor(new XSSFColor(new Color(182, 184, 192)));
//设置图案样式
titleStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
titleStyle.setFont(titleFont);
setBorder(titleStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
Row titleRow = sheet.createRow(rowIndex);
titleRow.setHeightInPoints(25);
colIndex = 0;
for (String field : titles) {
Cell cell = titleRow.createCell(colIndex);
cell.setCellValue(field);
cell.setCellStyle(titleStyle);
colIndex++;
}
rowIndex++;
return rowIndex;
}
/**
* 设置内容
*
* @param wb
* @param sheet
* @param rows
* @param rowIndex
* @return
*/
private static int writeRowsToExcel(XSSFWorkbook wb, Sheet sheet, List<List<Object>> rows, int rowIndex) {
int colIndex;
Font dataFont = wb.createFont();
dataFont.setFontName("simsun");
dataFont.setFontHeightInPoints((short) 14);
dataFont.setColor(IndexedColors.BLACK.index);
XSSFCellStyle dataStyle = wb.createCellStyle();
dataStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
dataStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
dataStyle.setFont(dataFont);
setBorder(dataStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
for (List<Object> rowData : rows) {
Row dataRow = sheet.createRow(rowIndex);
dataRow.setHeightInPoints(25);
colIndex = 0;
for (Object cellData : rowData) {
Cell cell = dataRow.createCell(colIndex);
if (cellData != null) {
cell.setCellValue(cellData.toString());
} else {
cell.setCellValue("");
}
cell.setCellStyle(dataStyle);
colIndex++;
}
rowIndex++;
}
return rowIndex;
}
/**
* O7 版本坑爹bug
* @param wb
*/
private void cacheFontInfo(Workbook wb) {
for (short i = 0, le = wb.getNumberOfFonts(); i < le; i++) {
Font font = wb.getFontAt(i);
fontCache.put(font.getBoldweight() + "_" + font.getItalic() + "_" + font.getFontName()
+ "_" + font.getFontHeightInPoints() + "_" + font.getColor(),
font.getIndex() + "");
}
}
private void fontStyle(Font font) {
if (font.getBoldweight() >= Font.BOLDWEIGHT_BOLD)
out.format(" font-weight: bold;%n");
if (font.getItalic())
out.format(" font-style: italic;%n");
out.format(" font-family: %s;%n", font.getFontName());
int fontheight = font.getFontHeightInPoints();
if (fontheight == 9) {
fontheight = 10;
}
out.format(" font-size: %dpt;%n", fontheight);
helper.styleColor(out, "color", getColor(font));
}