本生成文件仅仅是处理基本列表和分组报表过程
先看效果图
1 全局常量
public class JRptConstant {
public static final String STYLE_DEFAULT = "STYLE_DEFAULT";// 默认
public static final String STYLE_PAGEHEADER = "STYLE_PAGEHEADER";// 页眉页尾
public static final String STYLE_PAGETITLE = "STYLE_PAGETITLE"; // 报表 标题
public static final String STYLE_PAGESUBTITLE = "STYLE_PAGESUBTITLE"; // 副标题
public static final String STYLE_COLHEADER = "STYLE_COLHEADER"; // 表 栏头, 小计,合计,分组头等
public static final String STYLE_COLCONTEXT = "STYLE_COLCONTEXT"; // 正文
public static final String RPTGRP_NAME = "rptGrp"; // 分组BAND名称
public static final String DFT_FONTNAME ="宋体";
public static final int DFT_FONTSIZE =10;
public static final String DFT_FONTCOLOR ="#000000";
public static final String FLAG_YES ="Y";
public static final String FLAG_NO ="";
public static final String FLAG_TOTAL ="总计:";
public static final String FLAG_SUBTOTAL ="小计:";
public static final String VAR_SUBTL_PRE ="var_subtl_";
public static final String VAR_TL_PRE ="var_tl_";
public static final String COLHEADER_BACKCOLOR="#E1E1E1";
//public static final String RPT_DATASOURCE_NAME = "Datasource_RPT";
public static final double LINE_WIDTH =0.5; // 标题高度
public static final int HEIGHT_UINT =25; // 基本行高
public static final int HEIGHT_PAGEHEADERBAND = 25; // 页眉页脚高度
public static final int WIDTH_GRPTEXT = 60; // 分组标题4个汉字宽度默认
public enum RptDataSourceType {
JRDS_JavaBean ,
JRDS_JsonFile ,
JRDS_JsonString,
JRDS_ListMap ;
}
public enum RptExportType {
JRET_FileType ,
JRET_OutputStream,
}
}
2 输入参数类
public class JRptInputInfo implements Serializable {
/**
* 网格ID标识 或报表ID 标识
*/
private String rptId;
/*
* 哪个用户要取报表
*/
private String userKey;
/**
* 语言类型
*/
private String langType;
private RptDataSourceType dataSourceType ;
private String queryConditionText; //打印查询条件文本
private String dataOfJsonString;
private String dataOfJsonFile;
private List<?> dataOfBeanList;
private List<HashMap<String, Object>> dataOfMapList;
/**
* 要获取的文件类型:pdf,excels,excel,text,html,word,image,
*/
private Enum_FileType exportFileType =Enum_FileType.FILETYPE_HTML;
....省去get set
3 生成XML文件时保存的报表信息类
public class JRptReportInfo {
private String rptKey; //对应关键字唯一性
private DftViewMaster dftViewMaster; // 表格主表
private DftRptMaster dftRptMaster; // 报表 设置
private List<DftViewCol> lstViewCols; // 栏位设置信息
private List<DftViewCol> lstGrpCols = new ArrayList<DftViewCol>();
private List<DftViewCol> lstSumCols = new ArrayList<DftViewCol>();
private int pageRealWidth ; //页面实际可显示区宽度
private int totalColsWidth=0 ; //所有列排列后总显示数据宽度
private Boolean beColUseAvgWidth = false;
private int avgColsWidth=0 ; //表平均宽度
private int firstColStartX =0; //第一列显示起始位置,
private int lastColEndX =0; //最后一列显示结束位置,
private int pageHeaderHeight =0 ; //标题高度
private int titleHeight =0 ; //标题高度
private int sumarryHeight=0 ; //标题高度
private int colHeaderHeight=0 ; //标题栏高度
private int colDetailHeight=0 ; //明细栏高度
private Element bandOfTitle; //TITLE = title 或pageHeader
private Element bandOfGrpHeader; //表分组头栏
private Element bandOfGrpFooter; //表分组头栏
//分组KEY字段
private DftViewCol grpKeyField;
private int colFontWidth ; //明细栏字段宽度
private String rptWorkFilePath =""; //工作目录
4 初始化报表所需要数据(初始化JRptReportInfo类)
/**
* 初始化报表的一些参数和数据
* @param jrInputInfo
*/
private void setRptInfoInitVariableValue(JRptInputInfo jrInputInfo) {
lstRptProvider = new JRptListRptProvider() ;
JRptReportInfo jrRptInfo = new JRptReportInfo();
jrRptInfo.setRptKey(jrInputInfo.getUserKey() + '_' + jrInputInfo.getRptId());
lstRptProvider.setRptInfo( App.initJasperReportDataSettting(jrRptInfo,jrInputInfo));
DftRptMaster dftRptMaster = lstRptProvider.getRptInfo().getDftRptMaster();
DftViewMaster dftViewMaster = lstRptProvider.getRptInfo().getDftViewMaster();
//实际内容宽
int calcHeight = dftRptMaster.getPageWidth() - dftRptMaster.getPageLeft()- dftRptMaster.getPageRight();
if ( dftRptMaster.getPageColumns()>1) {
calcHeight = calcHeight /dftRptMaster.getPageColumns() - dftViewMaster.getColSpace() /2;
}
jrRptInfo.setPageRealWidth( calcHeight);
//计算平均列宽度,用于列不按实际宽度,平均宽度
jrRptInfo.setBeColUseAvgWidth(dftViewMaster.getColStretch().equalsIgnoreCase("all"));
if (jrRptInfo.getLstViewCols().size()>0) {
jrRptInfo.setAvgColsWidth(jrRptInfo.getPageRealWidth()/jrRptInfo.getLstViewCols().size());
}
//计算总宽度和起始位置,放在注册字段中处理
calcHeight = JRptConstant.HEIGHT_UINT;
//栏头高度
calcHeight = dftViewMaster.getRowHeaderHeight() + dftViewMaster.getRowSpace();
if (calcHeight <=0 ) calcHeight = JRptConstant.HEIGHT_UINT;
jrRptInfo.setColHeaderHeight(dftViewMaster.getBeHasTopTitle()==1?(calcHeight+calcHeight):calcHeight);
//计算标题高度------
//有副标题
calcHeight = JRptConstant.HEIGHT_UINT;
if (!StringUtils.isEmpty(dftRptMaster.getRptSubheadLangno())) {
calcHeight = calcHeight +JRptConstant.HEIGHT_UINT;
}
//如果打印查询条件
if (dftRptMaster.getBePrintDataFilter()==1 && !StringUtils.isEmpty(jrInputInfo.getQueryConditionText())) {
calcHeight = calcHeight +JRptConstant.HEIGHT_UINT;
}
//如果打印表头条文
if (dftRptMaster.getBePrintRptBegin()==1 && !StringUtils.isEmpty(dftRptMaster.getRptBegin())) {
calcHeight = calcHeight +JRptConstant.HEIGHT_UINT;
}
//如果栏头不每页显示,则栏头需要打印在title区中
if (dftRptMaster.getBePrintHeaderPerPage()!=1) {
calcHeight = calcHeight + jrRptInfo.getColHeaderHeight();
}
jrRptInfo.setTitleHeight(calcHeight);
//计算页眉高度
if ((dftRptMaster.getBePrintTitlePerPage()==1)|| (dftRptMaster.getBePrintPageHeader()==1)) {
calcHeight = JRptConstant.HEIGHT_PAGEHEADERBAND;
if (dftRptMaster.getBePrintTitlePerPage()==1) {//每页都打印标题,则高度加上TITLE 高度
calcHeight = calcHeight + jrRptInfo.getTitleHeight();
}
jrRptInfo.setPageHeaderHeight(calcHeight);
}
//明细行高
calcHeight = dftViewMaster.getRowHeight() + dftViewMaster.getRowSpace();
if (calcHeight <=0 ) calcHeight = JRptConstant.HEIGHT_UINT;
jrRptInfo.setColDetailHeight(calcHeight);
// 计算合计高度
//如果打印总计,或要打印表尾条文,或合计后打印图表
calcHeight = dftViewMaster.getRowHeaderHeight() + dftViewMaster.getRowSpace();
if (dftRptMaster.getBePrintPageSummary()==1||(dftRptMaster.getBePrintRptEnd()==1) ) {
if (dftRptMaster.getBePrintRptEnd()==1) {
calcHeight = calcHeight+calcHeight;
}
jrRptInfo.setSumarryHeight(calcHeight);
}
}
5 编译报表文件并输出相应文件
private boolean exportReportToFile(JRptInputInfo jrInputInfo ) {
// 12 编译 文件
//加载jrxml模板
try {
JasperDesign jdesign = JRXmlLoader.load(new File(lstRptProvider.getRptInfo().getRptWorkFilePath()
+lstRptProvider.getRptInfo().getRptKey()+".jrxml"));
//编译模板
JasperReport jasperReport = JasperCompileManager.compileReport(jdesign);
JRDataSource jrDs = null;
switch ( jrInputInfo.getDataSourceType()) {
case JRDS_JavaBean:
jrDs = new JRBeanCollectionDataSource(jrInputInfo.getDataOfBeanList());
break;
case JRDS_JsonString:
String jsonStr = jrInputInfo.getDataOfJsonString();
InputStream inputStrem = new ByteArrayInputStream(jsonStr.getBytes());
jrDs = new JsonDataSource(inputStrem);
break;
case JRDS_JsonFile:
FileInputStream jsonInStream = null;
try {
jsonInStream = new FileInputStream(jrInputInfo.getDataOfJsonFile());
} catch (Exception e){
e.printStackTrace();
}
jrDs = new JsonDataSource(jsonInStream);
break;
case JRDS_ListMap:
//List<HashMap<String, Object>> lst = rptInitModel.getDataOfMapList();
//Collection clt = rptInitModel.getDataOfMapList();;
jrDs = new JRMapCollectionDataSource((Collection) jrInputInfo.getDataOfMapList() );
break;
default:
return false;
}
/*数组
HashMap[] resportRows = new HashMap[rptInitModel.getDataOfMapList().size()];
HashMap<String, Object> hp = null;
List<HashMap<String, Object>> lst = rptInitModel.getDataOfMapList();
for(int i=0;i<resportRows.length; i++) { //HashMap hp:rptInitModel.getDataOfMapList()) {
hp = lst.get(i) ;
resportRows[i] = hp;
}
JRMapArrayDataSource jrDs = new JRMapArrayDataSource(resportRows);
*/
JasperPrint jrPrint = JasperFillManager.fillReport(jasperReport, null, jrDs);
String expFileName = lstRptProvider.getRptInfo().getRptWorkFilePath()+lstRptProvider.getRptInfo().getRptKey()
+ "." +jrInputInfo.getExportFileType().getFileTypeStr();
//14 导出文件
switch (jrInputInfo.getExportFileType()) {
case FILETYPE_EXCEL:
break;
case FILETYPE_EXCELXS:
break;
case FILETYPE_WORD:
break;
case FILETYPE_WORDXS:
break;
case FILETYPE_TEXT:
break;
case FILETYPE_HTML:
JasperExportManager.exportReportToHtmlFile( jrPrint,expFileName);
break;
case FILETYPE_JPG:
break;
case FILETYPE_PNG:
break;
default:
JasperExportManager.exportReportToPdfFile( jrPrint,expFileName);
break;
}
}
catch (JRException e) {
e.printStackTrace();
}
return true;
}
6 入口调用 方法
/**
* 本类是根据设置生成XML文件 方式 来生成报表
* @author Jason
*
*/
public class JRptServiceImpl implements JRptService {
private JRptListRptProvider lstRptProvider;
@Override
public Boolean showDynamicListReport(JRptInputInfo jrInputInfo) {
setRptInfoInitVariableValue(jrInputInfo);
//先取到报表所有设定信息
if (lstRptProvider.createListReportXMLFile(jrInputInfo))
return exportReportToFile(jrInputInfo);
return true;
}
后期再慢慢贴出生成XML文件过程