java报表开发之单元格格式设置
1、1.实现原理1.1新建单元格新建一个单元格,位置为(1,1),列向占2个单元格行向占2个单元格,文本值为 "FineReport",位置从(0,0)开始TemplateCellElementcellElement=newDefaultTemplateCellElement(1,1,2,2,"FineReport");
2、1.2设置单元格行高、列宽设置第1列宽为300px,设置第1行高为30px,行列编号都是从0开始worksheet.setColumnWidth(1,newOLDPIX(300));worksheet.setRowHeight(1,newOLDPIX(30));
3、1.3获取单元格样式得到CellElement的样式,如果没有新建默认样式Stylestyle=cellElement.getStyle();if(style==null){style=Style.getInstance();}1.4设置单元格样式/设置单元格单元格的样式cellElement.setStyle(style);
4、1.5设置字体、字号等//设置字体和前景的颜色FRFontfrFont=FRFont.getInstance("Dialog",Font.BOLD,16);frFont=frFont.applyForeground(newColor(21,76,160));style=style.deriveFRFont(frFont);//设置背景ColorBackgroundbackground=ColorBackground.getInstance(newColor(255,255,177));style=style.deriveBackground(background);
5、//设置水平居中style=style.deriveHorizontalAlignment(Constants.CENTER)稆糨孝汶;1.6设置单元格边框设置边框样式和边框颜色style=style.deriveBorder(Constants.LINE_DASH,Color.red,Constants.LINE_DOT,Color.gray,Constants.LINE_DASH_DOT,Color.BLUE,Constants.LINE_DOUBLE,Color.CYAN);2.实现步骤改变单元格的格式,应先取出该单元格(CellElement)的格式(Style)。若您是新建一个单元格,则Style是null,故当取出Style后应先判断其值是否为null,如果这个值为空,则需先新建一个Style,然后再将该值赋给CellElement。最后根据Style和FRFont中的方法进一步地设置该单元格的各种属性。可执行代码如下://单元格格式设置packagecom.fr.demo;importjava.awt.Color;importjava.awt.Font;importjava.util.Map;
6、importcom.酆璁冻嘌fr.base.Style;importcom.fr.base.background.Co造婷用痃lorBackground;importcom.fr.general.FRFont;importcom.fr.report.cell.DefaultTemplateCellElement;importcom.fr.report.cell.TemplateCellElement;importcom.fr.report.worksheet.WorkSheet;importcom.fr.stable.Constants;importcom.fr.stable.unit.OLDPIX;importcom.fr.web.core.Reportlet;importcom.fr.web.request.ReportletRequest;importcom.fr.main.TemplateWorkBook;importcom.fr.main.impl.WorkBook;publicclassSetCellElementStyleextendsReportlet{publicTemplateWorkBookcreateReport(ReportletRequestarg0){
7、 //新建报表WorkBookworkbook=newWorkBook();WorkSheetworksheet=newWorkSheet();//新建一个单元格,位置为(1,1),列占2单元格,行占2单元格,文本值为"FineReport"TemplateCellElementcellElement=newDefaultTemplateCellElement(1,1,2,2,"FineReport");//设置列宽为300px,设置行高为30pxworksheet.setColumnWidth(1,newOLDPIX(300));worksheet.setRowHeight(1,newOLDPIX(30));//得到CellElement的样式,如果没有新建默认样式Stylestyle=cellElement.getStyle();if(style==null){style=Style.getInstance();}
8、 //设置字体和前景的颜色FRFontfrFont=FRFont.getInstance("Dialog",Font.BOLD,16);frFont=frFont.applyForeground(newColor(21,76,160));style=style.deriveFRFont(frFont);//设置背景ColorBackgroundbackground=ColorBackground.getInstance(newColor(255,255,177));style=style.deriveBackground(background);//设置水平居中style=style.deriveHorizontalAlignment(Constants.CENTER);//设置边框style=style.deriveBorder(Constants.LINE_DASH,Color.red,Constants.LINE_DOT,Color.gray,Constants.LINE_DASH_DOT,Color.BLUE,Constants.LINE_DOUBLE,Color.CYAN);//改变单元格的样式cellElement.setStyle(style);
9、 //将单元格添加到报表中worksheet.addCellElement(cellElement);workbook.addReport(worksheet);returnworkbook;}@OverridepublicvoidsetParameterMap(Maparg0){//TODOAuto-generatedmethodstub}@OverridepublicvoidsetTplPath(Stringarg0){//TODOAuto-generatedmethodstub}}
10、2.1发布并预览将编译后的SetCellElementStyle.class类放置在应用WEB-INF\classes\com\fr\demo下,启动服务器,在浏览器中访问该程序网络报表,地址如下:http://localhost:8075/WebReport/ReportServer?reportlet=com.fr.demo.SetCellElementStyle便可以看到我们定义的网络报表了。