package com.paic.icore.acss.print.biz.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRParameter;
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.export.JRXmlExporter;
import net.sf.jasperreports.engine.export.JRXmlExporterParameter;
import net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.util.JRXmlUtils;
import org.w3c.dom.Document;
/**
*
* 报表输出为xml管理类
*
* @author EX-LICONGMING001
* <p> Date: 2014年3月11日 上午11:22:29 </p>
*/
public class JasperReportUtil {
/**
* 依报表文件名输出xml流
*
* @param reportPath
* @param reportName
* @param parameters
* @param doc
* @return
* @throws JRException
* @throws IOException
* @throws ParserConfigurationException
*/
public static byte[] createXmlByJrxmlName(String reportPath, String reportName, Map<String, Object> parameters,
Document doc) throws JRException, IOException, ParserConfigurationException {
JasperPrint jasperPrint = null;
ByteArrayOutputStream outs = new ByteArrayOutputStream();
// 张云鹏修改bytes = null; sonar异常Dead store to local variable
byte[] bytes = null;
try {
jasperPrint = fillReport(reportPath, reportName, parameters, doc);
JRXmlExporter jrXmlExporter = new JRXmlExporter();
jrXmlExporter.setParameter(JRXmlExporterParameter.JASPER_PRINT, jasperPrint);
jrXmlExporter.setParameter(JRXmlExporterParameter.CHARACTER_ENCODING, "utf-8");
jrXmlExporter.setParameter(JRXmlExporterParameter.OUTPUT_STREAM, outs);
jrXmlExporter.exportReport();
//JasperRunManager.runReportToPdf(inputStream, parameters, jrDataSource)
bytes = outs.toByteArray();
} finally {
outs.close();
}
return bytes;
}
/**
*
* 根据报表文件名输出PDF流
*
* @param reportPath
* @param reportName
* @param parameters
* @param doc
* @return
* @throws JRException
* @throws IOException
* @throws ParserConfigurationException
*/
public static byte[] createPdfByJrxmlName(String reportPath, String reportName, Map<String, Object> parameters,
Document doc) throws JRException, IOException, ParserConfigurationException {
JasperPrint jasperPrint = null;
ByteArrayOutputStream outs = new ByteArrayOutputStream();
byte[] bytes = null;
try {
jasperPrint = fillReport(reportPath, reportName, parameters, doc);
JRPdfExporter jrPdfExporter = new JRPdfExporter();
jrPdfExporter.setParameter(JRPdfExporterParameter.JASPER_PRINT, jasperPrint);
jrPdfExporter.setParameter(JRPdfExporterParameter.CHARACTER_ENCODING, "utf-8");
jrPdfExporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, outs);
jrPdfExporter.exportReport();
bytes = outs.toByteArray();
} finally {
outs.close();
}
return bytes;
}
/**
* 对报表填充数据
*
* @param reportPath-报表路径
* @param reportName-报表名
* @param parameters-参数
* @param doc-封装后的xml数据源
* @return
* @throws JRException
* @throws ParserConfigurationException
*/
private static JasperPrint fillReport(String reportPath, String reportName, Map<String, Object> parameters,
Document doc) throws JRException, ParserConfigurationException {
JasperPrint jasperPrint = null;
reportPath = reportPath.startsWith("/") ? reportPath.substring(1) : reportPath;
if (!reportPath.endsWith("/"))
reportPath += "/";
File sourceFile = new File(reportPath + reportName + ".jasper");
//若报表文件未检测到,则编译.jrxml
/*if (!sourceFile.exists()) {
JasperCompileManager.compileReportToFile(reportPath + reportName + ".jrxml", reportPath + reportName
+ ".jasper");
}*/
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(sourceFile);
// InputStream is = ClassLoader.getSystemResourceAsStream(reportPath + reportName + ".jasper");
// JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);
if (doc == null)
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
parameters.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, doc);
parameters.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "yyyy-MM-dd");
parameters.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, "#,##0.##");
parameters.put(JRXPathQueryExecuterFactory.XML_LOCALE, Locale.CHINESE);
parameters.put(JRParameter.REPORT_LOCALE, Locale.CHINA);
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters);
return jasperPrint;
}
public static void main(String[] args) {
//按页数生成xml数据源
//int count = 1;//页
String location = "D:\\Licm\\Doc\\test\\DTOtoXML2.xml";
//StringBuffer sb = new StringBuffer();
File file = new File(location);
/* sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<ply-voucher-dto-list>\n");
for (int i = 0; i < count; i++) {
sb.append(" <ply-voucher-dto>\n"
+ " <serialNo>1000000000023</serialNo>\n"
+ " <plyNo>20154785412236548152</plyNo>\n"
+ " <productName>农业保险</productName>\n"
+ " <number>1000000000000000000004</number>\n"
+ " <bbr>深圳市福田区玉龙新村</bbr>\n"
+ " <bbrCardId>431512488751243654</bbrCardId>\n"
+ " <bbrAddr>深圳市福田区玉龙新村姑城圾为中睛虽中中国同是上上夸夸国罪城国国蝇避避蝇避避避蝇避避避蝇避避避朋遥遥遥遥扔止</bbrAddr>\n"
+ " <productInfo>\n"
+ " <kind>农业保险</kind>\n"
+ " <unit>100000000</unit>\n"
+ " <count>100000000</count>\n"
+ " <unitMoney>100000000.00</unitMoney>\n"
+ " <amt>100000000.00</amt>\n"
+ " <pl>0.000015</pl>\n"
+ " <fl>0.000015</fl>\n"
+ " <prem>100000000.00</prem>\n"
+ " </productInfo>\n"
+ " <premMakeOf>\n"
+ " <zycz>20.1111</zycz>\n"
+ " <scz>20.1111</scz>\n"
+ " <dfcz>20.1111</dfcz>\n"
+ " <xcz>20.1111</xcz>\n"
+ " <nh>20.1111</nh>\n"
+ " <other>20.1111</other>\n"
+ " <zycz1>100000000.00</zycz1>\n"
+ " <scz1>100000000.00</scz1>\n"
+ " <dfcz1>100000000.00</dfcz1>\n"
+ " <xcz1>100000000.00</xcz1>\n"
+ " <nh1>100000000.00</nh1>\n"
+ " <other1>100000000.00</other1>\n"
+ " </premMakeOf>\n"
+ " <bdAddr>深圳市福田区玉龙新村姑城圾为中睛虽中中国同是上上夸夸国罪城国蝇避国蝇避避避避蝇避避避蝇避避避朋遥遥遥遥扔止</bdAddr>\n"
+ " <dayCount>365</dayCount>\n"
+ " <beginTime>2014-10-20 10:21:11</beginTime>\n"
+ " <endTime>2015-10-20 10:21:11</endTime>\n"
+ " <tbyd>在不中是国是国中,国中中中中蝇蝇蝇避遥遥遥扔止避避避避避申上占占占占不上一二地在在发民中是上一地地一主是是上了不上一二地在在发民中是上一地地一主是是上了不上一二地在在发民中是上一地地一主是是上了不上一二地在在发民中是上一地地一主是是上了不上一二地在在发民中是上一地地一主是是上了也民上上上上上止</tbyd>\n"
+ " <bxrAddr>深圳市福田区玉龙新村姑城圾为中睛虽中中国同是上上夸夸国罪城国蝇避避避蝇避避避蝇避避避朋遥遥遥遥扔止</bxrAddr>\n"
+ " <email>214578</email>\n" + " <tel>88881245</tel>\n" + " <tel2>88886214</tel2>\n"
+ " <currentlyTime>2014-10-20 10:21:11</currentlyTime>\n" + " </ply-voucher-dto>");
}
sb.append("\n</ply-voucher-dto-list>");
try {
OutputStream xmlOuts = new FileOutputStream(file);
xmlOuts.write(sb.toString().getBytes("UTF-8"));
xmlOuts.flush();
xmlOuts.close();
//return;
} catch (IOException e1) {
}*/
String reportPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
reportPath += "config/templet/argi/";
reportPath = reportPath.startsWith("/") ? reportPath.substring(1) : reportPath;
String jasperFileName = "TempletRptA1P_0001";//"TempletRpt0001"
Map<String, Object> parameters = new HashMap<String, Object>();
try {
File sourceFile = new File(reportPath + jasperFileName + ".jasper");
//若报表文件未检测到,则编译.jrxml
/*if (!sourceFile.exists()) {
JasperCompileManager.compileReportToFile(reportPath + jasperFileName + ".jrxml", reportPath
+ jasperFileName + ".jasper");
//System.out.println("jrxml编译成jasper成功!");
}*/
ByteArrayOutputStream tempOuts = new ByteArrayOutputStream();
InputStream is = new FileInputStream(file);
Document doc = JRXmlUtils.parse(is);
is.close();
parameters.put("title", "√");
parameters.put("SUBREPORT_DIR", reportPath);
parameters.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, doc);
parameters.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "yyyy-MM-dd");
parameters.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, "#,##0.##");
parameters.put(JRXPathQueryExecuterFactory.XML_LOCALE, Locale.CHINESE);
parameters.put(JRParameter.REPORT_LOCALE, Locale.CHINA);
OutputStream outs = new FileOutputStream(reportPath + jasperFileName + ".pdf");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(sourceFile);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters);
JRPdfExporter jrPdfExporter = new JRPdfExporter();
jrPdfExporter.setParameter(JRPdfExporterParameter.JASPER_PRINT, jasperPrint);
jrPdfExporter.setParameter(JRPdfExporterParameter.CHARACTER_ENCODING, "utf-8");
jrPdfExporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, tempOuts);
jrPdfExporter.exportReport();
//byte[] bytes = JasperRunManager.runReportToPdf(jasperReport, parameters);
outs.write(tempOuts.toByteArray());
outs.flush();
outs.close();
tempOuts.close();
} catch (JRException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
打印pdf生成
猜你喜欢
转载自licongming163.iteye.com/blog/2094972
今日推荐
周排行