1. 程式人生 > >jasperreport 生成pdf下載功能

jasperreport 生成pdf下載功能

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JExcelApiExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;


/**
 * pdf匯出工具 
 * 支援多個PDF合併匯出
 * @author Administrator
 *
 */
public class PdfUtil {


/**
* 匯出pdf通用方法
* @param filePath
* @param filename
* @param list
* @param parameters
* @param response
* @throws Exception
*/
public void exportPdfReport(String filePath, String filename,
List<?> list, HashMap<String, Object> parameters,
HttpServletResponse response) throws Exception {
OutputStream outputStream = null;
if (list == null) {
list = new ArrayList<Object>();
}
if (parameters == null) {
parameters = new HashMap<String, Object>();
}
try {
outputStream = response.getOutputStream();
// 迴圈list資料
JRDataSource source = new JRBeanCollectionDataSource(list, false);
// JasperFillManager.
// 給PDF填充資料
JasperPrint jasperPrint = JasperFillManager.fillReport(filePath,
parameters, source);
JRExporter pdf = new JRPdfExporter();
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(filename.getBytes("GB18030"), "iso8859-1")
+ ".pdf");
pdf.setParameter(JExcelApiExporterParameter.CHARACTER_ENCODING,
"utf-8");
pdf.setParameter(JExcelApiExporterParameter.JASPER_PRINT,
jasperPrint);
pdf.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM,
outputStream);
pdf.exportReport();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception ex) {
throw ex;
}
}
}
}

/**
* 多個PDF合併匯出,解決ireport一次只能導一頁的問題
* @param jasperPrintList
* @param filename
* @param request
* @param response
* @throws Exception
*/
public void exportMultiPdfReport(List<JasperPrint> jasperPrintList, String filename,
HttpServletRequest request,HttpServletResponse response) throws Exception {
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment; filename="+ new String(filename.getBytes("GB18030"), "ISO-8859-1")+ ".pdf");
JRExporter jrExporter = new JRPdfExporter();
jrExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");
jrExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,jasperPrintList);
jrExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,outputStream);
jrExporter.exportReport();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception ex) {
throw ex;
}
}
}
}