jasper生成pdf实例

/**
需要导入一下jar包
commons-beanutils-1.9.0.jar
commons-collections-3.2.1.jar
commons-digester-2.1.jar
commons-logging-1.1.1.jar
groovy-all-2.0.1.jar
iText-2.1.7.jar
iText-2.1.7.js2.jar
iTextAsian.jar
jasperreports-6.1.0.jar
jasperreports-fonts-6.1.0.jar
jxl-2.6.10.jar
mysql-connector-java-5.1.7-bin.jar
*/
package jasper;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;

public class PdfJasperTest {
	public static void main(String[] args) throws ClassNotFoundException, SQLException, JRException, IOException {
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/exam";
		String username = "root";
		String password = "huxc";
		
		Connection conn = DriverManager.getConnection(url, username, password);
		
		FileInputStream is = new FileInputStream("D:\\work\\jasper\\jasper\\report.jasper");
		
		Map<String ,Object> map = new HashMap<String ,Object>();
		map.put("year", "2015");
		
		byte[] bytes = JasperRunManager.runReportToPdf(is, map, conn);
		
		FileOutputStream out = new FileOutputStream("D:\\work\\jasper\\jasper\\report.pdf");
		
		out.write(bytes,0,bytes.length);
		
		conn.close();
		is.close();
		out.close();
	}
}

你可能感兴趣的:(jasper生成pdf实例)