|
|
@@ -161,6 +161,126 @@ public class PrintServiceImpl implements PrintService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public int exportMulti(String userName, String profile, String reportName, String whereCondition,
|
|
|
+ String otherParameters, ExportType exportType, File exportFile, Integer pageIndex, boolean useFileVirtualizer)
|
|
|
+ throws JRException, IOException, DocumentException, SQLException {
|
|
|
+ DruidDataSource dataSource = MasterManager.getDataSource(userName, profile);
|
|
|
+ if (dataSource == null) {
|
|
|
+ throw new SQLException("获取数据源失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ if(exportFile.getParentFile() != null && !exportFile.getParentFile().exists()){
|
|
|
+ exportFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ List<String> paths =new ArrayList<>();
|
|
|
+ paths.add("炬神外箱标签");
|
|
|
+ paths.add("同为盘.盒标签");
|
|
|
+ Connection connection = null;
|
|
|
+ List<JasperPrint> printList = new ArrayList<JasperPrint>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ connection = dataSource.getConnection();
|
|
|
+ for(int i =0 ;i <paths.size(); i++){
|
|
|
+ String masterOfJrxml = getMasterOfJrxml(userName, paths.get(i));
|
|
|
+ String jrxmlFilePath = fileService.getJrxmlFilePath(masterOfJrxml, paths.get(i));
|
|
|
+
|
|
|
+ // 向报表模板传递参数:报表路径、where条件、其他参数
|
|
|
+ Map<String, Object> parameters = new HashMap<>();
|
|
|
+ if (useFileVirtualizer) {
|
|
|
+ File virtualizerDir = new File(ReportUtils.getVirtualizerDir());
|
|
|
+ if (!virtualizerDir.exists()) {
|
|
|
+ virtualizerDir.mkdirs();
|
|
|
+ }
|
|
|
+ parameters.put(JRParameter.REPORT_VIRTUALIZER, new JRFileVirtualizer(2, virtualizerDir.getAbsolutePath()));
|
|
|
+ }
|
|
|
+ parameters.put(ReportConstants.PARAMETER_REPORT_DIR, fileService.getMasterPath(masterOfJrxml));
|
|
|
+ if (!StringUtils.isEmpty(whereCondition)) {
|
|
|
+ parameters.put(ReportConstants.PARAMETER_WHERE_CONDITION, whereCondition);
|
|
|
+ }
|
|
|
+ // 不为空,并且去除空格后的长度大于2(不为{})
|
|
|
+ if (!StringUtils.isEmpty(otherParameters) && otherParameters.replace(" ", "").length() > 2) {
|
|
|
+ parameters.putAll(JSONObject.parseObject(otherParameters));
|
|
|
+ }
|
|
|
+ String jasperFilePath = maybeCompileJrxmlFile(jrxmlFilePath, false, otherParameters, connection, dataSource.getDbType());
|
|
|
+ if(StringUtils.isEmpty(jasperFilePath)){
|
|
|
+ throw new IllegalStateException("编译报表模板失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info("export fillReport...");
|
|
|
+ // 从数据库获取数据填充报表
|
|
|
+ JasperPrint jasperPrint;
|
|
|
+ boolean customCellStyle = false;
|
|
|
+ // 只导出数据
|
|
|
+ if (exportType == ExportType.XLS_DATA || exportType == ExportType.XLSX_DATA) {
|
|
|
+ // 需自定义单元格格式
|
|
|
+ customCellStyle = true;
|
|
|
+ JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
|
|
|
+ // 移除模板中多余元素
|
|
|
+ removeUnusedElements(jasperDesign);
|
|
|
+ JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
|
|
|
+ jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
|
|
|
+ } else if (exportType == ExportType.DOC) {
|
|
|
+ // 导出word时需要对行距等进行调整
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(
|
|
|
+ modifyLineSpacing(jrxmlFilePath).getBytes("UTF-8"));
|
|
|
+ JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
|
|
|
+ jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
|
|
|
+ inputStream.close();
|
|
|
+ } else if ((exportType == ExportType.XLS || exportType == ExportType.XLSX) &&
|
|
|
+ ignorepage(reportName, connection, dataSource.getDbType())) {
|
|
|
+ JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
|
|
|
+ //移除分页之间的间隔
|
|
|
+ jasperDesign.setIgnorePagination(true);
|
|
|
+ JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
|
|
|
+ jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
|
|
|
+ } else {
|
|
|
+ jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (exportType == ExportType.XLS || exportType == ExportType.XLS_DATA) {
|
|
|
+ exportReportToXls(jasperPrint, exportFile, customCellStyle, pageIndex);
|
|
|
+ } else if (exportType == ExportType.XLSX || exportType == ExportType.XLSX_DATA) {
|
|
|
+ exportReportToXlsx(jasperPrint, exportFile, customCellStyle, pageIndex);
|
|
|
+ } else if (exportType == ExportType.DOC) {
|
|
|
+ exportReportToDoc(jasperPrint, exportFile, pageIndex);
|
|
|
+ } else if (exportType == ExportType.TXT) {
|
|
|
+ exportReportToText(jasperPrint, exportFile, pageIndex);
|
|
|
+ } else {
|
|
|
+ //exportReportToPdf(jasperPrint, exportFile, pageIndex);
|
|
|
+ printList.add(jasperPrint);
|
|
|
+ }
|
|
|
+ logger.info("export fillReport done...");
|
|
|
+ }
|
|
|
+ JRPdfExporter exporter = new JRPdfExporter();
|
|
|
+ setPageConfiguration(exporter, printList.get(0), pageIndex);
|
|
|
+ logger.info("print done...");
|
|
|
+ ExporterInput exporterInput = new SimpleExporterInput(printList.get(0));
|
|
|
+ exporterInput.getItems().add(new SimpleExporterInputItem(printList.get(1)));
|
|
|
+ exporter.setExporterInput(exporterInput);
|
|
|
+ OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(exportFile);
|
|
|
+ exporter.setExporterOutput(exporterOutput);
|
|
|
+ exporter.exportReport();
|
|
|
+ return 0;
|
|
|
+ } catch (JRRuntimeException e) {
|
|
|
+ // 如果因为某个类未实现 Serializable 而无法反序列化,则不使用 REPORT_VIRTUALIZER
|
|
|
+ if (e.getCause() instanceof NotSerializableException) {
|
|
|
+ logger.error(e.getMessage() + " 取消 REPORT_VIRTUALIZER ,重新打印");
|
|
|
+ return export(userName, profile, reportName, whereCondition, otherParameters, exportType, exportFile, pageIndex, false);
|
|
|
+ }
|
|
|
+ throw e;
|
|
|
+ } finally {
|
|
|
+ if (connection != null) {
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 可能需要编译报表模板文件
|
|
|
*
|