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();
}
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
# jasperReport实现动态列打印
# Java
# 中jasperReport实现动态列打印的示例
# Java客户端调用.NET的WebService实例
# Java模拟计算机的整数乘积计算功能示例
# Java中线程的基本方法使用技巧
# 【Java IO流】字节流和字符流的实例讲解
# Java环境配置图文教程(推荐)
# Java编程接口回调一般用法代码解析
# 小计
# 希望能
# 月下
# 也有
# 未命名
# 如有
# 很清楚
# 与之
# 月中
# 想让
# 谢谢大家
# 转换为
# 较长
# 本应
# 月初
# 已设置
# 疑问请
# 比其
# 长些
# detail
相关文章:
免费网站制作模板下载,除了易企秀之外还有什么H5平台可以制作H5长页面,最好是免费的?
大连网站设计制作招聘信息,大连投诉网站有哪些?
零基础网站服务器架设实战:轻量应用与域名解析配置指南
再谈Python中的字符串与字符编码(推荐)
如何在香港免费服务器上快速搭建网站?
如何通过多用户协作模板快速搭建高效企业网站?
如何在Windows虚拟主机上快速搭建网站?
建站主机是否属于云主机类型?
我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?
网站制作专业公司有哪些,如何制作一个企业网站,建设网站的基本步骤有哪些?
C#怎么创建控制台应用 C# Console App项目创建方法
5种Android数据存储方式汇总
建站主机选哪家性价比最高?
代购小票制作网站有哪些,购物小票的简要说明?
如何高效利用200m空间完成建站?
建站之星如何通过成品分离优化网站效率?
如何选择CMS系统实现快速建站与SEO优化?
网站海报制作教学视频教程,有什么免费的高清可商用图片网站,用于海报设计?
电视网站制作tvbox接口,云海电视怎样自定义添加电视源?
如何选择靠谱的建站公司加盟品牌?
小程序网站制作需要准备什么资料,如何制作小程序?
安徽网站建设与外贸建站服务专业定制方案
javascript中的try catch异常捕获机制用法分析
网站制作说明怎么写,简述网页设计的流程并说明原因?
建站之家VIP精选网站模板与SEO优化教程整合指南
php条件判断怎么写_ifelse和switchcase的使用区别【对比】
成都网站制作公司哪家好,四川省职工服务网是做什么用?
建站主机选哪种环境更利于SEO优化?
网站建设设计制作营销公司南阳,如何策划设计和建设网站?
建站之星如何快速更换网站模板?
建站之星展会模板:智能建站与自助搭建高效解决方案
如何访问已购建站主机并解决登录问题?
建站之星安装后界面空白如何解决?
如何做静态网页,sublimetext3.0制作静态网页?
企业宣传片制作网站有哪些,传媒公司怎么找企业宣传片项目?
建站之星客服服务时间及联系方式如何?
香港服务器租用每月最低只需15元?
如何在Golang中实现微服务服务拆分_Golang微服务拆分与接口管理方法
电商网站制作公司有哪些,1688网是什么意思?
极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
定制建站平台哪家好?企业官网搭建与快速建站方案推荐
焦点电影公司作品,电影焦点结局是什么?
免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?
建站主机默认首页配置指南:核心功能与访问路径优化
韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐
魔毅自助建站系统:模板定制与SEO优化一键生成指南
如何在景安服务器上快速搭建个人网站?
,如何利用word制作宣传手册?
高性能网站服务器部署指南:稳定运行与安全配置优化方案
*请认真填写需求信息,我们会在24小时内与您取得联系。