|
|
@@ -31,12 +31,14 @@ import net.sf.jasperreports.engine.JasperExportManager;
|
|
|
import net.sf.jasperreports.engine.JasperFillManager;
|
|
|
import net.sf.jasperreports.engine.JasperPrint;
|
|
|
import net.sf.jasperreports.engine.design.JasperDesign;
|
|
|
+import net.sf.jasperreports.engine.export.JRPdfExporter;
|
|
|
import net.sf.jasperreports.engine.export.JRXlsExporter;
|
|
|
import net.sf.jasperreports.engine.xml.JRXmlLoader;
|
|
|
import net.sf.jasperreports.export.ExporterInput;
|
|
|
import net.sf.jasperreports.export.OutputStreamExporterOutput;
|
|
|
import net.sf.jasperreports.export.SimpleExporterInput;
|
|
|
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
|
|
|
+import net.sf.jasperreports.export.SimplePdfReportConfiguration;
|
|
|
|
|
|
/**
|
|
|
* 报表打印
|
|
|
@@ -50,7 +52,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
private static Logger logger = Logger.getLogger(PrintService.class);
|
|
|
|
|
|
@Override
|
|
|
- public byte[] print(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
|
|
|
+ public byte[] export(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
|
|
|
String exportFileType) {
|
|
|
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(reportName)) {
|
|
|
return null;
|
|
|
@@ -107,6 +109,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
if (dataSource == null) {
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
connection = dataSource.getConnection();
|
|
|
|
|
|
// 从数据库获取数据填充报表
|
|
|
@@ -135,6 +138,107 @@ public class PrintServiceImpl implements PrintService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> preview(String userName, String reportName, String whereCondition,
|
|
|
+ Map<String, Object> otherParameters, Integer pageIndex) {
|
|
|
+ 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();
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+
|
|
|
+ // 从数据库获取数据填充报表
|
|
|
+ JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+
|
|
|
+ JRPdfExporter exporter = new JRPdfExporter();
|
|
|
+ if (pageIndex != null && pageIndex >= 0 && pageIndex < jasperPrint.getPages().size()) {
|
|
|
+ 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();
|
|
|
+
|
|
|
+ byte[] data = outputStream.toByteArray();
|
|
|
+ outputStream.close();
|
|
|
+ result.put("data", data);
|
|
|
+ result.put("pageSize", jasperPrint.getPages().size());
|
|
|
+ return result;
|
|
|
+ } 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 根据当前账套用户名,从主数据库master表获取账套数据库配置信息,作为报表模板的数据源
|
|
|
*
|
|
|
@@ -212,7 +316,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @param jasperPrint
|
|
|
* @param exportFileType
|
|
|
* @param outputStream
|
|
|
- * @return 报表是否成功打印
|
|
|
+ * @return 报表是否成功导出
|
|
|
*/
|
|
|
private boolean exportReport(JasperPrint jasperPrint, String exportFileType, OutputStream outputStream) {
|
|
|
try {
|
|
|
@@ -221,7 +325,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
} else if (exportFileType.equals("xls")) {
|
|
|
exportReportToXls(jasperPrint, outputStream);
|
|
|
} else {
|
|
|
- logger.error("不支持" + exportFileType + "格式!");
|
|
|
+ logger.error("不支持导出为 " + exportFileType + "格式!");
|
|
|
return false;
|
|
|
}
|
|
|
} catch (JRException e) {
|
|
|
@@ -232,7 +336,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 以pdf的格式打印报表
|
|
|
+ * 以pdf的格式导出报表
|
|
|
*
|
|
|
* @param jasperPrint
|
|
|
* @param outputStream
|
|
|
@@ -243,7 +347,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 以xls的格式打印报表
|
|
|
+ * 以xls的格式导出报表
|
|
|
*
|
|
|
* @param jasperPrint
|
|
|
* @param outputStream
|