我是 jasperreport 的新手。
- 谁能帮我从 ireport/modules/ext 复制 jar 文件的位置?
- 只有 ireport/modules/ext 中的 jar 文件就足够了吗?
- 如何与php连接。
谁能帮我解决这个问题
您必须将其复制到您的 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);