ireport导出各种格式pdf,excel,word,html,print ireport print order

2010-10-06 19:14:45|分类: Ireport |标签:无 |字号大中小订阅

import java.io.IOException;

import java.io.InputStream;

import java.io.ObjectOutputStream;

import java.io.UnsupportedEncodingException;

import java.lang.reflect.Field;

import java.util.Collection;

import java.util.HashMap;

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;

public class JasperExportUtils

{

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

/*

* 如果导出的是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,

HttpServletRequest request, HttpServletResponse response) throws IOException, JRException {

/*

* 设置头信息

*/

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

String fileName = new String("未命名.xls".getBytes("GBK"), "ISO8859_1");

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);
ireport导出各种格式(pdf,excel,word,html,print) ireport print order

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,注意此处中文问题, 1)在ireport的classpath中加入iTextAsian.jar

* 2)在ireport画jrxml时,pdf font name :STSong-Light ,pdf encoding :

* UniGB-UCS2-H

*/

public static void exportPdf(JasperPrint jasperPrint,

HttpServletRequest request, HttpServletResponse response) throws IOException, JRException {

response.setContentType("application/pdf");

String fileName = new String("未命名.pdf".getBytes("GBK"), "ISO8859_1");

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

+ fileName);

ServletOutputStream ouputStream = response.getOutputStream();

JasperExportManager.exportReportToPdfStream(jasperPrint,

ouputStream);

ouputStream.flush();

ouputStream.close();

}

/**

* 导出html

*/

public static void exportHtml(JasperPrint jasperPrint,

HttpServletRequest request, HttpServletResponse response) throws IOException, JRException {

response.setContentType("text/html");

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

exporter.exportReport();

ouputStream.flush();

ouputStream.close();

}

/**

* 导出word

*/

public static void exportWord(JasperPrint jasperPrint,

HttpServletRequest request, HttpServletResponse response)

throws JRException, IOException {

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

String fileName = new String("未命名.doc".getBytes("GBK"), "ISO8859_1");

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

}

/**

* 打印

*/

public static void exportPrint(JasperPrint jasperPrint,

HttpServletResponse response, HttpServletRequest request)

throws IOException {

response.setContentType("application/octet-stream");

ServletOutputStream ouputStream = response.getOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(ouputStream);

oos.writeObject(jasperPrint);

oos.flush();

oos.close();

ouputStream.flush();

ouputStream.close();

}

/**

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

*

* @param datas

* 数据

* @param type

* 文件类型

* @param is

* jasper文件的来源

* @param request

* @param response

*/

public static void export(Collection datas, String type, InputStream is,

HttpServletRequest request, HttpServletResponse response) {

try {

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, request, response);

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

exportPdf(jasperPrint, request, response);

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

exportHtml(jasperPrint, request, response);

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

exportWord(jasperPrint, request, response);

}

} catch (Exception e) {

e.printStackTrace();

}

}

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

}

评论这张

   转发至微博

  

爱华网本文地址 » http://www.413yy.cn/a/25101011/67584.html

更多阅读

pdf转换器怎么用 pdf转换成word

?  很多pdf转换成word转换器在实现pdf和word两者之间的转换的时候,对于pdf文件之内的图片、文字样式、超链接等一系列的内容的识别能力都并不完美。一般来说,转换能力较差的转换器软件转换之后,在word文件之内经常会出现各种排版上

怎样用word2013编辑pdf文件 word编辑pdf文件

怎样用word2013编辑pdf文件——简介一般来说,pdf是不容易编辑的,很多软件都只提供pdf文件的浏览操作,但是想编辑pdf文件,又不想下载其他软件,或者是没时间精力学习pdf编辑软件,有什么捷径呢?有,就是直接利用word来编辑pdf文件,详见下面图文

pdf格式转换器使用方法 pdf格式转换器

pdf格式转换器使用方法——简介PDF格式转换成WORD、excel、PPT等,可以使用工具进行转换。根据不同的PDF制作的格式,可以使用不同的工具进行转换pdf格式转换器使用方法——工具/原料AnyBizSoft PDF Converter V2.5 Solid Converter P

手机通讯录直接导出到电脑为EXCEL格式 苹果通讯录导出excel

手机通讯录直接导出到电脑为EXCEL格式——简介现在有手机的人越来越多了,同时还有不少时尚达人以及部分因为业务的需要的达人们,人手好几个手机都很正常。现在又是一个智能机的时代,而手机通讯录里的电话号码也越来越多,如果把他们保存

声明:《ireport导出各种格式pdf,excel,word,html,print ireport print order》为网友我酷毙了吗分享!如侵犯到您的合法权益请联系我们删除