导出excel修改样式

protected HSSFWorkbook workbook = null;
protected HSSFSheet curSheet = null;
protected HSSFRow curRow = null;
protected HSSFCellStyle curStyle = null;

excel合并单元格
//合并第r行的,从第2列开始,第三列结束,相当于合并第r行的第2列与第三列
this.curSheet.addMergedRegion( new Region( r, ( short )1, r, ( short )3 ) );

//合并第1列的,从第r行合并到第r+5行
this.curSheet.addMergedRegion( new Region( r, ( short )0, r + 5, ( short )0 ) );

//修改样式
//修改excel下边框大小为0
this.curStyle.setBorderBottom((short)0);
//修改excel左边框大小为0
this.curStyle.setBorderLeft((short)0);

//单元格内容换行

强制换行 直接加"\n"
HSSFCellStyle style = workbook.createCellStyle();
style.setWrapText(true);//自动换行



//10号字体,带边框,垂直居中
    public HSSFCellStyle twFourLineCenter( HSSFWorkbook workbook ){
        HSSFCellStyle style = workbook.createCellStyle();
        HSSFFont font = workbook.createFont(); //定义字体
        font.setFontName( "宋体" );
        font.setFontHeightInPoints( ( short )10 );
        font.setBoldweight( ( short )1000000 );
        style.setFont( font );
        style.setBorderBottom( HSSFCellStyle.BORDER_THIN );
        style.setBottomBorderColor( HSSFColor.BLACK.index );
        style.setBorderLeft( HSSFCellStyle.BORDER_THIN );
        style.setLeftBorderColor( HSSFColor.BLACK.index );
        style.setBorderRight( HSSFCellStyle.BORDER_THIN );
        style.setRightBorderColor( HSSFColor.BLACK.index );
        style.setBorderTop( HSSFCellStyle.BORDER_THIN );
        style.setTopBorderColor( HSSFColor.BLACK.index );
        style.setVerticalAlignment( HSSFCellStyle.VERTICAL_CENTER );
        style.setAlignment( HSSFCellStyle.ALIGN_CENTER );
        style.setWrapText(true);
        return style;

    }

//设置exce行为黄色背景
this.curStyle=rowLineStyle(this.workbook);

/**
* 定义行样式
* @param workbook
* @return
*/
public HSSFCellStyle rowLineStyle( HSSFWorkbook workbook ){
HSSFCellStyle style = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints( ( short )10 );
font.setFontName( "宋体" );
font.setItalic( false );
font.setStrikeout( false );
font.setBoldweight( ( short )1000000 );
style.setFont( font );
// style.setFillBackgroundColor(HSSFColor.YELLOW.index);
style.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);    //填充的背景颜色
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(HSSFColor.YELLOW.index);   style.setVerticalAlignment( HSSFCellStyle.VERTICAL_CENTER );
style.setAlignment( HSSFCellStyle.ALIGN_LEFT );
style.setBorderRight((short)1);
style.setWrapText(true);
return style;
}

















你可能感兴趣的:(Excel)