Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,htm

简介: Ireport 报表导出 Poi + ireport 导出pdf, doc ,excel ,html 格式 下面是报表导出工具类reportExportUtils 需要导出以上格式的报表 只需要调用本工具类的exportmain 方法即可。

 

Ireport 报表导出 Poi + ireport 导出pdf, doc ,excel ,html 格式

下面是报表导出工具类reportExportUtils 需要导出以上格式的报表 只需要调用本工具类的exportmain 方法即可。

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.lang.reflect.Field;

import java.util.Collection;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRDataSource;

import net.sf.jasperreports.engine.JRException;

import net.sf.jasperreports.engine.JRExporter;

import net.sf.jasperreports.engine.JRExporterParameter;

import net.sf.jasperreports.engine.JasperExportManager;

import net.sf.jasperreports.engine.JasperFillManager;

import net.sf.jasperreports.engine.JasperPrint;

import net.sf.jasperreports.engine.JasperReport;

import net.sf.jasperreports.engine.base.JRBaseReport;

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import net.sf.jasperreports.engine.export.JRHtmlExporter;

import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;

import net.sf.jasperreports.engine.export.JRRtfExporter;

import net.sf.jasperreports.engine.export.JRXlsExporter;

import net.sf.jasperreports.engine.export.JRXlsExporterParameter;

import net.sf.jasperreports.engine.util.JRLoader;

import org.apache.commons.lang.StringUtils;

import org.apache.log4j.Logger;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("unchecked")

public class JasperUtils {

private static Logger logger = Logger.getLogger(JasperUtils.class);

public static final String PRINT_TYPE = "print";

public static final String PDF_TYPE = "pdf";

public static final String EXCEL_TYPE = "excel";

public static final String HTML_TYPE = "html";

public static final String WORD_TYPE = "word";

public static void prepareReport(JasperReport jasperReport, String type) {

logger

.debug("The method======= prepareReport() start.......................");

/*

 * 如果导出的是excel,则需要去掉周围的margin

 */

if ("excel".equals(type))

try {

Field margin = JRBaseReport.class

.getDeclaredField("leftMargin");

margin.setAccessible(true);

margin.setInt(jasperReport, 0);

margin = JRBaseReport.class.getDeclaredField("topMargin");

margin.setAccessible(true);

margin.setInt(jasperReport, 0);

margin = JRBaseReport.class.getDeclaredField("bottomMargin");

margin.setAccessible(true);

margin.setInt(jasperReport, 0);

Field pageHeight = JRBaseReport.class

.getDeclaredField("pageHeight");

pageHeight.setAccessible(true);

pageHeight.setInt(jasperReport, 2147483647);

} catch (Exception exception) {

}

}

/**

 * 导出excel

 */

public static void exportExcel(JasperPrint jasperPrint,

String defaultFilename, HttpServletRequest request,

HttpServletResponse response) throws IOException, JRException {

logger

.debug("执行导出excel   The method======= exportExcel() start.......................");

/*

 * 设置头信息

 */

response.setContentType("application/vnd.ms-excel");

String defaultname = null;

if (defaultFilename.trim() != null && defaultFilename != null) {

defaultname = defaultFilename + ".xls";

} else {

defaultname = "export.xls";

}

String fileName = new String(defaultname.getBytes("gbk"), "utf-8");

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);

exporter.exportReport();

ouputStream.flush();

ouputStream.close();

}

/**

 * 导出pdf,注意此处中文问题,

 * 

 * 这里应该详细说:主要在ireport里变下就行了。看图

 * 

 * 1)在ireport的classpath中加入iTextAsian.jar 2)在ireport画jrxml时,看ireport最左边有个属性栏。

 * 

 * 下边的设置就在点字段的属性后出现。 pdf font name :STSong-Light ,pdf encoding :UniGB-UCS2-H

 */

private static void exportPdf(JasperPrint jasperPrint,

String defaultFilename, HttpServletRequest request,

HttpServletResponse response) throws IOException, JRException {

response.setContentType("application/pdf");

String defaultname = null;

if (defaultFilename.trim() != null && defaultFilename != null) {

defaultname = defaultFilename + ".pdf";

} else {

defaultname = "export.pdf";

}

String fileName = new String(defaultname.getBytes("GBK"), "utf-8");

response.setHeader("Content-disposition", "attachment; filename="

+ fileName);

ServletOutputStream ouputStream = response.getOutputStream();

JasperExportManager.exportReportToPdfStream(jasperPrint, ouputStream);

ouputStream.flush();

ouputStream.close();

}

/**

 * 导出html

 * 

 * @param pageIndexStr

 */

private static void exportHtml(JasperPrint jasperPrint,

String defaultFilename, HttpServletRequest request,

HttpServletResponse response, String pageIndexStr)

throws IOException, JRException {

response.setContentType("text/html");

ServletOutputStream ouputStream = response.getOutputStream();

JRHtmlExporter exporter = new JRHtmlExporter();

// 得到当前页码

Integer pageIndex = getPageIndex(jasperPrint, request, pageIndexStr);

// 如果页码不为空 则设置分页页码

if (pageIndex != null) {

exporter.setParameter(JRExporterParameter.PAGE_INDEX, pageIndex);

}

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);

exporter.exportReport();

ouputStream.flush();

ouputStream.close();

}

/**

 * 

 * description:通过传进来的pageIndexStr 得到当前页码 Date 2013-1-18 上午10:49:38

 * 

 * @param @param jasperPrint

 * @param @param request

 * @param @param pageIndexStr

 * @param @return

 * @return Integer

 */

private static Integer getPageIndex(JasperPrint jasperPrint,

HttpServletRequest request, String pageIndexStr) {

// 如果pageIndexStr为空或空字符串则返回null

if (pageIndexStr == null || StringUtils.isBlank(pageIndexStr)) {

return null;

}

Integer pageIndex = 0;

int lastPageIndex = 0;

// 得到最后一页的 页码

if (jasperPrint.getPages() != null) {

lastPageIndex = jasperPrint.getPages().size() - 1;

}

// 如果字符串==lastPage 则反lastPageIndex的值赋给pageIndex 并返回pageIndex

if ("lastPage".equals(pageIndexStr)) {

pageIndex = lastPageIndex;

return pageIndex;

}

try {

pageIndex = Integer.parseInt(pageIndexStr);

// 从ireport 传来的PageIndex是从1开始,而JRExporterParameter.PAGE_INDEX是从0开始的

if (pageIndex > 0) {

pageIndex = pageIndex - 1;

}

} catch (Exception e) {

e.printStackTrace();

}

if (pageIndex < 0) {

pageIndex = 0;

}

if (pageIndex > lastPageIndex) {

pageIndex = lastPageIndex;

}

return pageIndex;

}

/**

 * 导出word

 */

private static void exportWord(JasperPrint jasperPrint,

String defaultFilename, HttpServletRequest request,

HttpServletResponse response) throws JRException, IOException {

response.setContentType("application/msword;charset=utf-8");

String defaultname = null;

if (defaultFilename.trim() != null && defaultFilename != null) {

defaultname = defaultFilename + ".doc";

} else {

defaultname = "export.doc";

}

String fileName = new String(defaultname.getBytes("GBK"), "utf-8");

response.setHeader("Content-disposition", "attachment; filename="

+ fileName);

JRExporter exporter = new JRRtfExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response

.getOutputStream());

exporter.exportReport();

}

/**

 * 按照类型导出不同格式文件

 * 

 * @param datas

 *            数据

 * @param type

 *            文件类型

 * @param is

 *            jasper文件的来源

 * @param request

 * @param response

 * @param defaultFilename默认的导出文件的名称

 */

private static void export(Collection datas, String type,

String defaultFilename, InputStream is, HttpServletRequest request,

HttpServletResponse response, String pageIndexStr) {

logger

.debug("导出判断     The method======= export() start.......................");

try {

logger.info("is==" + is);

JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);

prepareReport(jasperReport, type);

JRDataSource ds = new JRBeanCollectionDataSource(datas, false);

Map parameters = new HashMap();

JasperPrint jasperPrint = JasperFillManager.fillReport(

jasperReport, parameters, ds);

if (EXCEL_TYPE.equals(type)) {

exportExcel(jasperPrint, defaultFilename, request, response);

} else if (PDF_TYPE.equals(type)) {

exportPdf(jasperPrint, defaultFilename, request, response);

} else if (HTML_TYPE.equals(type)) {

exportHtml(jasperPrint, defaultFilename, request, response,

pageIndexStr);

} else if (WORD_TYPE.equals(type)) {

exportWord(jasperPrint, defaultFilename, request, response);

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

 * 导出入口 (导出所有的数据 )

 * 

 * @param exportType

 *            导出文件的类型

 * @param jaspername

 *            jasper文件的名字 如: xx.jasper

 * @param lists

 *            导出的数据

 * @param request

 * @param response

 * @param defaultFilename默认的导出文件的名称

 */

public static void exportmain(String exportType, String jaspername,

List lists, String defaultFilename) {

exportmain(exportType, jaspername, lists, defaultFilename, null);

}

/**

 * 导出入口 (分页显示 导出传入的pageIndex 页的数据)

 * 

 * @param exportType

 *            导出文件的类型

 * @param jaspername

 *            jasper文件的名字 如: xx.jasper

 * @param lists

 *            导出的数据

 * @param request

 * @param response

 * @param defaultFilename默认的导出文件的名称

 * @param pageIndex

 *            需要导出数据的页码 当pageIndex = null时导出所有数据

 */

@SuppressWarnings("deprecation")

public static void exportmain(String exportType, String jaspername,

List lists, String defaultFilename, String pageIndexStr) {

logger

.debug("进入导出    The method======= exportmain() start.......................");

ActionContext ct = ActionContext.getContext();

HttpServletRequest request = (HttpServletRequest) ct

.get(ServletActionContext.HTTP_REQUEST);

HttpServletResponse response = ServletActionContext.getResponse();

String filenurl = request.getRealPath("/view/situation/jasper/"

+ jaspername);

File file = new File(filenurl);

InputStream is = null;

try {

is = new FileInputStream(file);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

export(lists, exportType, defaultFilename, is, request, response,

pageIndexStr);

}

}

 

目录
相关文章
|
2月前
|
Python
办公自动化-Python如何提取Word标题并保存到Excel中?
办公自动化-Python如何提取Word标题并保存到Excel中?
55 2
|
2月前
aspose实现word,excel等文件预览
aspose实现word,excel等文件预览
|
8天前
|
Web App开发 前端开发 安全
2024年新一代WebOffice内嵌网页组件,Web网页在线编辑Word/Excel/PPT
WebOffice控件面临兼容性、用户体验和维护难题。随着浏览器更新,依赖插件的技术不再适用,如Chrome不再支持NPAPI和PPAPI。产品普遍不支持多版本Office并存,定制能力弱,升级复杂。猿大师办公助手提供了解决方案,它兼容多种浏览器,包括最新版和国产浏览器,不依赖插件,支持文档对比,具有丰富的功能和接口,兼容多种Office版本,允许源码级定制,提供终身技术支持,并实现静默在线升级。适用于多种行业和操作系统。
48 4
|
26天前
VBA如何用Excel数据批量生成Word文档
VBA|用Excel数据批量生成并修改用模板创建的Word文档
|
1月前
|
Java API Spring
集成EasyPoi(一个基于POI的Excel导入导出工具)到Spring Boot项目中
集成EasyPoi(一个基于POI的Excel导入导出工具)到Spring Boot项目中
77 1
|
1月前
使用LabVIEW打开默认应用程序中的文档(PDF,Word,Excel,Html)
使用LabVIEW的&quot;Open a Document on Disk.vi&quot;,存于&lt;LabVIEW&gt;\vi.lib\Platform\browser.llb,可让默认应用打开硬盘文档。此VI仅基础打开功能,高级控制推荐LabVIEW Report Generation Toolkit或ActiveX。注意:避免版本升级问题,最好将VI复制到vi.lib外的目录。
|
23天前
|
easyexcel Java API
Apache POI与easyExcel:Excel文件导入导出的技术深度分析
Apache POI与easyExcel:Excel文件导入导出的技术深度分析
|
2月前
|
Java Apache
Java将word、excel文件转成pdf文件
【5月更文挑战第26天】Java将word、excel文件转成pdf文件
410 1
|
2月前
|
Java Apache 索引
POI操作大全(动态合并单元格,为单元格生成一个自定义的数据显示格式,自定义公式计算结果生成,读取excel,word文件在生成图片,word指定位置生成图片)
POI操作大全(动态合并单元格,为单元格生成一个自定义的数据显示格式,自定义公式计算结果生成,读取excel,word文件在生成图片,word指定位置生成图片)
222 0
|
23天前
|
IDE Java 编译器
使用Java分割PDF文件
使用Java分割PDF文件
25 1