|
|
@@ -54,86 +54,9 @@ public class PrintServiceImpl implements PrintService {
|
|
|
@Override
|
|
|
public byte[] export(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
|
|
|
String exportFileType) {
|
|
|
- if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(reportName)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- // 报表路径为报表根路径REPORT_DIR + 当前账套用户名userName
|
|
|
- String reportDir = new StringBuilder(ReportConstants.REPORT_DIR).append(userName).append(File.separator)
|
|
|
- .toString();
|
|
|
-
|
|
|
- String jrxmlFilePath = new StringBuilder(reportDir).append("jrxml").append(File.separator).append(reportName)
|
|
|
- .append(".jrxml").toString();
|
|
|
- File jrxmlFile = new File(jrxmlFilePath);
|
|
|
- // 报表模板不存在
|
|
|
- if (!jrxmlFile.exists()) {
|
|
|
- logger.error("未发现模板文件: " + jrxmlFile.getPath());
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- String jasperFilePath = jrxmlFile.getPath().replace(".jrxml", ".jasper");
|
|
|
- // 报表模板编译后的文件
|
|
|
- File jasperFile = new File(jasperFilePath);
|
|
|
- try {
|
|
|
- // 报表模板未编译过
|
|
|
- if (!jasperFile.exists()) {
|
|
|
- logger.info("正在编译报表模板...");
|
|
|
- JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFile);
|
|
|
- JasperCompileManager.compileReportToFile(jasperDesign, jasperFilePath);
|
|
|
- } else {
|
|
|
- // 如果在编译之后,报表模板有更改过 ,重新编译
|
|
|
- if (jrxmlFile.lastModified() > jasperFile.lastModified()) {
|
|
|
- logger.info("正在重新编译报表模板...");
|
|
|
- JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFile);
|
|
|
- JasperCompileManager.compileReportToFile(jasperDesign, jasperFilePath);
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (JRException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- // 向报表模板传递参数:报表路径、where条件、其他参数
|
|
|
- Map<String, Object> parameters = new HashMap<>();
|
|
|
- parameters.put(ReportConstants.PARAMETER_REPORT_DIR, reportDir);
|
|
|
- if (!StringUtils.isEmpty(whereCondition)) {
|
|
|
- parameters.put(ReportConstants.PARAMETER_WHERE_CONDITION, whereCondition);
|
|
|
- }
|
|
|
- if (!CollectionUtils.isEmpty(otherParameters)) {
|
|
|
- parameters.putAll(otherParameters);
|
|
|
- }
|
|
|
-
|
|
|
- Connection connection = null;
|
|
|
- try {
|
|
|
- // 获取数据源
|
|
|
- DataSource dataSource = getDataSource(userName);
|
|
|
- if (dataSource == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- connection = dataSource.getConnection();
|
|
|
-
|
|
|
- // 从数据库获取数据填充报表
|
|
|
- JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
|
|
|
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
- if (exportReport(jasperPrint, exportFileType, outputStream)) {
|
|
|
- byte[] data = outputStream.toByteArray();
|
|
|
- outputStream.close();
|
|
|
- return data;
|
|
|
- }
|
|
|
- } catch (SQLException e) {
|
|
|
- logger.error("数据库连接错误!");
|
|
|
- } catch (JRException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- if (connection != null) {
|
|
|
- try {
|
|
|
- connection.close();
|
|
|
- } catch (SQLException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
+ Map<String, Object> result = print(userName, reportName, whereCondition, otherParameters, exportFileType, null);
|
|
|
+ if (!CollectionUtils.isEmpty(result)) {
|
|
|
+ return (byte[]) result.get("data");
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -141,6 +64,22 @@ public class PrintServiceImpl implements PrintService {
|
|
|
@Override
|
|
|
public Map<String, Object> preview(String userName, String reportName, String whereCondition,
|
|
|
Map<String, Object> otherParameters, Integer pageIndex) {
|
|
|
+ return print(userName, reportName, whereCondition, otherParameters, null, pageIndex);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 输出报表的数据
|
|
|
+ *
|
|
|
+ * @param userName
|
|
|
+ * @param reportName
|
|
|
+ * @param whereCondition
|
|
|
+ * @param otherParameters
|
|
|
+ * @param exportFileType
|
|
|
+ * @param pageIndex
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Map<String, Object> print(String userName, String reportName, String whereCondition,
|
|
|
+ Map<String, Object> otherParameters, String exportFileType, Integer pageIndex) {
|
|
|
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(reportName)) {
|
|
|
return null;
|
|
|
}
|
|
|
@@ -204,23 +143,41 @@ public class PrintServiceImpl implements PrintService {
|
|
|
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
|
|
- JRPdfExporter exporter = new JRPdfExporter();
|
|
|
- if (pageIndex != null && pageIndex >= 0 && pageIndex < jasperPrint.getPages().size()) {
|
|
|
+ // exportFileType导出文件的格式不为空,表示是导出,并非预览
|
|
|
+ if (!StringUtils.isEmpty(exportFileType)) {
|
|
|
+ boolean exportSucceeded = exportReport(jasperPrint, exportFileType, outputStream);
|
|
|
+ if (exportSucceeded) {
|
|
|
+ byte[] data = outputStream.toByteArray();
|
|
|
+ outputStream.close();
|
|
|
+ result.put("data", data);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 报表预览,则直接输出pdf,并且需要进行分页
|
|
|
+ else if (pageIndex != null) {
|
|
|
+ // 页码并非有效数值,重置为第一页
|
|
|
+ if (pageIndex < 0 || pageIndex >= jasperPrint.getPages().size()) {
|
|
|
+ pageIndex = 0;
|
|
|
+ }
|
|
|
+ JRPdfExporter exporter = new JRPdfExporter();
|
|
|
SimplePdfReportConfiguration configuration = new SimplePdfReportConfiguration();
|
|
|
configuration.setPageIndex(pageIndex);
|
|
|
exporter.setConfiguration(configuration);
|
|
|
- }
|
|
|
- ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
|
|
|
- exporter.setExporterInput(exporterInput);
|
|
|
- OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
|
|
|
- exporter.setExporterOutput(exporterOutput);
|
|
|
- exporter.exportReport();
|
|
|
+ ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
|
|
|
+ exporter.setExporterInput(exporterInput);
|
|
|
+ OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
|
|
|
+ exporter.setExporterOutput(exporterOutput);
|
|
|
+ exporter.exportReport();
|
|
|
|
|
|
- byte[] data = outputStream.toByteArray();
|
|
|
- outputStream.close();
|
|
|
- result.put("data", data);
|
|
|
- result.put("pageSize", jasperPrint.getPages().size());
|
|
|
- return result;
|
|
|
+ byte[] data = outputStream.toByteArray();
|
|
|
+ outputStream.close();
|
|
|
+ result.put("data", data);
|
|
|
+ result.put("pageSize", jasperPrint.getPages().size());
|
|
|
+ return result;
|
|
|
+ } else {
|
|
|
+ logger.error("exportFileType 和 pageSize 不能同时为空!");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
} catch (SQLException e) {
|
|
|
logger.error("数据库连接错误!");
|
|
|
} catch (JRException e) {
|
|
|
@@ -305,13 +262,12 @@ public class PrintServiceImpl implements PrintService {
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- // defaultDataSource.close();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 以不同的格式打印报表
|
|
|
+ * 以不同的格式导出报表
|
|
|
*
|
|
|
* @param jasperPrint
|
|
|
* @param exportFileType
|