Эх сурвалжийг харах

1、增加按分页导出按钮 2、增加参数配置,导出xls时忽略分页

guq 7 жил өмнө
parent
commit
705c873bc9

+ 51 - 0
report-common/src/main/java/com/uas/report/util/ZipUtils.java

@@ -215,4 +215,55 @@ public class ZipUtils {
 		zipFile.close();
 		logger.info("Unzip finished from... " + zipFilePath);
 	}
+
+	/**
+	 * 压缩文件
+	 *
+	 * @param srcFiles
+	 *            待压缩文件
+	 * @param zipFile
+	 *            压缩后保存的文件
+	 * @return
+	 */
+	public static boolean zip(String[] srcFiles, String zipFile) {
+		String[] fileNames = new String[srcFiles.length];
+		for (int i = 0; i < srcFiles.length; i++) {
+			fileNames[i] = getFileName(srcFiles[i]);
+		}
+		try {
+			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(zipFile));
+			ZipOutputStream zos = new ZipOutputStream(bos);
+			String entryName = null;
+			for (int i = 0; i < fileNames.length; i++) {
+				entryName = fileNames[i];
+				ZipEntry entry = new ZipEntry(entryName);
+				zos.putNextEntry(entry);
+				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFiles[i]));
+				byte[] b = new byte[1024];
+				while (bis.read(b, 0, 1024) != -1) {
+					zos.write(b, 0, 1024);
+				}
+				bis.close();
+				zos.closeEntry();
+			}
+			zos.flush();
+			zos.close();
+			return true;
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return false;
+	}
+
+	/**
+	 * 解析文件名
+	 *
+	 * @param filePath
+	 * @return
+	 */
+	public static String getFileName(String filePath) {
+		int location = filePath.lastIndexOf(File.separator);
+		String fileName = filePath.substring(location + 1);
+		return fileName;
+	}
 }

+ 15 - 4
report/src/main/java/com/uas/report/controller/PrintController.java

@@ -8,6 +8,7 @@ import com.uas.report.model.PrintType;
 import com.uas.report.service.FileService;
 import com.uas.report.service.PrintService;
 import com.uas.report.util.Assert;
+import com.uas.report.util.PdfUtils;
 import com.uas.report.util.ReportUtils;
 import com.uas.report.util.StringUtils;
 import net.sf.jasperreports.engine.JRException;
@@ -176,6 +177,9 @@ public class PrintController {
         String otherParameters = printParameter.getOtherParameters();
         String title = printParameter.getTitle();
         ReportUtils.checkParameters(userName, reportName);
+        //是否以分页的形式导出pdf文件
+        boolean split = "SPLITBYPAGE".equals(exportFileType) ? true : false;
+		exportFileType = split ? "PDF" : exportFileType;
 
         String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
 		ExportType exportType = StringUtils.isEmpty(exportFileType) ? ExportType.PDF
@@ -194,9 +198,16 @@ public class PrintController {
 			}
 			printService.export(userName, profile, reportName, whereCondition, otherParameters, exportType, file, null, true);
 		}
-
-		String exportFileName = (!StringUtils.isEmpty(title) ? title : reportName) + "." + exportType.getQualifier();
-		fileService.rangeDownload(file, exportFileName, request, response);
+		String exportFileName = (!StringUtils.isEmpty(title) ? title : reportName);
+		if (split) {
+			//pdf按页分文件
+			String[] files = PdfUtils.splitByPage(file.getAbsolutePath(), exportFileName);
+			exportFileName += ".zip";
+			fileService.rangeDownload(files, exportFileName, request, response);
+		} else {
+			exportFileName += "." + exportType.getQualifier();
+			fileService.rangeDownload(file, exportFileName, request, response);
+		}
 	}
 
     /**
@@ -227,7 +238,7 @@ public class PrintController {
         String pdfPath = reportName + "/"
                 + fileService.generateFileName(userName, profile, whereCondition, otherParameters, ExportType.PDF.getQualifier())
                 + "." + ExportType.PDF.getQualifier();
-        File file = new File(ReportUtils.getDocumentsDir(), pdfPath);
+		File file = new File(ReportUtils.getDocumentsDir(), pdfPath);
         // 指定flush为true(强制刷新pdf)
 		// 文件无效(不存在或过期),重新创建pdf文件
 		if ((flush != null && flush)

+ 15 - 0
report/src/main/java/com/uas/report/service/FileService.java

@@ -156,6 +156,21 @@ public interface FileService {
 	public void rangeDownload(File file, String fileName, HttpServletRequest request, HttpServletResponse response)
 			throws IOException;
 
+	/**
+	 * 下载多个文件(打包下载,支持断点续传)
+	 *
+	 * @param files
+	 *            要下载的文件
+	 * @param fileName
+	 *            下载后的文件名称
+	 * @param request
+	 * @param response
+	 * @throws IOException
+	 */
+	public void rangeDownload(String[] files, String fileName, HttpServletRequest request, HttpServletResponse response)
+			throws IOException;
+
+
 	/**
 	 * 下载文件(支持断点续传)
 	 *

+ 15 - 0
report/src/main/java/com/uas/report/service/impl/FileServiceImpl.java

@@ -304,6 +304,21 @@ public class FileServiceImpl implements FileService {
 			throws IOException {
 		rangeDownload(file, fileName, null, request, response);
 	}
+	/**
+	* @Description 多个文件打包下载
+	* @Param: [srcFiles, zipFile, request, response]
+	* @return: void
+	* @Author: guq
+	* @Date: 2018/9/21
+	*/
+	@Override
+	public void rangeDownload(String[] srcFiles, String zipFile, HttpServletRequest request, HttpServletResponse response) throws IOException {
+		boolean zip = ZipUtils.zip(srcFiles, zipFile);
+		if (zip) {
+			File zf = new File(zipFile);
+			rangeDownload(zf, zipFile, null, request, response);
+		}
+	}
 
 	@Override
 	public void rangeDownloadWithContentType(File file, String contentType, HttpServletRequest request, HttpServletResponse response)

+ 23 - 1
report/src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -79,7 +79,7 @@ public class PrintServiceImpl implements PrintService {
 		// 向报表模板传递参数:报表路径、where条件、其他参数
 		Map<String, Object> parameters = new HashMap<>();
         if (useFileVirtualizer) {
-            File virtualizerDir = new File(ReportUtils.getVirtualizerDir());
+			File virtualizerDir = new File(ReportUtils.getVirtualizerDir());
             if (!virtualizerDir.exists()) {
                 virtualizerDir.mkdirs();
             }
@@ -123,6 +123,13 @@ public class PrintServiceImpl implements PrintService {
 				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);
 			}
@@ -810,4 +817,19 @@ public class PrintServiceImpl implements PrintService {
 		}
 	}
 
+	private boolean ignorepage(String reportName, Connection connection, String dbtype) {
+		String sql = "select * from sysprintset where REPORTNAME='" + reportName + "' and nvl(IgnorePage,0)= -1";
+		int count = 0;
+		try {
+			count = getCount(connection, sql, dbtype);
+			if (count > 0) {
+				return true;
+			}
+		} catch (SQLException e) {
+			e.printStackTrace();
+			return  false;
+		}
+		return false;
+	}
+
 }

+ 56 - 0
report/src/main/java/com/uas/report/util/PdfUtils.java

@@ -0,0 +1,56 @@
+package com.uas.report.util;
+
+import com.lowagie.text.Document;
+import com.lowagie.text.pdf.PdfCopy;
+import com.lowagie.text.pdf.PdfImportedPage;
+import com.lowagie.text.pdf.PdfReader;
+
+import java.io.File;
+import java.io.FileOutputStream;
+
+/**
+ * 工具类
+ *
+ * @author guq
+ * @since 2018年09月21日 上午9:21:24
+ */
+public class PdfUtils {
+    
+    /**
+    * @Param:
+     * inFile 需要拆分的Pdf文件
+     * outName 需要输出文件模板名称
+    * @return: 输出拆分的文件名称
+    * @Author: guq
+    * @Date: 2018/9/21
+    */
+    public static String[] splitByPage(String inFile, String outName) {
+        File file = new File(inFile);
+        String[] ourFiles = null;
+        if (!file.exists() && !file.isFile())
+            throw new IllegalArgumentException("不存在该文件" + inFile);
+        try {
+            PdfReader reader = new PdfReader(inFile);
+            Integer n = reader.getNumberOfPages();
+            ourFiles = new String[n];
+            int i = 0;
+            while ( i < n ) {
+                String path = inFile.substring(0, inFile.lastIndexOf(File.separator) + 1);
+                String outFile = path + outName + "_"
+                        + String.format("%0" + n.toString().length() + "d", i + 1) + ".pdf";
+                ourFiles[i] = outFile;
+                Document document = new Document(reader.getPageSizeWithRotation(1));
+                PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile));
+                document.open();
+                PdfImportedPage page = writer.getImportedPage(reader, ++i);
+                writer.addPage(page);
+                document.close();
+                writer.close();
+            }
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+        }
+        return ourFiles;
+    }
+}

+ 1 - 1
report/src/main/webapp/WEB-INF/views/index.html

@@ -5,6 +5,6 @@
 <title>Report</title>
 </head>
 <body>
-	<p>Report Home</p>
+	<p>Report Home : version 201809210001</p>
 </body>
 </html>

+ 4 - 0
report/src/main/webapp/WEB-INF/views/preview.html

@@ -76,6 +76,10 @@
 				title="导出 Text">
 				<i class="fa fa-file-text-o fa-lg" aria-hidden="true"></i>
 			</button>
+			<button id="export_SPLITBYPAGE" hidden="true" class="toolbarButton"
+					title="导出 PDF(文件分页)">
+				<i class="fa fa-file-pdf-o fa-lg" aria-hidden="true"></i>
+			</button>
 		</div>
 	</div>