jasperreport + ireport 导出各种类型文件(word,excel,html,pdf,打印) .
2012-08-16 06:59
776 查看
在做ireport时,数据的来源有两种方法:
1)在使用ireport画表时,直接写好sql。java程序直接执行sql。
2)先要编写程序得出数据,如list,再把list传入。
对于第一种方法,优点很明显,不用写太多代码,但是不太灵活,如果要在得出的数据上做些处理,或一条sql无法获得想要的数据,这个时候,就要使用第二种方法。这也就是本文的重点所在。如何写出一个可以导出各种文件类型(word,excel,html,pdf,打印)的工具类,来简化代码,优化结构,并且展示它的使用。
1、工具类
package util.report;
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;
/*
* @author yaoguangyao
* jasperreport 导出各种类型文件
*/
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);
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";
}
2、测试使用
servlet 代码片段
...................................
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
List list = new ArrayList();
Student stu1 = new Student();
stu1.setId("21");
stu1.setName("小黄");
list.add(stu1);
Student stu2 = new Student();
stu2.setId("22");
stu2.setName("小明");
list.add(stu2);
java.io.InputStream is = this.getServletContext().getResourceAsStream("/report2.jasper");
//这里可以传入pdf,excel,word,html,print导出各种类型文件
JasperExportUtils.export(list, "word", is, req, resp);
}
1)在使用ireport画表时,直接写好sql。java程序直接执行sql。
2)先要编写程序得出数据,如list,再把list传入。
对于第一种方法,优点很明显,不用写太多代码,但是不太灵活,如果要在得出的数据上做些处理,或一条sql无法获得想要的数据,这个时候,就要使用第二种方法。这也就是本文的重点所在。如何写出一个可以导出各种文件类型(word,excel,html,pdf,打印)的工具类,来简化代码,优化结构,并且展示它的使用。
1、工具类
package util.report;
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;
/*
* @author yaoguangyao
* jasperreport 导出各种类型文件
*/
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);
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";
}
2、测试使用
servlet 代码片段
...................................
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
List list = new ArrayList();
Student stu1 = new Student();
stu1.setId("21");
stu1.setName("小黄");
list.add(stu1);
Student stu2 = new Student();
stu2.setId("22");
stu2.setName("小明");
list.add(stu2);
java.io.InputStream is = this.getServletContext().getResourceAsStream("/report2.jasper");
//这里可以传入pdf,excel,word,html,print导出各种类型文件
JasperExportUtils.export(list, "word", is, req, resp);
}
相关文章推荐
- iReport+JasperReport实现数据库记录导出到html、excel、pdf文档
- ireport导出各种格式(pdf,excel,word,html,print)
- ireport导出各种格式(pdf,excel,word,html,print)
- android Intent打开各种类型文件((PDF、word、excel、ppt、chm)
- android Intent打开各种类型文件((PDF、word、excel、ppt、chm)
- android Intent 默认打开打开各种类型文件((PDF、word、excel、ppt、chm)
- asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台
- android用于打开各种文件的intent,包括以下文件PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO
- android用于打开各种文件的intent,包括以下文件PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO
- android Intent打开各种类型文件(PDF、word、excel、ppt、chm)
- android Intent打开各种类型文件(PDF,PPT,WORD,EXCEL,CHM,H...
- ireport设计导出模板,Jasperreport导出PDF、HTML、Excel
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,html 格式
- android用于打开各种文件的intent,包括以下文件PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDE(代码待完善)
- Spring MVC+ Ireport 做统计报表,导出HTML、EXCEL等格式的文件,打印;
- android Intent打开各种类型文件((PDF、word、excel、ppt、chm)
- android Intent打开各种类型文件((PDF、word、excel、ppt、chm)
- 在java jasperreport 导出pdf excel html 文件到本地
- html导出table支持(excel,png,word,pdf<pdf不太行,中文不支持>)
- DataTable导出为word,excel,html,csv,pdf,.txt