ExcelStyle
来源:互联网 发布:nginx 多站点 编辑:程序博客网 时间:2024/06/10 15:02
package com.wyj.excel.test;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFClientAnchor;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.CellRangeAddress;import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Comment;import org.apache.poi.ss.usermodel.Drawing;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.ss.usermodel.RichTextString;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFCellStyle;import org.apache.poi.xssf.usermodel.XSSFClientAnchor;import org.apache.poi.xssf.usermodel.XSSFFont;import org.apache.poi.xssf.usermodel.XSSFRichTextString;/** * excel的样式 * * @author 王宜君 */public class ExcelStyle{ /** * 设置2003excel头部样式 * @param workbook 工作表 * @param style 样式 * @return */ public static HSSFCellStyle setHeadStyle( HSSFWorkbook workbook, HSSFCellStyle style ) { style.setFillForegroundColor( HSSFColor.SKY_BLUE.index ); style.setFillPattern( HSSFCellStyle.SOLID_FOREGROUND ); style.setBorderBottom( HSSFCellStyle.BORDER_THIN ); style.setBorderLeft( HSSFCellStyle.BORDER_THIN ); style.setBorderRight( HSSFCellStyle.BORDER_THIN ); style.setBorderTop( HSSFCellStyle.BORDER_THIN ); style.setAlignment( HSSFCellStyle.ALIGN_CENTER ); // 生成字体 HSSFFont font = workbook.createFont(); font.setColor( HSSFColor.VIOLET.index ); font.setFontHeightInPoints( (short)12 ); font.setBoldweight( HSSFFont.BOLDWEIGHT_BOLD ); // 把字体应用到当前的样样式 style.setFont( font ); return style; } public static CellStyle setHeadStyle( Workbook workbook ) { // 生成一个样式 CellStyle style = workbook.createCellStyle(); // 设置这些样式 style.setFillForegroundColor( HSSFColor.SKY_BLUE.index ); style.setFillPattern( XSSFCellStyle.SOLID_FOREGROUND ); style.setBorderBottom( XSSFCellStyle.BORDER_THIN ); style.setBorderLeft( XSSFCellStyle.BORDER_THIN ); style.setBorderRight( XSSFCellStyle.BORDER_THIN ); style.setBorderTop( XSSFCellStyle.BORDER_THIN ); style.setAlignment( XSSFCellStyle.ALIGN_CENTER ); style.setVerticalAlignment( XSSFCellStyle.VERTICAL_CENTER ); // 生成一个字体 Font font = workbook.createFont(); font.setColor( HSSFColor.VIOLET.index ); font.setFontHeightInPoints( (short)12 ); font.setBoldweight( XSSFFont.BOLDWEIGHT_BOLD ); // 把字体应用到当前的样式 style.setFont( font ); return style; } /** * 设置2003和2007excel体部样式 * @param workbook 工作表 * @return 样式 */ public static CellStyle setBodyStyle( Workbook workbook ) { // 生成并设置另一个样式 CellStyle style = workbook.createCellStyle(); style.setFillForegroundColor( HSSFColor.LIGHT_YELLOW.index ); style.setFillPattern( XSSFCellStyle.SOLID_FOREGROUND ); style.setBorderBottom( XSSFCellStyle.BORDER_THIN ); style.setBorderLeft( XSSFCellStyle.BORDER_THIN ); style.setBorderRight( XSSFCellStyle.BORDER_THIN ); style.setBorderTop( XSSFCellStyle.BORDER_THIN ); style.setAlignment( XSSFCellStyle.ALIGN_CENTER ); style.setVerticalAlignment( XSSFCellStyle.VERTICAL_CENTER ); // 生成另一个字体 Font font = workbook.createFont(); font.setBoldweight( XSSFFont.BOLDWEIGHT_NORMAL ); // 把字体应用到当前的样式 style.setFont( font ); return style; } /** * 设置excel标题的注释 * @param sheet 工作表 * @param regions 合并区域 * @param extension 扩展名 */ public static void setComment(Sheet sheet,List<CellRangeAddress> regions,String extension) { // 声明一个画图的顶级管理器 Drawing patriarch = sheet.createDrawingPatriarch(); // 设置合并单元格 if ( regions != null && regions.size() > 0 ) { for ( int i = 0; i < regions.size(); i++ ) { sheet.addMergedRegion( regions.get( i ) );// 指定合并区域 } } // sheet.addMergedRegion(new Region(1,(short)0,3,(short)0));//指定合并区域 Comment comment = null; RichTextString richText = null; if ( "xls".equals( extension ) ) { // 定义注释的大小和位置,详见文档 // patriarch.createCellComment(arg0) comment = patriarch.createCellComment( new HSSFClientAnchor( 0, 0, 0, 0, (short)4, 2, (short)6, 5 ) ); richText = new HSSFRichTextString( "可以在POI中添加注释!" ); } else if ( "xlsx".equals( extension ) ) { comment = patriarch.createCellComment( new XSSFClientAnchor( 0, 0, 0, 0, (short)4, 2, (short)6, 5 ) ); richText = new XSSFRichTextString( "可以在POI中添加注释!" ); } // 设置注释内容 comment.setString( richText ); // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容. comment.setAuthor( "leno" ); } /** * 设置2003的体部样式 * @param workbook 工作表 * @param style 单元格样式 * @return */ public static HSSFCellStyle setbodyStyle( HSSFWorkbook workbook, HSSFCellStyle style ) { style.setFillForegroundColor( HSSFColor.LIGHT_YELLOW.index ); style.setFillPattern( HSSFCellStyle.SOLID_FOREGROUND ); style.setBorderBottom( HSSFCellStyle.BORDER_THIN ); style.setBorderLeft( HSSFCellStyle.BORDER_THIN ); style.setBorderRight( HSSFCellStyle.BORDER_THIN ); style.setBorderTop( HSSFCellStyle.BORDER_THIN ); style.setAlignment( HSSFCellStyle.ALIGN_CENTER ); style.setVerticalAlignment( HSSFCellStyle.VERTICAL_CENTER ); // 生成字体 HSSFFont font = workbook.createFont(); font.setBoldweight( HSSFFont.BOLDWEIGHT_NORMAL ); // 把字体应用到当前的样样式 style.setFont( font ); return style; }}