欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 中jasperReport实现动态列打印的实现代码

程序员文章站 2024-02-28 16:01:34
java 中jasperreport实现动态列打印的实现代码         以下代码中注释说明很清...

java 中jasperreport实现动态列打印的实现代码

        以下代码中注释说明很清楚,希望能帮助到大家,大家参考下。

示例代码:

public actionresult projectprint() { 
  string[] printvalue = null; 
  // 从页面中获得要查询的字段 
  string reqprintvalue = getrequest().getparameter("printvalue"); 
  // 没有选择则默认全打印 
  if (null == reqprintvalue || stringutils.isempty(reqprintvalue)) { 
   printvalue = new string[] { "pnumber", "pname", "pdepart", "pdecision", "pthrow", "plastmonth", "pfund", "ploan" }; 
  } else { 
   printvalue = reqprintvalue.split(","); 
  } 
  // 查询统计数据 
  list<object[]> projectlist = getentitymanager().queryprintprojectinfo(printvalue); 
 
  // 将数据转换为map对象,换化成map对象 
  list<map> reportdatalist = new arraylist<map>(); 
 
  for (int i = 0; i < projectlist.size(); i++) { 
   object[] personstr = projectlist.get(i); 
   map reportdata = new hashmap(); 
   for (int j = 0; j < personstr.length; j++) { 
    reportdata.put("field_" + j, string.valueof(personstr[j])); 
   } 
   reportdatalist.add(reportdata); 
  } 
 
  int columcount = 0;// 数据列 
  int fieldcount = 0;// 字段列数(因为pname比较长所以想让pname比其它的列长些,故设计这个变量) 
  int pnamecount = -1;// 记录下pname的序号 
  for (int i = 0; i < printvalue.length; i++) { 
   // pthrow下面有两列 
   if ("pthrow".equals(printvalue[i])) { 
    columcount = columcount + 2; 
    fieldcount = fieldcount + 2; 
    // ploan下面也有两列 
   } else if ("ploan".equals(printvalue[i])) { 
    columcount = columcount + 2; 
    fieldcount = fieldcount + 2; 
    // 故意让pname也占两列 
   } else if ("pname".equals(printvalue[i])) { 
    pnamecount = i;// 记录下pname的序号 
    columcount = columcount + 1; 
    fieldcount = fieldcount + 2; 
   } else { 
    // 其它的列都占一个单位 
    columcount = columcount + 1; 
    fieldcount = fieldcount + 1; 
   } 
  } 
 
  inputstream is = null; 
  try { 
   // 从资源文件中读取报表 
   is = this.getclass().getresourceasstream("/reports/project.jrxml"); 
   jasperdesign jasperdesign = (jasperdesign) jrxmlloader.load(is); 
 
   map stylemap = jasperdesign.getstylesmap(); 
   // column header 对应的样式 
   jrdesignstyle theaderstyle = (jrdesignstyle) stylemap.get("theader"); 
   // column detail 对应的样式 
   jrdesignstyle tbodystyle = (jrdesignstyle) stylemap.get("tboby"); 
   // pagefoot 对应的样式 
   jrdesignstyle tfootstyle = (jrdesignstyle) stylemap.get("tfoot"); 
 
   int _start_x_ = 20;// x轴的起始位置 
   int startx = _start_x_; // x轴的起始位置 
   // 单列的宽度 
   // 535是jaseprereport报表column最大的宽度 
   int columnwidth = 535 / fieldcount; 
   // 20,24,15是报表中已设置的,一定与之相同 
   final int columnheadbandheight = 20; 
   final int detailheight = 24; 
   final int pagefootheight = 15; 
 
   // 设置报表字段 
   for (int idx = 0; idx < columcount; idx++) { 
    jrdesignfield field = new jrdesignfield(); 
    field.setname("field_" + idx); 
    field.setvalueclass(java.lang.string.class); 
    jasperdesign.addfield(field); 
   } 
 
   jrdesignband columnheadband = (jrdesignband) jasperdesign.getcolumnheader(); 
   // 绘制表头 
   for (int idx = 0; idx < printvalue.length; idx++) { 
    if ("pnumber".equals(printvalue[idx])) { 
     jrdesignstatictext statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     statictext.setwidth(columnwidth); 
     statictext.sety(0); 
     statictext.setx(startx); 
     statictext.setheight(2 * columnheadbandheight); 
     statictext.settext("序号"); 
     columnheadband.addelement(statictext); 
     startx += columnwidth; 
    } else if ("pname".equals(printvalue[idx])) { 
     jrdesignstatictext statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     // 项目名称的宽度是其它的宽度的2倍 
     statictext.setwidth(columnwidth * 2); 
     statictext.sety(0); 
     statictext.setx(startx); 
     statictext.setheight(2 * columnheadbandheight); 
     statictext.settext("项目名称"); 
     columnheadband.addelement(statictext); 
     startx += columnwidth * 2; 
    } else if ("pdepart".equals(printvalue[idx])) { 
     jrdesignstatictext statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     statictext.setwidth(columnwidth); 
     statictext.sety(0); 
     statictext.setx(startx); 
     statictext.setheight(2 * columnheadbandheight); 
     statictext.settext("部门"); 
     columnheadband.addelement(statictext); 
     startx += columnwidth; 
    } else if ("pdecision".equals(printvalue[idx])) { 
     jrdesignstatictext statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     statictext.setwidth(columnwidth); 
     statictext.sety(0); 
     statictext.setx(startx); 
     statictext.setheight(2 * columnheadbandheight); 
     statictext.settext("已决策"); 
     columnheadband.addelement(statictext); 
     startx += columnwidth; 
    } else if ("pthrow".equals(printvalue[idx])) { 
     // 投审会下面有两列 
     jrdesignstatictext statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     statictext.setwidth(columnwidth * 2); 
     statictext.sety(0); 
     statictext.setx(startx); 
     statictext.setheight(columnheadbandheight); 
     statictext.settext("投审会"); 
     columnheadband.addelement(statictext); 
 
     statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     columnheadband.addelement(statictext); 
     statictext.setwidth(columnwidth); 
     statictext.sety(columnheadbandheight); 
     statictext.setx(startx); 
     statictext.setheight(columnheadbandheight); 
     statictext.settext("12月初"); 
 
     statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     columnheadband.addelement(statictext); 
     statictext.setwidth(columnwidth); 
     statictext.sety(columnheadbandheight); 
     statictext.setx(startx + columnwidth); 
     statictext.setheight(columnheadbandheight); 
     statictext.settext("12月中"); 
     columnheadband.addelement(statictext); 
     startx += 2 * columnwidth; 
    } else if ("plastmonth".equals(printvalue[idx])) { 
     // 投决会下面有一列 
     jrdesignstatictext statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     statictext.setwidth(columnwidth); 
     statictext.sety(0); 
     statictext.setx(startx); 
     statictext.setheight(columnheadbandheight); 
     statictext.settext("投决会"); 
     columnheadband.addelement(statictext); 
 
     statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     columnheadband.addelement(statictext); 
     statictext.setwidth(columnwidth); 
     statictext.sety(columnheadbandheight); 
     statictext.setx(startx); 
     statictext.setheight(columnheadbandheight); 
     statictext.settext("12月下"); 
     columnheadband.addelement(statictext); 
     startx += columnwidth; 
    } else if ("pfund".equals(printvalue[idx])) { 
     jrdesignstatictext statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     statictext.setwidth(columnwidth); 
     statictext.sety(0); 
     statictext.setx(startx); 
     statictext.setheight(2 * columnheadbandheight); 
     statictext.settext("基金投资额"); 
     columnheadband.addelement(statictext); 
     startx += columnwidth; 
    } else if ("ploan".equals(printvalue[idx])) { 
     // 投贷协同额下面有两列 
     jrdesignstatictext statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     statictext.setwidth(columnwidth * 2); 
     statictext.sety(0); 
     statictext.setx(startx); 
     statictext.setheight(columnheadbandheight); 
     statictext.settext("投贷协同额"); 
     columnheadband.addelement(statictext); 
 
     statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     columnheadband.addelement(statictext); 
     statictext.setwidth(columnwidth); 
     statictext.sety(columnheadbandheight); 
     statictext.setx(startx); 
     statictext.setheight(columnheadbandheight); 
     statictext.settext("金额"); 
 
     statictext = new jrdesignstatictext(); 
     statictext.setstyle(theaderstyle); 
     columnheadband.addelement(statictext); 
     statictext.setwidth(columnwidth); 
     statictext.sety(columnheadbandheight); 
     statictext.setx(startx + columnwidth); 
     statictext.setheight(columnheadbandheight); 
     statictext.settext("入库情况"); 
     columnheadband.addelement(statictext); 
     startx += 2 * columnwidth; 
    } 
   } 
 
   // 绘制detail部门 
   startx = _start_x_; 
   jrdesignband columndetailband = (jrdesignband) jasperdesign.getdetail(); 
   for (int idx = 0; idx < columcount; idx++) { 
    jrdesigntextfield textfield = new jrdesigntextfield(); 
    textfield.setstretchwithoverflow(true); 
    textfield.setx(startx); 
    textfield.sety(0); 
    if (pnamecount == idx) { 
     textfield.setwidth(2 * columnwidth); 
     startx += 2 * columnwidth; 
    } else { 
     textfield.setwidth(columnwidth); 
     startx += columnwidth; 
    } 
    textfield.setheight(detailheight); 
    textfield.setpositiontype(jrelement.position_type_float); 
    textfield.setstyle(tbodystyle); 
    textfield.setblankwhennull(true); 
    jrdesignexpression expression = new jrdesignexpression(); 
    expression.setvalueclass(java.lang.string.class); 
    expression.settext("$f{field_" + idx + "}"); 
    textfield.setexpression(expression); 
    columndetailband.addelement(textfield); 
   } 
 
   jrdesignband pagefootband = (jrdesignband) jasperdesign.getpagefooter(); 
   // 合计数据,本应统计的 
   list<object[]> pagecountlist = new arraylist<object[]>(); 
   object[] obj = new string[] { "合计", "15299", "", "", "67121", "92420", "155877", }; 
   pagecountlist.add(obj); 
   obj = new string[] { "", "", "", "xxx小计", "", "24473", "16470", }; 
   pagecountlist.add(obj); 
   obj = new string[] { "", "", "", "www小计", "", "7289", "1674", }; 
   pagecountlist.add(obj); 
   obj = new string[] { "", "", "", "zzz小计", "", "32700", "13000", }; 
   pagecountlist.add(obj); 
   obj = new string[] { "", "", "", "yyy小计", "", "12733", "120733", }; 
   pagecountlist.add(obj); 
   obj = new string[] { "", "", "", "aaa小计", "", "2225", "120733", }; 
   pagecountlist.add(obj); 
   obj = new string[] { "", "", "", "bbb小计", "", "3000", "0", }; 
   pagecountlist.add(obj); 
   int footwidth = 535 / 7; 
   for (int p = 0; p < pagecountlist.size(); p++) { 
    for (int k = 0; k < 7; k++) { 
     object[] ob = pagecountlist.get(p); 
     jrdesignstatictext statictext = new jrdesignstatictext(); 
     statictext.setstyle(tfootstyle); 
     statictext.setwidth(footwidth); 
     statictext.sety(pagefootheight * p); 
     statictext.setx(k * footwidth + _start_x_); 
     statictext.setheight(pagefootheight); 
     statictext.settext(string.valueof(ob[k])); 
     pagefootband.addelement(statictext); 
    } 
   } 
 
   // 编译报表 
   jasperreport jasperreport = jaspercompilemanager.compilereport(jasperdesign); 
   string type = this.getrequest().getparameter("type");//pdf格式 
   jasperutils.preparereport(jasperreport, type); 
   // 报表数据源 
   jrdatasource datasource = new jrbeancollectiondatasource(reportdatalist); 
   jasperprint jasperprint = jasperfillmanager.fillreport(jasperreport, null, datasource); 
   httpservletresponse response = this.getresponse(); 
   jasperutils.export(jasperprint, response, getrequest(), type); 
  } catch (exception e) { 
   e.printstacktrace(); 
  } 
 
  return null; 
 } 
 
public static void export(jasperprint jasperprint, httpservletresponse response, httpservletrequest request, 
   string type) throws ioexception { 
  if (excel_type.equals(type)) { 
   exportexcel(jasperprint, response, request); 
  } else if (pdf_type.equals(type)) { 
   exportpdf(jasperprint, response, request); 
  } else if (html_type.equals(type)) { 
   exporthtml(jasperprint, response, request); 
  } else { 
   exportprint(jasperprint, response, request); 
  } 
 
 } 
public static void exportexcel(jasperprint jasperprint, httpservletresponse response, httpservletrequest request) 
   throws ioexception { 
  response.setcontenttype("application/vnd.ms-excel"); 
  string filename = downloadhelper.encodefilename("未命名.xls", request); 
  response.setheader("content-disposition", "attachment;filename=" + filename); 
  servletoutputstream ouputstream = response.getoutputstream(); 
  jrxlsexporter exporter = new jrxlsexporter(); 
  exporter.setparameter(jrexporterparameter.jasper_print, jasperprint); 
  exporter.setparameter(jrexporterparameter.output_stream, ouputstream); 
  exporter.setparameter(jrxlsexporterparameter.is_remove_empty_space_between_rows, boolean.true); 
  exporter.setparameter(jrxlsexporterparameter.is_one_page_per_sheet, boolean.false); 
  exporter.setparameter(jrxlsexporterparameter.is_white_page_background, boolean.false); 
  try { 
   exporter.exportreport(); 
  } catch (jrexception e) { 
   e.printstacktrace(); 
  } 
  ouputstream.flush(); 
  ouputstream.close(); 
 } 
 
 public static void exportpdf(jasperprint jasperprint, httpservletresponse response, httpservletrequest request) 
   throws ioexception { 
  response.setcontenttype("application/pdf"); 
  string filename = downloadhelper.encodefilename("未命名.pdf", request); 
  response.setheader("content-disposition", "attachment;filename=" + filename); 
  servletoutputstream ouputstream = response.getoutputstream(); 
  try { 
   jasperexportmanager.exportreporttopdfstream(jasperprint, ouputstream); 
  } catch (jrexception e) { 
   e.printstacktrace(); 
  } 
  ouputstream.flush(); 
  ouputstream.close(); 
 } 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!