Browse Source

区别于下载纯数据excel的选项,增加下载完整excel的功能;预览最大页数限制改为200页;提高预览稳定性

sunyj 9 years ago
parent
commit
d73cabb3dd

+ 46 - 26
src/main/java/com/uas/report/controller/PrintController.java

@@ -85,9 +85,14 @@ public class PrintController {
 		}
 		// 下载pdf、纯数据excel
 		else if (printType.equals(ReportConstants.PDF_PRINT_TYPE)) {
-			export(userName, profile, reportName, whereCondition, otherParameters, "pdf", response);
-		} else if (printType.equals(ReportConstants.EXCEL_PRINT_TYPE)) {
-			export(userName, profile, reportName, whereCondition, otherParameters, "xls_with_only_data", response);
+			export(userName, profile, reportName, whereCondition, otherParameters, ReportConstants.PDF_FILE_TYPE,
+					response);
+		}
+		// 该下载接口供UAS系统使用,应其要求,EXCEL_PRINT_TYPE只能为EXCEL(该值意思本应为下载全部数据的excel),
+		// 导致与EXCEL_WITH_ONLY_DATA_FILE_TYPE(下载纯数据的excel)命名不相匹配
+		else if (printType.equals(ReportConstants.EXCEL_PRINT_TYPE)) {
+			export(userName, profile, reportName, whereCondition, otherParameters,
+					ReportConstants.EXCEL_WITH_ONLY_DATA_FILE_TYPE, response);
 		} else {
 			throw new ReportException("printType不合法");
 		}
@@ -119,15 +124,15 @@ public class PrintController {
 
 		logger.info("开始导出报表:" + reportName);
 		if (StringUtils.isEmpty(exportFileType)) {
-			exportFileType = "pdf";
+			exportFileType = ReportConstants.PDF_FILE_TYPE;
 		}
 
 		byte[] data = null;
 
-		String filePath = ReportConstants.GENERATED_FILES_PATH + reportName + "/"
-				+ fileService.generateFileName(userName, profile, reportName, whereCondition, otherParameters);
-		if (exportFileType.equals("xls_with_only_data")) {
-			filePath += ".xls";
+		String filePath = ReportConstants.GENERATED_FILES_PATH + reportName + "/" + fileService
+				.generateFileName(userName, profile, reportName, whereCondition, otherParameters, exportFileType);
+		if (exportFileType.equals(ReportConstants.EXCEL_WITH_ONLY_DATA_FILE_TYPE)) {
+			filePath += "." + ReportConstants.EXCEL_FILE_TYPE;
 		} else {
 			filePath += "." + exportFileType;
 		}
@@ -160,8 +165,8 @@ public class PrintController {
 
 		try {
 			String exportFileName = reportName;
-			if (exportFileType.equals("xls_with_only_data")) {
-				exportFileName += ".xls";
+			if (exportFileType.equals(ReportConstants.EXCEL_WITH_ONLY_DATA_FILE_TYPE)) {
+				exportFileName += "." + ReportConstants.EXCEL_FILE_TYPE;
 			} else {
 				exportFileName += "." + exportFileType;
 			}
@@ -206,7 +211,9 @@ public class PrintController {
 
 		// 相对路径,返回给前端
 		String pdfPath = ReportConstants.GENERATED_FILES_PATH + reportName + "/"
-				+ fileService.generateFileName(userName, profile, reportName, whereCondition, otherParameters) + ".pdf";
+				+ fileService.generateFileName(userName, profile, reportName, whereCondition, otherParameters,
+						ReportConstants.PDF_FILE_TYPE)
+				+ "." + ReportConstants.PDF_FILE_TYPE;
 		final File file = new File(PathUtils.getAppPath() + pdfPath);
 		if (pageIndex == null || pageIndex == 1) {
 			// 文件无效(不存在或过期),重新创建pdf文件
@@ -215,7 +222,9 @@ public class PrintController {
 				// 需要生成第一页(可能页数过多,提示用户不支持预览打印),再后台开线程生成总的pdf
 				// 先生成第一页pdf
 				Integer pageSize = printService.createPdfFile(userName, profile, reportName, whereCondition,
-						otherParameters, file.getPath().replace(".pdf", "_1.pdf"), 1);
+						otherParameters, file.getPath().replace("." + ReportConstants.PDF_FILE_TYPE,
+								"_1." + ReportConstants.PDF_FILE_TYPE),
+						1);
 				result.put("pageSize", pageSize);
 				// 再开线程生成后面页的pdf、总的pdf(即未分页的pdf)、纯数据excel
 				new Thread(new Runnable() {
@@ -232,10 +241,14 @@ public class PrintController {
 			}
 		} else {
 			// 文件无效(不存在或过期),重新创建pdf文件
-			if (!fileService.isFileValid(file.getPath().replace(".pdf", "_" + pageIndex + ".pdf"),
+			if (!fileService.isFileValid(
+					file.getPath().replace("." + ReportConstants.PDF_FILE_TYPE,
+							"_" + pageIndex + "." + ReportConstants.PDF_FILE_TYPE),
 					fileService.getJrxmlFilePath(userName, reportName))) {
 				printService.createPdfFile(userName, profile, reportName, whereCondition, otherParameters,
-						file.getPath().replace(".pdf", "_" + pageIndex + ".pdf"), pageIndex);
+						file.getPath().replace("." + ReportConstants.PDF_FILE_TYPE,
+								"_" + pageIndex + "." + ReportConstants.PDF_FILE_TYPE),
+						pageIndex);
 			}
 		}
 		result.put("pdfPath", pdfPath);
@@ -257,33 +270,40 @@ public class PrintController {
 	 * @param otherParameters
 	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
 	 *            JSON格式,数据为键值对
-	 * @param pdfOrXls
-	 *            pdf或者xls
+	 * @param fileType
+	 *            文件格式,pdf、xls、xls_with_only_data
 	 * @return 文件的信息
 	 */
 	@RequestMapping(value = "/getGeneratedPdfOrXlsInformation")
 	@ResponseBody
 	public Map<String, Object> getGeneratedPdfOrXlsInformation(String userName, String profile, String reportName,
-			String whereCondition, String otherParameters, String pdfOrXls) {
+			String whereCondition, String otherParameters, String fileType) {
 		Map<String, Object> result = new HashMap<>();
-		if (StringUtils.isEmpty(pdfOrXls)) {
-			pdfOrXls = "pdf";
+		if (StringUtils.isEmpty(fileType)) {
+			fileType = ReportConstants.PDF_FILE_TYPE;
 		}
-		String filePath = ReportConstants.GENERATED_FILES_PATH + reportName + "/"
-				+ fileService.generateFileName(userName, profile, reportName, whereCondition, otherParameters);
-		File file = new File(PathUtils.getAppPath() + filePath + "." + pdfOrXls);
-		result.put("file", file.getPath());
-		if (pdfOrXls.equals("pdf")) {
+		String filePath = ReportConstants.GENERATED_FILES_PATH + reportName + "/" + fileService.generateFileName(
+				userName, profile, reportName, whereCondition, otherParameters, ReportConstants.PDF_FILE_TYPE);
+		File file = null;
+		if (fileType.equals(ReportConstants.PDF_FILE_TYPE)) {
+			file = new File(PathUtils.getAppPath() + filePath + "." + ReportConstants.PDF_FILE_TYPE);
+			result.put("valid",
+					fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(userName, reportName)));
+			result.put("size", file.length());
+		} else if (fileType.equals(ReportConstants.EXCEL_FILE_TYPE)) {
+			file = new File(PathUtils.getAppPath() + filePath + "." + ReportConstants.EXCEL_FILE_TYPE);
 			result.put("valid",
 					fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(userName, reportName)));
 			result.put("size", file.length());
-		} else if (pdfOrXls.equals("xls")) {
+		} else if (fileType.equals(ReportConstants.EXCEL_WITH_ONLY_DATA_FILE_TYPE)) {
+			file = new File(PathUtils.getAppPath() + filePath + "." + ReportConstants.EXCEL_FILE_TYPE);
 			result.put("valid",
 					fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(userName, reportName)));
 			result.put("size", file.length());
 		} else {
-			throw new ReportException("pdfOrXls只能为pdf或xls");
+			throw new ReportException("fileType只能为pdf、xls、xls_with_only_data");
 		}
+		result.put("file", file.getPath());
 		return result;
 	}
 

+ 4 - 2
src/main/java/com/uas/report/service/FileService.java

@@ -109,7 +109,7 @@ public interface FileService {
 	public boolean isFileValid(String filePath, String jrxmlFilePath);
 
 	/**
-	 * 根据报表名、账套名等生成文件名
+	 * 根据报表名、账套名等生成文件名(文件名:reportName + hashCode)
 	 * 
 	 * @param userName
 	 *            不为null;当前账套用户名
@@ -122,10 +122,12 @@ public interface FileService {
 	 * @param otherParameters
 	 *            可为null;若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,
 	 *            报表某些字段的值取决于这些参数; JSON格式,数据为键值对
+	 * @param exportFileType
+	 *            导出为文件时,文件的格式(pdf、xls、xls_with_only_data等)
 	 * @return 生成的文件名(不含后缀名)
 	 */
 	public String generateFileName(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters);
+			String otherParameters, String exportFileType);
 
 	/**
 	 * 获取给定的pdf文件的页数

+ 4 - 1
src/main/java/com/uas/report/service/impl/FileServiceImpl.java

@@ -246,7 +246,7 @@ public class FileServiceImpl implements FileService {
 
 	@Override
 	public String generateFileName(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters) {
+			String otherParameters, String exportFileType) {
 		if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(reportName)) {
 			return null;
 		}
@@ -260,6 +260,9 @@ public class FileServiceImpl implements FileService {
 		if (!StringUtils.isEmpty(otherParameters)) {
 			stringBuilder.append(otherParameters);
 		}
+		if (!StringUtils.isEmpty(exportFileType)) {
+			stringBuilder.append(exportFileType);
+		}
 		// 文件名:reportName + hashCode
 		return reportName + "_" + stringBuilder.toString().hashCode();
 	}

+ 31 - 4
src/main/java/com/uas/report/util/ReportConstants.java

@@ -9,25 +9,52 @@ package com.uas.report.util;
 public class ReportConstants {
 
 	/**
-	 * 报表参数 - WHERE_CONDITION,where字句(需含where)
+	 * 报表参数WHERE_CONDITION,where字句(需含where)
 	 */
 	public static final String PARAMETER_WHERE_CONDITION = "WHERE_CONDITION";
 
 	/**
-	 * 报表参数 - REPORT_DIR,报表的路径
+	 * 报表参数 REPORT_DIR,报表的路径
 	 */
 	public static final String PARAMETER_REPORT_DIR = "REPORT_DIR";
 
 	/**
-	 * 请求类型,判断是预览、打印、下载pdf还是下载纯数据excel
+	 * 请求类型:预览
 	 */
 	public static final String PREVIEW_PRINT_TYPE = "PREVIEW";
+
+	/**
+	 * 请求类型:打印
+	 */
 	public static final String PRINT_PRINT_TYPE = "PRINT";
+
+	/**
+	 * 请求类型:下载pdf
+	 */
 	public static final String PDF_PRINT_TYPE = "PDF";
+
+	/**
+	 * 请求类型:下载纯数据excel
+	 */
 	public static final String EXCEL_PRINT_TYPE = "EXCEL";
 
 	/**
-	 * 生成的pdf、excel等文件的路径
+	 * 导出文件的格式:pdf
+	 */
+	public static final String PDF_FILE_TYPE = "pdf";
+
+	/**
+	 * 导出文件的格式:excel
+	 */
+	public static final String EXCEL_FILE_TYPE = "xls";
+
+	/**
+	 * 导出文件的格式:纯数据excel
+	 */
+	public static final String EXCEL_WITH_ONLY_DATA_FILE_TYPE = "xls_with_only_data";
+
+	/**
+	 * 生成的pdf、excel等文件的相对路径
 	 */
 	public static final String GENERATED_FILES_PATH = "resources/generate/";
 }

+ 3 - 0
src/main/webapp/WEB-INF/views/preview2.html

@@ -51,6 +51,9 @@
 			<button id="downloadPdf" class="toolbarButton" title="下载PDF">
 				<i class="fa fa-file-pdf-o fa-lg" aria-hidden="true"></i>
 			</button>
+			<button id="downloadExcel" class="toolbarButton" title="下载Excel">
+				<i class="fa fa-file-excel-o fa-lg" aria-hidden="true"></i>
+			</button>
 			<button id="downloadExcelWithOnlyData" class="toolbarButton"
 				title="下载Excel(仅数据)">
 				<i class="fa fa-file-excel-o fa-lg" aria-hidden="true"></i>

+ 36 - 10
src/main/webapp/resources/js/preview2/app.js

@@ -9,7 +9,7 @@ var hiddenFrame = document.getElementById("hiddenFrame");
 var spinner;
 
 // 能打印的最大页数(页数超过,需要先下载pdf,再打印)
-var PRINT_MAX_PAGE_SIZE = 500;
+var PRINT_MAX_PAGE_SIZE = 200;
 var ALERT_FILE_TOO_LARGE = "pdf超过" + PRINT_MAX_PAGE_SIZE
 		+ "页,建议先下载到本地,再进行查看或打印";
 var pdfDoc;
@@ -109,6 +109,7 @@ $("#downloadPdf").click(
 			console.log(new Date().format()
 					+ " ---- subscribed wholePdfGeneratedSignal");
 			$.subscribe("wholePdfGeneratedSignal", downloadPdf);
+			showLoading();
 			waitWholePdfGenerated();
 		});
 
@@ -122,14 +123,23 @@ function downloadPdf() {
 	$.unsubscribe("wholePdfGeneratedSignal", downloadPdf);
 	console.log(new Date().format() + " ---- start download...");
 	window.location = downloadUrl("pdf");
+	hiddenFrame.src = wholePdfPath;
 }
 
+// 下载纯数据excel
+$("#downloadExcel").click(function() {
+	if (!pdfDoc) {
+		return;
+	}
+	window.open(downloadUrl("xls"));
+});
+
 // 下载纯数据excel
 $("#downloadExcelWithOnlyData").click(function() {
 	if (!pdfDoc) {
 		return;
 	}
-	window.location = downloadUrl("xls_with_only_data");
+	window.open(downloadUrl("xls_with_only_data"));
 });
 
 // 键盘左右键进行翻页
@@ -176,8 +186,25 @@ function printPdf() {
 		alert(ALERT_FILE_TOO_LARGE);
 		return;
 	}
+	if (!pdfDoc) {
+		return;
+	}
+	console.log(new Date().format()
+			+ " ---- subscribed wholePdfGeneratedSignal");
+	$.subscribe("wholePdfGeneratedSignal", print);
+	showLoading();
+	waitWholePdfGenerated();
+}
+
+/**
+ * 取消订阅信号,打印
+ */
+function print() {
+	console.log(new Date().format()
+			+ " ---- received and unsubscribe wholePdfGeneratedSignal");
+	$.unsubscribe("wholePdfGeneratedSignal", print);
 	console.log(new Date().format() + " ---- start print...");
-	hiddenFrame.contentWindow.print();
+	setTimeout("hiddenFrame.contentWindow.print();hideLoading()", 1000);
 }
 
 /**
@@ -209,6 +236,7 @@ function loadPagedPdf(pagedPdfPath, ifPreloadWholePdf) {
 												+ " ---- subscribed wholePdfGeneratedSignal");
 								$.subscribe("wholePdfGeneratedSignal",
 										loadWholePdf);
+								showLoading();
 								waitWholePdfGenerated();
 							}
 						}
@@ -246,7 +274,6 @@ function loadData(page) {
 				document.title = getParameter("reportName");
 				// 加载第一页文档,并且预加载整个文档
 				loadPagedPdf(pagedPdfPath, true);
-				showLoading();
 				// $.when(getDocument()).done(loadWholePdf);
 				// console.log(renderTask._internalRenderTask.running);
 			} else {
@@ -292,7 +319,6 @@ function loadWholePdf() {
  * 每隔一定时间查询文件状态,直到文件有效
  */
 function waitWholePdfGenerated() {
-	showLoading();
 	var valid = getGeneratedPdfOrXlsInformation("pdf").valid;
 	if (!valid) {
 		console.log(new Date().format() + " ---- 文件还未生成");
@@ -309,16 +335,16 @@ function waitWholePdfGenerated() {
 /**
  * 获取生成的pdf或者xls的信息
  * 
- * @param pdfOrXls
- *            pdf或者xls
+ * @param fileType
+ *            文件格式,pdf、xls、xls_with_only_data
  */
-function getGeneratedPdfOrXlsInformation(pdfOrXls) {
+function getGeneratedPdfOrXlsInformation(fileType) {
 	var data;
 	$.ajax({
 		type : "get",
 		async : false,
 		url : "print/getGeneratedPdfOrXlsInformation" + window.location.search
-				+ "&pdfOrXls=" + pdfOrXls,
+				+ "&fileType=" + fileType,
 		success : function(result) {
 			console
 					.log(new Date().format() + " ---- "
@@ -334,7 +360,7 @@ function getGeneratedPdfOrXlsInformation(pdfOrXls) {
  */
 function renderPage() {
 	hideLoading();
-	if (firstRequest && printType == "PRINT") {
+	if (firstRequest && printType == "PRINT" && pageSize <= PRINT_MAX_PAGE_SIZE) {
 		showLoading();
 	}
 	if (!pdfDoc) {