package com.report;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRAbstractExporter;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
public class ReportUtil {
public static String jasperFilePath = "F:\\document\\report\\report1.jasper";
public static String pdfFilePath = "F:\\document\\report\\report1.pdf";
public static String docxFilePath = "F:\\document\\report\\report1.docx";
public static String xlsxFilePath = "F:\\document\\report\\report1.xlsx";
/**
* 生成文件pdf
*/
public static void addPdf() {
try {
// 第一步:装载jasper文件
File jasperFileName = new File(jasperFilePath);
// 第二步:设置参数值
/* 设置参数 */
HashMap<String, Object> params = new HashMap<String, Object>();// 建立参数表
params.put("name","我们的产品"); // 设置参数值
// 第三步:利用JasperRunManager生成PDF文件
JasperRunManager.runReportToPdfFile(jasperFileName.getPath(),
params, new JREmptyDataSource());
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 生成文件word
*/
public static void addDocx() {
File jasperFileName = new File(jasperFilePath);
Map<String, Object> params = new HashMap<String, Object>();
params.put("name", "我们的产品");
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperFileName.getPath(), params, new JREmptyDataSource());
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
JRAbstractExporter exporter = new JRDocxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
exporter.exportReport();
byte[] bytes = oStream.toByteArray();
if (bytes != null && bytes.length > 0) {
OutputStream ouputStream = new FileOutputStream(new File(
docxFilePath));
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
} else {
}
} catch (Exception ex) {
}
}
/**
* 生成文件excel
*/
public static void addXls() {
File jasperFileName = new File(jasperFilePath);
Map<String, Object> params = new HashMap<String, Object>();
params.put("name", "我们的产品");
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperFileName.getPath(), params, new JREmptyDataSource());
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
JRAbstractExporter exporter = new JRXlsxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
exporter.setParameter(
JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
Boolean.TRUE); // 删除记录最下面的空行
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.FALSE);// 删除多余的ColumnHeader
exporter.setParameter(
JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
Boolean.FALSE);// 显示边框
exporter.exportReport();
byte[] bytes = oStream.toByteArray();
if (bytes != null && bytes.length > 0) {
OutputStream ouputStream = new FileOutputStream(new File(
xlsxFilePath));
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
} else {
}
} catch (Exception ex) {
}
}
/**
* 测试
*/
public static void main(String[] args) {
// 生成pdf文件
ReportUtil.addPdf();
// 生成word文件
ReportUtil.addDocx();
// 生成excel文件
ReportUtil.addXls();
}
}
jasper模版文件+工程demo
4星 · 超过85%的资源 需积分: 18 2 浏览量
2013-05-13
17:51:46
上传
评论
收藏 11.18MB RAR 举报
thymetiantian
- 粉丝: 10
- 资源: 3
最新资源
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
- 基于单片机的风力发电机转速控制源码
- 基于C++实现的风力发电气动平衡监测系统+源代码+测量数据(高分课程设计)
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
- 基于stm32f103+空心杯电机+oled按键+运动算法
- 《CKA/CKAD应试指南/从docker到kubernetes 完全攻略》学习笔记 第1章docker基础(1.1-1.4)
- 基于python实现的水下压缩空气储能互补系统建模仿真与经济效益分析+源代码+论文
- 华中科技大学-自然语言处理实验,Bi-LSTM+CRF的中文分词框架,并且利用基于深度学习的方法进行中文命名实体识别++源码报告
- 基于动态罚函数的铁路车流分配与径路优化模型python源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈