J2EE使用ireport做导出PDF操作

首先,在lib中,加入一下jar:

    jasperreports-4.5.0.jar //
    groovy-all-1.7.5.jar
    iText-2.1.7.jar //导出PDF
    iTextAsian.jar //导出PDF,解决中文问题


使用ireport设计报表时,导出pdf,解决中文问题,需设置下面三个属性
Pdf font name :STSong-Light //简体宋体
Pdf Embedded : 勾选
Pdf Encoding : UniGB-UCS2-H (Chinese Simplified)

把生成的*.jasper文件复制到webroot下面的一个目录。

package net.bingosoft.ireport.servlet;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bingosoft.common.ObjectRegistry;
import net.bingosoft.common.config.Config;
import net.bingosoft.common.data.BaseDao;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRPdfExporterParameter;
import net.sf.jasperreports.engine.util.FileBufferedOutputStream;
import net.sf.jasperreports.engine.util.JRLoader;

/**
 * ireport导出类
 * @version 2012-02-29 
 *
 */
public class IreportServlet extends HttpServlet{

	private static final long serialVersionUID = 5306946463388538105L;
	
	private Map<String, Object> params;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		ServletContext servletContext = this.getServletConfig().getServletContext();
		//取文件路径标识
		String filePathFag = req.getParameter("FILE_PATH_FAG");
		//根据文件路径标识,从配置文件中,获取实际路径
		String filePath = Config.getString(filePathFag);
		File jsperFile = new File(servletContext.getRealPath(filePath));
		
		BaseDao baseDao = ObjectRegistry.getBaseDao();
		JasperPrint jasperPrint = null;
		params = new HashMap<String, Object>();
		try {
			//装载.jasper文件
			JasperReport jasperReport = (JasperReport)JRLoader.loadObject(jsperFile);
			//填充数据
			jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, baseDao.getDatasource().getConnection());
			
		} catch (JRException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		if(null != jasperPrint){
			FileBufferedOutputStream fbos = new FileBufferedOutputStream();
			JRPdfExporter exporter = new JRPdfExporter();
			exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, fbos);
			exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT, jasperPrint);
			
			try {
				exporter.exportReport();
				if(fbos.size()>0){
					resp.setContentType("application/pdf");
					resp.addHeader("Content-Disposition","attachment; filename=\test.pdf");
					resp.setContentLength(fbos.size());
					ServletOutputStream outStream = resp.getOutputStream();
					try{
						fbos.writeData(outStream);
						fbos.dispose();
						outStream.flush();
					}finally{
						if(null != outStream){
							outStream.close();
						}
					}
				}
			} catch (JRException e) {
				e.printStackTrace();
			}finally{
				if(null !=fbos){
					fbos.close();
					fbos.dispose();
				}
			}
		}
	}
}



上面是使用servlet。那么需要在web.xml中,配置自定义的servlet
  	<servlet>
  		<servlet-class>net.bingosoft.ireport.servlet.IreportServlet</servlet-class>
  		<servlet-name>ireportServlet</servlet-name>
  	</servlet>
  	
  	<servlet-mapping>
  		<servlet-name>ireportServlet</servlet-name>
  		<url-pattern>/ireportServlet</url-pattern>
  	</servlet-mapping>

导出效果图:
J2EE使用ireport做导出PDF操作

你可能感兴趣的:(IREPORT)