1、生成头信息及页面
protected Element createPageXmlFileRoot( ) {
DftRptMaster dftRptMaster = rptInfo.getDftRptMaster();
DftViewMaster dftViewMaster = rptInfo.getDftViewMaster();
rptDomFileXml.addComment("Created with Jaspersoft Studio version 6.8.0.final using JasperReports Library version 6.8.0-2ed8dfabb690ff337a5797129f2cd92902b0c87b");
Element xmlFileRoot = rptDomFileXml.addElement("jasperReport","http://jasperreports.sourceforge.net/jasperreports");
xmlFileRoot.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlFileRoot.addAttribute("xsi:schemaLocation", "http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd");
//报表名称
xmlFileRoot.addAttribute( "name", this.rptInfo.getRptKey());
int columnCount =dftRptMaster.getPageColumns(); //分列数量
if (columnCount>1) {
xmlFileRoot.addAttribute( "columnCount", Integer.toString(columnCount)); //一页几列
}
xmlFileRoot.addAttribute( "pageWidth", dftRptMaster.getPageWidth().toString()) //页宽
.addAttribute( "pageHeight", dftRptMaster.getPageHeight().toString());//页高
if(dftRptMaster.getBePageVertical()!=1) { //如果页页是竖向
xmlFileRoot.addAttribute("orientation", "Landscape");
} else {
xmlFileRoot.addAttribute("orientation", "Portrait");
}
表示页面没有信息的时候怎么办,默认是no pages,如果你想没有数据的时候也显示,就用AllSectionsNoDetail
xmlFileRoot.addAttribute( "whenNoDataType", "AllSectionsNoDetail");
xmlFileRoot.addAttribute( "columnWidth", Integer.toString(rptInfo.getPageRealWidth()))
.addAttribute( "columnSpacing", dftViewMaster.getColSpace().toString() ) // 列间隔
.addAttribute( "leftMargin", dftRptMaster.getPageLeft().toString()) //
.addAttribute( "rightMargin", dftRptMaster.getPageRight().toString()) //
.addAttribute( "topMargin", dftRptMaster.getPageTop().toString()) //
.addAttribute( "bottomMargin", dftRptMaster.getPageBottom().toString()) //
.addAttribute( "isTitleNewPage", Boolean.toString(dftRptMaster.getBePrintCover()==1)) // TItle是否只显示在第一页中,这个和数据表”be_print_cover 是否打印封面“页配合使用
.addAttribute( "isSummaryWithPageHeaderAndFooter", "true") // 最后一页将Page Header(页头)、Page Footer(页脚)和Summary(栏)设置在一起显示
.addAttribute("isFloatColumnFooter", "true") //Float Column footer 在detail 1和footer中间去除大量空白 true指定列页脚部分是否应打印在列的底部
.addAttribute( "whenResourceMissingType", "Empty")
.addAttribute( "isIgnorePagination", "false") //true勾选上 取消分页功能
.addAttribute( "isSummaryNewPage", "false"); //合计起新页
//数据源
xmlFileRoot.addElement("property")
.addAttribute("name","com.jaspersoft.studio.data.defaultdataadapter")
.addAttribute("value", "One Empty Record" );
//设置换算单位 全部以px为单位计算,
xmlFileRoot.addElement("property")
.addAttribute("name","com.jaspersoft.studio.unit.*")
.addAttribute("value", "px");
return xmlFileRoot;
}
2、生成格式字段
/**
* 2 建立 格式内容,字体等
* @param xmlFileRoot
*/
protected void createPageStyle(Element xmlFileRoot) {
DftRptMaster dftRptMaster = this.rptInfo.getDftRptMaster();
DftViewMaster dftViewMaster = rptInfo.getDftViewMaster();
//默认格式
Element style = xmlFileRoot.addElement("style");
Element el = setBandStyleAttribute(style,JRptConstant.STYLE_DEFAULT,
dftViewMaster.getContextFontName(), dftViewMaster.getContextFontSize()==null?0:dftViewMaster.getContextFontSize(), dftViewMaster.getContextFontColor(),
dftViewMaster.getContextFontStyle());// 4 正文 ,,如果都没设置, 此为默认
setBoxLipenForm(style,"0.0",false,false,false,false);
el.addAttribute( "isDefault", "true");
//页眉页脚字体
style = xmlFileRoot.addElement("style");
setBandStyleAttribute(style,JRptConstant.STYLE_PAGEHEADER,
dftRptMaster.getPagehfFontName(), dftRptMaster.getPagehfFontSize()==null?0:dftRptMaster.getPagehfFontSize(), dftRptMaster.getPagehfFontColor(),
dftRptMaster.getPagehfFontStyle());
setBoxLipenForm(style,"0.0",false,false,false,false);
//标题字体
style = xmlFileRoot.addElement("style");
setBandStyleAttribute(style,JRptConstant.STYLE_PAGETITLE,
dftRptMaster.getTitleFontName(), dftRptMaster.getTitleFontSize()==null?0:dftRptMaster.getTitleFontSize(), dftRptMaster.getTitleFontColor(),
dftRptMaster.getTitleFontStyle());
setBoxLipenForm(style,"0.0",false,false,false,false);
//2副标题
style = xmlFileRoot.addElement("style");
setBandStyleAttribute(style,JRptConstant.STYLE_PAGESUBTITLE,
dftRptMaster.getSubheaderFontName(), dftRptMaster.getSubheaderFontSize()==null?0:dftRptMaster.getSubheaderFontSize(), dftRptMaster.getSubheaderFontColor(),
dftRptMaster.getSubheaderFontStyle());
setBoxLipenForm(style,"0.0",false,false,false,false);
Boolean beHasHLine = dftRptMaster.getBeHasHline()==1 ||dftViewMaster.getBeHasTopTitle()==1;//双标题必须要线
Boolean beHasVLine = dftRptMaster.getBeHasVline()==1 ||dftViewMaster.getBeHasTopTitle()==1;
//栏位明细表头
style = xmlFileRoot.addElement("style");
el = setBandStyleAttribute(style,JRptConstant.STYLE_COLHEADER,
dftViewMaster.getHeaderFontName(), dftViewMaster.getHeaderFontSize()==null?0:dftViewMaster.getHeaderFontSize(), dftViewMaster.getHeaderFontColor(),
dftViewMaster.getHeaderFontStyle());// 3 表 栏头, 小计,合计,分组头等
el.addAttribute("vTextAlign", "Middle");
if (dftRptMaster.getBeHasVline()==1) { //如果有竖线,则表头居中,如果没竖线,栏位根据栏位来定
el.addAttribute("hTextAlign","Center");
}
if (!StringUtils.isEmpty(dftViewMaster.getHeaderBackground())) {
style.addAttribute("backcolor", dftViewMaster.getHeaderBackground());
}
setBoxLipenForm(style,Double.toString(JRptConstant.LINE_WIDTH*2),true,beHasVLine,true,beHasVLine);
//明细
style = xmlFileRoot.addElement("style");
el = setBandStyleAttribute(style,JRptConstant.STYLE_COLCONTEXT,
dftViewMaster.getContextFontName(), dftViewMaster.getContextFontSize()==null?0:dftViewMaster.getContextFontSize(), dftViewMaster.getContextFontColor(),
dftViewMaster.getContextFontStyle());// 4 正文 ,,如果都没设置, 此为默认
setBoxLipenForm(style,Double.toString(JRptConstant.LINE_WIDTH),beHasHLine,beHasVLine,beHasHLine,beHasVLine);
}
3、注册字段
/**
* 4 注册字段信息
* @param xmlFileRoot
*/
protected void registDataFieldList(Element xmlFileRoot ) {
List<DftViewCol> lstViewCols = rptInfo.getLstViewCols();
DftViewCol dftViewCol = null;
Element field = null;
int tlColsWidth =0;
int colSpace = rptInfo.getDftViewMaster().getColSpace();
String fieldName = "";
List<DftViewCol> sortCols = new ArrayList<DftViewCol>();
for (int i =0 ; i< lstViewCols.size(); i++) {
dftViewCol = lstViewCols.get(i);
fieldName = dftViewCol.getFieldName().trim();
field = xmlFileRoot.addElement("field").addAttribute("name",fieldName)
.addAttribute("class", this.getFieldClassByDataType(dftViewCol.getDataType()));
field.addElement("property").addAttribute("name","net.sf.jasperreports.json.field.expression")
.addAttribute("value", fieldName);
field.addElement("fieldDescription").addCDATA(fieldName);
if (dftViewCol.getBeGroupKey()==1) { //找到分组KEY
rptInfo.setGrpKeyField(dftViewCol);
}
if (dftViewCol.getBeSort()==1) {
sortCols.add(dftViewCol);
}
if (dftViewCol.getBeInGroup()==1) {
rptInfo.getLstGrpCols().add(dftViewCol);
}
//生成小计合计变量栏位集合
if ((getRptCalcFunctionString(dftViewCol.getSumType())!=null)) {
rptInfo.getLstSumCols().add(dftViewCol);
}
tlColsWidth = tlColsWidth + dftViewCol.getColWidth() + colSpace;
}
if (rptInfo.getDftViewMaster().getColStretch().equalsIgnoreCase("all")) { //如果平均列
tlColsWidth = lstViewCols.size() * rptInfo.getAvgColsWidth();
}
rptInfo.setTotalColsWidth(tlColsWidth);
//计算起始位置和最后止位置
int firstX = 0;
int endX=0;
if (tlColsWidth >= rptInfo.getPageRealWidth()) {
firstX =0;endX =rptInfo.getPageRealWidth();
} else {
if (rptInfo.getDftRptMaster().getBePageHcenter()==1) { //如果居中
firstX = (rptInfo.getPageRealWidth() - tlColsWidth) /2;
endX = firstX + tlColsWidth;
} else {
firstX =0;
endX = tlColsWidth;
//如果不居中并且拉伸最后一列
if (rptInfo.getDftViewMaster().getColStretch().equalsIgnoreCase("last")) {
endX =rptInfo.getPageRealWidth();
}
}
}
rptInfo.setTotalColsWidth(tlColsWidth);
rptInfo.setFirstColStartX(firstX);
rptInfo.setLastColEndX(endX);
for (DftViewCol sortCol:sortCols) {
field =xmlFileRoot.addElement("sortField").addAttribute("name", sortCol.getFieldName());
if (sortCol.getBeSortAsc()!=1) {
field.addAttribute("order","Descending");
}
}
}
4、注册参数
/**
* 3 建立报表额外参数,如公司信息,LOGO等
* @param xmlFileRoot
* @return
*/
protected void createPageParam (Element xmlFileRoot ) {
xmlFileRoot.addElement("parameter").addAttribute("name","pa_org_no").addAttribute("class", "java.lang.String").addAttribute("isForPrompting", "false");
xmlFileRoot.addElement("parameter").addAttribute("name","pa_org_name").addAttribute("class", "java.lang.String").addAttribute("isForPrompting", "false");
xmlFileRoot.addElement("parameter").addAttribute("name","pa_img_log").addAttribute("class", "java.io.InputStream").addAttribute("isForPrompting", "false");
xmlFileRoot.addElement("parameter").addAttribute("name","pa_addr").addAttribute("class", "java.lang.String").addAttribute("isForPrompting", "false");
xmlFileRoot.addElement("parameter").addAttribute("name","pa_tel").addAttribute("class", "java.lang.String").addAttribute("isForPrompting", "false");
}
5、注册变量
/**
* 5, 建立 变量信息
* @param xmlFileRoot
*/
protected void createPageVariableInfo(Element xmlFileRoot) {
DftRptMaster dftRptMaster = rptInfo.getDftRptMaster();
//建立页码变量
xmlFileRoot.addElement("variable").addAttribute("name", "var_pageNo").addAttribute("class", "java.lang.Integer")
.addAttribute("resetType","Page")
.addElement("variableExpression").addCDATA("$V{PAGE_NUMBER}");
//总页数变量
xmlFileRoot.addElement("variable").addAttribute("name", "var_totalPage").addAttribute("class", "java.lang.Integer")
.addAttribute("resetType","Page").addAttribute("calculation", "Highest")
.addElement("variableExpression").addCDATA("$V{PAGE_NUMBER}");
//页眉变量 这儿需要根据模板内容格式定义规范修改取值
xmlFileRoot.addElement("variable").addAttribute("name", "var_pageHeader_Left").addAttribute("class", "java.lang.String")
.addElement("variableExpression").addCDATA("\""+ dftRptMaster.getPageHeaderLeft()+"\"");
xmlFileRoot.addElement("variable").addAttribute("name", "var_pageHeader_Mid").addAttribute("class", "java.lang.String")
.addElement("variableExpression").addCDATA("\""+dftRptMaster.getPageHeaderCenter()+"\"");
xmlFileRoot.addElement("variable").addAttribute("name", "var_pageHeader_Right").addAttribute("class", "java.lang.String")
.addElement("variableExpression").addCDATA("\""+dftRptMaster.getPageHeaderRight()+"\"");
//页脚变量
xmlFileRoot.addElement("variable").addAttribute("name", "var_pageFooter_Left").addAttribute("class", "java.lang.String")
.addElement("variableExpression").addCDATA("\""+dftRptMaster.getPageFooterLeft()+"\"");
xmlFileRoot.addElement("variable").addAttribute("name", "var_pageFooter_Mid").addAttribute("class", "java.lang.String")
.addElement("variableExpression").addCDATA("\""+dftRptMaster.getPageFooterCenter()+"\"");
xmlFileRoot.addElement("variable").addAttribute("name", "var_pageFooter_Right").addAttribute("class", "java.lang.String")
.addElement("variableExpression").addCDATA("\""+dftRptMaster.getPageFooterRight()+"\"");
//表头条文
if (!StringUtils.isEmpty(dftRptMaster.getRptBegin())) {
xmlFileRoot.addElement("variable").addAttribute("name", "var_RptBegin").addAttribute("class", "java.lang.String")
.addElement("variableExpression").addCDATA("\""+dftRptMaster.getRptBegin()+"\"");
}
//表尾条文
if (!StringUtils.isEmpty(dftRptMaster.getRptEnd())) {
xmlFileRoot.addElement("variable").addAttribute("name", "var_Rptend").addAttribute("class", "java.lang.String")
.addElement("variableExpression").addCDATA("\""+dftRptMaster.getRptEnd()+"\"");
}
String calcFun = null;
for (DftViewCol dftViewCol: rptInfo.getLstSumCols()) {
//分组小计变量
//合计变量
calcFun = getRptCalcFunctionString(dftViewCol.getSumType());
if (calcFun!=null) {
xmlFileRoot.addElement("variable")
.addAttribute("name", JRptConstant.VAR_SUBTL_PRE+dftViewCol.getFieldName())
.addAttribute("class", this.getFieldClassByDataType(dftViewCol.getDataType()))
.addAttribute("resetType","Group").addAttribute("resetGroup", JRptConstant.RPTGRP_NAME)
.addAttribute("calculation", calcFun)
.addElement("variableExpression").addCDATA("$F{"+dftViewCol.getFieldName()+"}");
//建立合计变量
xmlFileRoot.addElement("variable")
.addAttribute("name", JRptConstant.VAR_TL_PRE+dftViewCol.getFieldName())
.addAttribute("class", this.getFieldClassByDataType(dftViewCol.getDataType()))
.addAttribute("resetType","Report")
.addAttribute("calculation", calcFun)
.addElement("variableExpression").addCDATA("$F{"+dftViewCol.getFieldName()+"}");
}
}
}
6、标题生成
/**
* 建立 pageTitle
* @param xmlRoot
*/
protected void createPageTitleInfo(Element xmlRoot, Element titleBand,int startY) {
DftRptMaster dftRptMaster = rptInfo.getDftRptMaster();
int yPosion = startY;
//标题
Element txt = titleBand.addElement("staticText");
Element re = txt.addElement("reportElement").addAttribute("key", "rptTitle")
.addAttribute("style", JRptConstant.STYLE_PAGETITLE)
.addAttribute("positionType", "Float")
.addAttribute("stretchType", "RelativeToTallestObject")
.addAttribute("x", "0")
.addAttribute("y",Integer.toString(yPosion))
.addAttribute("width", Integer.toString(rptInfo.getPageRealWidth()))
.addAttribute("height",Integer.toString(JRptConstant.HEIGHT_UINT))
.addAttribute("isPrintWhenDetailOverflows", "true");//此属性是自动折行
re.addElement("property")
.addAttribute("name","com.jaspersoft.studio.element.name")
.addAttribute("value", "rptTitle");
txt.addElement("textElement")
.addAttribute("textAlignment", "Center")
.addAttribute("verticalAlignment", "Middle")
.addAttribute("markup", "styled");
txt.addElement("text").addCDATA(dftRptMaster.getRptDescLangno()); //标题内容
yPosion =yPosion +JRptConstant.HEIGHT_UINT ;
if (!StringUtils.isEmpty(dftRptMaster.getRptSubheadLangno())) {
//副标题
txt = titleBand.addElement("staticText");
re = txt.addElement("reportElement").addAttribute("key", "rptSubTitle")
.addAttribute("style", JRptConstant.STYLE_PAGESUBTITLE)
.addAttribute("positionType", "Float")
.addAttribute("stretchType", "RelativeToTallestObject")//此属性是自动折行
.addAttribute("x", "0")
.addAttribute("y", Integer.toString(yPosion))
.addAttribute("width", Integer.toString(rptInfo.getPageRealWidth()))
.addAttribute("height",Integer.toString(JRptConstant.HEIGHT_UINT-1))
.addAttribute("isRemoveLineWhenBlank", "true")
.addAttribute("isPrintWhenDetailOverflows", "true");
re.addElement("property")
.addAttribute("name","com.jaspersoft.studio.element.name")
.addAttribute("value", "rptSubTitle");
txt.addElement("textElement")
.addAttribute("textAlignment", "Center")
.addAttribute("verticalAlignment", "Middle")
.addAttribute("markup", "styled");
txt.addElement("text").addCDATA(dftRptMaster.getRptSubheadLangno()); //标题内容
yPosion =yPosion +JRptConstant.HEIGHT_UINT ;
}
//如果打印查询条件
if (dftRptMaster.getBePrintDataFilter()==1 && !StringUtils.isEmpty(rptInputInfo.getQueryConditionText())) {
//报表查询条件
txt = titleBand.addElement("staticText");
re = txt.addElement("reportElement").addAttribute("key", "rptFilterText")
.addAttribute("style", JRptConstant.STYLE_PAGESUBTITLE)
.addAttribute("positionType", "Float")
.addAttribute("stretchType", "RelativeToTallestObject")//此属性是自动折行
.addAttribute("x", Integer.toString(rptInfo.getFirstColStartX())) //和起始列并齐
.addAttribute("y", Integer.toString(yPosion))
.addAttribute("width", Integer.toString(rptInfo.getLastColEndX() -rptInfo.getFirstColStartX() ))
.addAttribute("height",Integer.toString(JRptConstant.HEIGHT_UINT-1))
.addAttribute("isRemoveLineWhenBlank", "true")
.addAttribute("isPrintWhenDetailOverflows", "true");
re.addElement("property")
.addAttribute("name","com.jaspersoft.studio.element.name")
.addAttribute("value", "rptFilterText");
txt.addElement("textElement")
.addAttribute("textAlignment", "Left")
.addAttribute("verticalAlignment", "Middle")
.addAttribute("markup", "styled");
txt.addElement("text").addCDATA("\"" + rptInputInfo.getQueryConditionText().trim()+"\"");
yPosion =yPosion +JRptConstant.HEIGHT_UINT ;
}
//如果打印表头条文
if (dftRptMaster.getBePrintRptBegin()==1 && !StringUtils.isEmpty(dftRptMaster.getRptBegin())) {
txt = titleBand.addElement("textField").addAttribute("evaluationTime", "Auto");
re = txt.addElement("reportElement").addAttribute("key", "rptBegin")
.addAttribute("style", JRptConstant.STYLE_PAGESUBTITLE)
.addAttribute("positionType", "Float")
.addAttribute("stretchType", "RelativeToTallestObject")//此属性是自动折行
.addAttribute("x", Integer.toString(rptInfo.getFirstColStartX())) //和起始列并齐
.addAttribute("y", Integer.toString(yPosion))
.addAttribute("width", Integer.toString(rptInfo.getLastColEndX() -rptInfo.getFirstColStartX() ))
.addAttribute("height",Integer.toString(JRptConstant.HEIGHT_UINT-1))
.addAttribute("isRemoveLineWhenBlank", "true")
.addAttribute("isPrintWhenDetailOverflows", "true");
txt.addElement("textElement")
.addAttribute("textAlignment", "Left")
.addAttribute("verticalAlignment", "Middle")
.addAttribute("markup", "styled");
txt.addElement("textFieldExpression").addCDATA("$V{var_RptBegin}") ;
yPosion =yPosion +JRptConstant.HEIGHT_UINT ;
}
// 如果要打印lobgo,将logo放于TITLE左区
if (dftRptMaster.getBePrintLogo() == 1) {
Element img =titleBand.addElement("image")
.addAttribute("hAlign","Center").addAttribute("vAlign", "Middle");
img.addElement("reportElement").addAttribute("key", "imgLogo")
.addAttribute("stretchType", "ContainerHeight")
.addAttribute("mode", "Transparent")
.addAttribute("x", "0").addAttribute("y", Integer.toString(startY))
.addAttribute("width", Integer.toString(rptInfo.getTitleHeight()*2))
.addAttribute("height", Integer.toString(rptInfo.getTitleHeight()))
.addAttribute("isRemoveLineWhenBlank","true")
.addElement("property")
.addAttribute("name", "com.jaspersoft.studio.element.name")
.addAttribute("value", "imgLogo");
img.addElement("imageExpression").addCDATA("$P{pa_img_log}");
}
}
7、页眉页脚生成
/**
* 建立页眉区
* @param xmlRoot
*/
protected void createPageHeaderInfo(Element xmlRoot) {
DftRptMaster dftRptMaster = rptInfo.getDftRptMaster();
//如果不打印页眉,并且标题区不打印在每页上(标题就不建立在页眉上),则不用建立页眉
if ((dftRptMaster.getBePrintTitlePerPage()!=1) && (dftRptMaster.getBePrintPageHeader()!=1)) return ;
int bandHeight =dftRptMaster.getBePrintTitlePerPage()==1?JRptConstant.HEIGHT_PAGEHEADERBAND + rptInfo.getTitleHeight():
JRptConstant.HEIGHT_PAGEHEADERBAND;
Element pageHeadBand = xmlRoot.addElement("pageHeader").addElement("band").addAttribute("height", Integer.toString(bandHeight)) ;
if (dftRptMaster.getBePrintTitlePerPage()!=1) {
pageHeadBand.addElement("printWhenExpression").addCDATA("$V{PAGE_NUMBER}!=1");
}
int startX =0;
if (dftRptMaster.getBePrintPageHeader()==1){
fillPageHeaderAndPageFooterContent(pageHeadBand,bandHeight,true);
startX = JRptConstant.HEIGHT_PAGEHEADERBAND;
}
if (dftRptMaster.getBePrintTitlePerPage()==1) {
rptInfo.setBandOfTitle(pageHeadBand);
createPageTitleInfo(xmlRoot, pageHeadBand,startX);
}
}
/**
* 建立页脚区
* @param xmlRoot
*/
protected void createPageFooterInfo(Element xmlRoot) {
if (rptInfo.getDftRptMaster().getBePrintPageFooter() ==1) {
Element footerBand = xmlRoot.addElement("pageFooter").addElement("band")
.addAttribute("height", Integer.toString(JRptConstant.HEIGHT_PAGEHEADERBAND ));
this.fillPageHeaderAndPageFooterContent(footerBand, JRptConstant.HEIGHT_PAGEHEADERBAND, false);
};
}
很多设置参数第一章节的数据结构设计
上一篇:Jasper Report 6.8 根据后台数据生成动态报表(JRXML文件实现)(二)生成XML文件(支持json,bean,map list数据源)