我是jasperreport的新手。
任何人都可以帮我这个吗
答案 0 :(得分:2)
您必须将其复制到java_home目录中。即时通讯使用windows7所以在我的情况下它将C:\ Program Files \ Java \ jre7 \ lib \ ext。确保为java_home设置了类路径。 (我希望你知道怎么做)
是的,这将为您做到
完成下面给出的例子
include('../includes/javabridge.php');
java_set_file_encoding("UTF-8");
// Load the Jasper Report
$jrxml = new java("net.sf.jasperreports.engine.xml.JRXmlLoader");
$jasperTemplate = $jrxml->load(realpath("customer.jrxml"));
// Load the Query
$_Query = new java("net.sf.jasperreports.engine.design.JRDesignQuery");
$_Query->setText("select * FROM customers");
$jasperTemplate->setQuery($_Query);
// Compile the Report after including the Query
$compile = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
$report = $compile->compileReport($jasperTemplate);
// Connect to the database
$class = new JavaClass("java.lang.Class");
$class->forName("com.mysql.jdbc.Driver");
$mysqlDriver = new JavaClass("java.sql.DriverManager");
$conn = $mysqlDriver->getConnection("jdbc:mysql://localhost:3309/dbcustomer?user=root&password=");
// Now Fill the Report
$jasperFill = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
$params = new Java("java.util.HashMap");
$params->put("title", "Customer");
$jasperPrint = $jasperFill->fillReport($report, $params, $conn);
// Now Export the Report to desired format
$jasperExport = new java("net.sf.jasperreports.engine.JRExporter");
// Change here to generate report in pdf , excel,docx etc..
$Format = "pdf";
switch ($Format)
{
case 'xls':
$outputPath = realpath(".") . "\\" . "output.xls";
$jasperExport = new java("net.sf.jasperreports.engine.export.JRXlsExporter");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_ONE_PAGE_PER_SHEET, java("java.lang.Boolean")->TRUE);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_WHITE_PAGE_BACKGROUND, java("java.lang.Boolean")->FALSE);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, java("java.lang.Boolean")->TRUE);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=output.xls");
break;
case 'csv':
$outputPath = realpath(".") . "\\" . "output.csv";
$jasperExport = new java("net.sf.jasperreports.engine.export.JRCsvExporter");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.export.JRCsvExporterParameter")->FIELD_DELIMITER, ",");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.export.JRCsvExporterParameter")->RECORD_DELIMITER, "\n");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.export.JRCsvExporterParameter")->CHARACTER_ENCODING, "UTF-8");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=output.csv");
break;
case 'docx':
$outputPath = realpath(".") . "\\" . "output.docx";
$jasperExport = new java("net.sf.jasperreports.engine.export.ooxml.JRDocxExporter");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename=output.docx");
break;
case 'html':
$outputPath = realpath(".") . "\\" . "output.html";
$jasperExport = new java("net.sf.jasperreports.engine.export.JRHtmlExporter");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
break;
case 'pdf':
$outputPath = realpath(".") . "\\" . "output.pdf";
$jasperExport = new java("net.sf.jasperreports.engine.export.JRPdfExporter");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=output.pdf");
break;
case 'ods':
$outputPath = realpath(".") . "\\" . "output.ods";
$jasperExport = new java("net.sf.jasperreports.engine.export.oasis.JROdsExporter");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
header("Content-type: application/vnd.oasis.opendocument.spreadsheet");
header("Content-Disposition: attachment; filename=output.ods");
break;
case 'odt':
$outputPath = realpath(".") . "\\" . "output.odt";
$jasperExport = new java("net.sf.jasperreports.engine.export.oasis.JROdtExporter");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
header("Content-type: application/vnd.oasis.opendocument.text");
header("Content-Disposition: attachment; filename=output.odt");
break;
case 'txt':
$outputPath = realpath(".") . "\\" . "output.txt";
$jasperExport = new java("net.sf.jasperreports.engine.export.JRTextExporter");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.export.JRTextExporterParameter")->PAGE_WIDTH, 120);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.export.JRTextExporterParameter")->PAGE_HEIGHT, 60);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
header("Content-type: text/plain");
break;
case 'rtf':
$outputPath = realpath(".") . "\\" . "output.rtf";
$jasperExport = new java("net.sf.jasperreports.engine.export.JRRtfExporter");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
header("Content-type: application/rtf");
header("Content-Disposition: attachment; filename=output.rtf");
break;
case 'pptx':
$outputPath = realpath(".") . "\\" . "output.pptx";
$jasperExport = new java("net.sf.jasperreports.engine.export.ooxml.JRPptxExporter");
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$jasperExport->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
header("Content-type: aapplication/vnd.ms-powerpoint");
header("Content-Disposition: attachment; filename=output.pptx");
break;
}
$jasperExport->exportReport();
readfile($outputPath);
unlink($outputPath);