1.ireport使用说明
ireport 是一款方便实用的报表编辑工具,你可以使用这个工具通过简单的拖拉的形式设计自己需要的报表页面,同时它可以独立连接数据库。如果你想学习如何使用ireport工具你 可以仔细阅读这篇文章。
2.安装包及相关环境下载
由于ireport工具年代已经比较久远,而且不是国内开发的软件,已经被收购所以下载建议不要找官网,可以找百度网盘等别人下载好的链接地址,或者头铁的去官网试一试:
- 官网下载地址: ireport官网下载地址
- 百度网盘下载地址 百度网盘下载地址
提取码:j8e4; - 下载完成后,无法直接打开,应为软甲比较老旧不支持jdk1.7以上的jvm环境,所以还需要下载一个jdk1.7,(电脑中有jdk1.7或以下版本的忽略即可)JDK 1.7安装包(Windows 64位)可以在官网上下载,也可以在百度网盘上下载 jdk-7u80-windows-x64.exe。
链接:jdk1.7下载地址 提取码:lj89 - 以上环境相关安装完成即可打开;
3.快速入门上手
-
安装完成后打开软件,首先需要设置数据库连接(这里以Mysql为例,其他类似); -
配置完数据库后点击文件=》New,根据报表情况选择对应的模板
根据指示填写相关信息后点击完成 进入设计页面,页面右侧会显示属性栏和组件面板栏 左边展示报表设计格式 其中Detail1是用来存放列表信息的可以存在多个,其余的部分只能存在一个,每一个Detail1都可以编辑一个对应的SQL来获取报表数据。每个模块生成报表后展示如下 通过使用组件面板的工具设计好报表样式后,我们可以通过工具直接编译预览 首先在最左侧的操作栏中选中你的项目右键找到属性 在属性中找到When No Data这个设置为如图所示, 点击编译后可以进行预览 其中Detail1中的信息需要通过SQL来展示,我们需要点击图中按钮位置来进行SQL编写及报表页面中相关报表的变量内容设置。 最后一定要注意在你设计完成之后还需要对内容中的字体进行pdf设置 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0duYl91w-1652084941143)(https://img-在这里插入代码片 blog.csdnimg.cn/3105c0d45dda4efcb0e4c09fe0e674ef.png)]
在属性上找到最后面的PDF相关设置,然后将值设置为如果所示的值。然后通过编译预览功能就可以查看报表。
如何使用代码生成报表
参考以下代码
package com.ntocc.util.ireport;
import com.ntocc.config.RDSConnection;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Map;
@Component
public class IReportUtils {
private static Connection connection;
private static Logger logger = LoggerFactory.getLogger(IReportUtils.class);
public static Connection getConnection(){
String driver = RDSConnection.driver;
String url = RDSConnection.url;
String user = RDSConnection.user;
String pass = RDSConnection.pass;
try {
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user, pass);
return con;
}catch(Exception e){
e. printStackTrace();
logger.error(e.getMessage());
}
return null;
}
public static Connection getConn(){
if (connection == null ){
connection = getConnection();
}
return connection;
}
public static void exportToPdf(String jasperFile, Map<String,Object> paramMap, String defaultFileName, HttpServletRequest request,
HttpServletResponse response) throws IOException, JRException {
if (StringUtils.isEmpty(defaultFileName)){
defaultFileName = "export.pdf";
}else {
defaultFileName = defaultFileName + ".pdf";
}
String fileName = new String(defaultFileName.getBytes("GBK"), "ISO8859_1");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFile,paramMap, getConn());
ServletOutputStream ouputStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, ouputStream);
ouputStream.flush();
ouputStream.close();
}
public static void exportToDoc(String jasperFile, Map<String,Object> paramMap, String defaultFileName, HttpServletRequest request,
HttpServletResponse response) throws IOException, JRException {
if (StringUtils.isEmpty(defaultFileName)){
defaultFileName = "export.doc";
}else {
defaultFileName = defaultFileName + ".doc";
}
String fileName = new String(defaultFileName.getBytes("GBK"), "ISO8859_1");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFile,paramMap, getConn());
JRExporter exporter = new JRRtfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
response.getOutputStream());
exporter.exportReport();
}
public static void exportToHtml(String jasperFile, Map<String,Object> paramMap, HttpServletRequest request,
HttpServletResponse response) throws IOException, JRException {
response.setContentType("text/html");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFile,paramMap, getConn());
ServletOutputStream ouputStream = response.getOutputStream();
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
Boolean.FALSE);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
String imageDIR =request.getSession().getServletContext().getRealPath("/");
exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, imageDIR);
String imageURI = request.getContextPath() + "/";
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imageURI);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
exporter.exportReport();
ouputStream.flush();
ouputStream.close();
}
public static void exportToExcel(String jasperFile, Map<String,Object> paramMap, String defaultFileName, HttpServletRequest request,
HttpServletResponse response) throws IOException, JRException {
if (StringUtils.isEmpty(defaultFileName)){
defaultFileName = "export.xls";
}else {
defaultFileName = defaultFileName + ".xls";
}
String fileName = new String(defaultFileName.getBytes("GBK"), "ISO8859_1");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFile,paramMap, getConn());
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);
exporter.exportReport();
ouputStream.flush();
ouputStream.close();
}
public static void exportToPdf(String jasperFile, Map<String,Object> paramMap, String defaultFileName, HttpServletRequest request,
HttpServletResponse response, JRDataSource dataSource) throws IOException, JRException {
if (StringUtils.isEmpty(defaultFileName)){
defaultFileName = "export.pdf";
}else {
defaultFileName = defaultFileName + ".pdf";
}
String fileName = new String(defaultFileName.getBytes("GBK"), "ISO8859_1");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
JasperPrint jasperPrint = new JasperPrintWithDataSource(paramMap, jasperFile, dataSource).getJasperPrint();
ServletOutputStream ouputStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, ouputStream);
ouputStream.flush();
ouputStream.close();
}
public static void exportToDoc(String jasperFile, Map<String,Object> paramMap, String defaultFileName, HttpServletRequest request,
HttpServletResponse response, JRDataSource dataSource) throws IOException, JRException {
if (StringUtils.isEmpty(defaultFileName)){
defaultFileName = "export.doc";
}else {
defaultFileName = defaultFileName + ".doc";
}
String fileName = new String(defaultFileName.getBytes("GBK"), "ISO8859_1");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
JasperPrint jasperPrint = new JasperPrintWithDataSource(paramMap, jasperFile, dataSource).getJasperPrint();
JRExporter exporter = new JRRtfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
response.getOutputStream());
exporter.exportReport();
}
public static void exportToHtml(String jasperFile, Map<String,Object> paramMap, HttpServletRequest request,
HttpServletResponse response, JRDataSource dataSource) throws IOException, JRException {
response.setContentType("text/html");
JasperPrint jasperPrint = new JasperPrintWithDataSource(paramMap, jasperFile, dataSource).getJasperPrint();
ServletOutputStream ouputStream = response.getOutputStream();
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
Boolean.FALSE);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
String imageDIR =request.getSession().getServletContext().getRealPath("/");
exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, imageDIR);
String imageURI = request.getContextPath() + "/";
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imageURI);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
exporter.exportReport();
ouputStream.flush();
ouputStream.close();
}
public static void exportToExcel(String jasperFile, Map<String,Object> paramMap, String defaultFileName, HttpServletRequest request,
HttpServletResponse response, JRDataSource dataSource) throws IOException, JRException {
if (StringUtils.isEmpty(defaultFileName)){
defaultFileName = "export.xls";
}else {
defaultFileName = defaultFileName + ".xls";
}
String fileName = new String(defaultFileName.getBytes("GBK"), "ISO8859_1");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
JasperPrint jasperPrint = new JasperPrintWithDataSource(paramMap, jasperFile, dataSource).getJasperPrint();
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);
exporter.exportReport();
ouputStream.flush();
ouputStream.close();
}
}
|