Browse Source

add two ExportType: XLSX and XLSX_DATA

sunyj 8 years ago
parent
commit
d11ef72a13

+ 13 - 0
src/main/java/com/uas/report/SystemProperties.java

@@ -36,6 +36,12 @@ public class SystemProperties {
 	@Value("${max-record-size.phone}")
 	private int maxRecordSizePhone;
 
+	/**
+	 * 是否使用 xlsx
+	 */
+	@Value("${use-xlsx}")
+	private boolean useXlsx;
+
 	public int getTaskPeriod() {
 		return taskPeriod;
 	}
@@ -68,4 +74,11 @@ public class SystemProperties {
 		this.maxRecordSizePhone = maxRecordSizePhone;
 	}
 
+	public boolean isUseXlsx() {
+		return useXlsx;
+	}
+
+	public void setUseXlsx(boolean useXlsx) {
+		this.useXlsx = useXlsx;
+	}
 }

+ 52 - 38
src/main/java/com/uas/report/controller/PrintController.java

@@ -1,38 +1,30 @@
 package com.uas.report.controller;
 
-import java.io.File;
-import java.io.IOException;
-import java.sql.SQLException;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
+import com.uas.report.SystemProperties;
+import com.uas.report.model.ExportType;
+import com.uas.report.model.PrintType;
+import com.uas.report.service.FileService;
+import com.uas.report.service.PrintService;
+import com.uas.report.util.*;
+import net.sf.jasperreports.engine.JRException;
 import org.dom4j.DocumentException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 
-import com.uas.report.model.ExportType;
-import com.uas.report.model.PrintType;
-import com.uas.report.service.FileService;
-import com.uas.report.service.PrintService;
-import com.uas.report.util.ArrayUtils;
-import com.uas.report.util.CollectionUtils;
-import com.uas.report.util.FileUtils;
-import com.uas.report.util.Platform;
-import com.uas.report.util.ReportConstants;
-import com.uas.report.util.ReportUtils;
-import com.uas.report.util.StringUtils;
-
-import net.sf.jasperreports.engine.JRException;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * 报表打印
- * 
+ *
  * @author sunyj
  * @since 2016年8月16日 下午3:49:02
  */
@@ -41,6 +33,9 @@ import net.sf.jasperreports.engine.JRException;
 @RequestMapping("/print")
 public class PrintController {
 
+	@Autowired
+	private SystemProperties systemProperties;
+
 	@Autowired
 	private PrintService printService;
 
@@ -49,7 +44,7 @@ public class PrintController {
 
 	/**
 	 * 为UAS系统打印提供服务, 根据printType进行预览、打印、下载pdf、下载纯数据excel等操作
-	 * 
+	 *
 	 * @param userName
 	 *            不为null;当前账套名称
 	 * @param profile
@@ -89,28 +84,33 @@ public class PrintController {
 			request.getRequestDispatcher("preview").forward(request, response);
 			break;
 		case PDF:
-			export(userName, profile, reportName, whereCondition, otherParameters, ExportType.PDF.name(), true,
-					title, request, response);
+			export(userName, profile, reportName, whereCondition, otherParameters, ExportType.PDF.name(), true, title,
+					request, response);
 			break;
 		// 该下载接口供 UAS 系统使用,应其要求,printType 为{@link PrintType.EXCEL}时,下载纯数据的 excel
 		case EXCEL:
-			export(userName, profile, reportName, whereCondition, otherParameters, ExportType.XLS_DATA.name(),
-					true, title, request, response);
+			if (systemProperties.isUseXlsx()) {
+				export(userName, profile, reportName, whereCondition, otherParameters, ExportType.XLSX_DATA.name(),
+						true, title, request, response);
+			} else {
+				export(userName, profile, reportName, whereCondition, otherParameters, ExportType.XLS_DATA.name(), true,
+						title, request, response);
+			}
 			break;
 		case WORD:
-			export(userName, profile, reportName, whereCondition, otherParameters, ExportType.DOC.name(), true,
-					title, request, response);
+			export(userName, profile, reportName, whereCondition, otherParameters, ExportType.DOC.name(), true, title,
+					request, response);
 			break;
 		case TEXT:
-			export(userName, profile, reportName, whereCondition, otherParameters, ExportType.TXT.name(), true,
-					title, request, response);
+			export(userName, profile, reportName, whereCondition, otherParameters, ExportType.TXT.name(), true, title,
+					request, response);
 			break;
 		}
 	}
 
 	/**
 	 * 导出报表
-	 * 
+	 *
 	 * @param userName
 	 *            不为null;当前账套名称
 	 * @param profile
@@ -171,7 +171,7 @@ public class PrintController {
 
 	/**
 	 * 报表预览时获取pdf相对路径
-	 * 
+	 *
 	 * @param userName
 	 *            不为null;当前账套名称
 	 * @param profile
@@ -227,7 +227,7 @@ public class PrintController {
 
 	/**
 	 * 获取pdf数据(该接口暂时供手机端访问)
-	 * 
+	 *
 	 * @param userName
 	 *            不为null;当前账套名称
 	 * @param profile
@@ -269,8 +269,9 @@ public class PrintController {
 
 	/**
 	 * 获取该模板在当前条件下的结果数目
-	 * 
-	 * @param userName不为null;当前账套用户名
+	 *
+	 * @param userName
+     *            不为null;当前账套用户名
 	 * @param profile
 	 *            用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
 	 * @param reportName
@@ -296,4 +297,17 @@ public class PrintController {
 		ReportUtils.checkParameters(userName, reportName);
 		return printService.getCount(userName, profile, reportName, whereCondition, otherParameters);
 	}
+
+	/**
+	 * 是否使用 xlsx
+	 *
+	 * @param request
+	 * @param response
+	 * @return
+	 */
+	@RequestMapping(value = "/isUseXlsx")
+	@ResponseBody
+	public boolean isUseXlsx(HttpServletRequest request, HttpServletResponse response) {
+		return systemProperties.isUseXlsx();
+	}
 }

+ 14 - 4
src/main/java/com/uas/report/model/ExportType.java

@@ -2,7 +2,7 @@ package com.uas.report.model;
 
 /**
  * 导出类型
- * 
+ *
  * @author sunyj
  * @since 2017年10月24日 下午4:44:54
  */
@@ -23,6 +23,16 @@ public enum ExportType {
 	 */
 	XLS_DATA("xls"),
 
+	/**
+	 * excel
+	 */
+	XLSX("xlsx"),
+
+	/**
+	 * 纯数据 excel
+	 */
+	XLSX_DATA("xlsx"),
+
 	/**
 	 * word
 	 */
@@ -39,7 +49,7 @@ public enum ExportType {
 		return qualifier;
 	}
 
-	private ExportType(String qualifier) {
+	ExportType(String qualifier) {
 		this.qualifier = qualifier;
 	}
 
@@ -53,8 +63,8 @@ public enum ExportType {
 
 	/**
 	 * 获取导出类型
-	 * 
-	 * @param type
+	 *
+	 * @param type 导出类型
 	 * @return 获取的导出类型
 	 * @throws IllegalArgumentException
 	 *             失败,抛出异常

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

@@ -39,7 +39,7 @@ import java.util.regex.Pattern;
 
 /**
  * 报表打印
- * 
+ *
  * @author sunyj
  * @since 2016年8月18日 下午2:58:28
  */
@@ -81,7 +81,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 输出报表的数据
-	 * 
+	 *
 	 * @param userName
 	 * @param profile
 	 * @param reportName
@@ -151,7 +151,7 @@ public class PrintServiceImpl implements PrintService {
 				logger.info("export fillReport...");
 				boolean customCellStyle = false;
 				// 只导出数据
-				if (exportType == ExportType.XLS_DATA) {
+				if (exportType == ExportType.XLS_DATA || exportType == ExportType.XLSX_DATA) {
 					// 需自定义单元格格式
 					customCellStyle = true;
 					JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
@@ -172,9 +172,10 @@ public class PrintServiceImpl implements PrintService {
 				}
 
 				if (exportType == ExportType.XLS || exportType == ExportType.XLS_DATA) {
-//					exportReportToXls(jasperPrint, outputStream, customCellStyle);
+					exportReportToXls(jasperPrint, outputStream, customCellStyle);
+				} else if (exportType == ExportType.XLSX || exportType == ExportType.XLSX_DATA) {
 					exportReportToXlsx(jasperPrint, outputStream, customCellStyle);
-				} else if (exportType == ExportType.DOC) {
+                } else if (exportType == ExportType.DOC) {
 					exportReportToDoc(jasperPrint, outputStream);
 				} else if (exportType == ExportType.TXT) {
 					exportReportToText(jasperPrint, outputStream);
@@ -207,7 +208,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 可能需要编译报表模板文件
-	 * 
+	 *
 	 * @param jrxmlFilePath
 	 *            报表模板文件
 	 * @param isSub
@@ -260,7 +261,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 获取报表模板所引用的子报表路径
-	 * 
+	 *
 	 * @param jrxmlFilePath
 	 * @return
 	 * @throws DocumentException
@@ -289,7 +290,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 编译jrxml报表模板为jasper文件
-	 * 
+	 *
 	 * @param jrxmlFilePath
 	 *            模板文件
 	 * @param isSub
@@ -318,7 +319,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 处理模板中的field(与数据库中列名进行比较,删除表中无对应列名的field)
-	 * 
+	 *
 	 * @param jrxmlFilePath
 	 * @param otherParameters
 	 * @param connection
@@ -376,7 +377,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 从数据库获取列名
-	 * 
+	 *
 	 * @param connection
 	 * @param sql
 	 * @return
@@ -395,7 +396,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 获取模板中实际使用的Field
-	 * 
+	 *
 	 * @param jrxmlFilePath
 	 * @return
 	 * @throws IOException
@@ -414,7 +415,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 替换字体
-	 * 
+	 *
 	 * @param jrxmlFilePath
 	 * @throws IOException
 	 * @throws FileNotFoundException
@@ -428,7 +429,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 更改可换行的textField的行距
-	 * 
+	 *
 	 * @param jrxmlFilePath
 	 * @return 修改后的jrxml源码
 	 * @throws FileNotFoundException
@@ -477,7 +478,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 以xls的格式导出报表
-	 * 
+	 *
 	 * @param jasperPrint
 	 * @param outputStream
 	 * @throws JRException
@@ -494,7 +495,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 以xlsx的格式导出报表
-	 * 
+	 *
 	 * @param jasperPrint
 	 * @param outputStream
 	 * @throws JRException
@@ -511,7 +512,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 以doc的格式导出报表
-	 * 
+	 *
 	 * @param jasperPrint
 	 * @param outputStream
 	 * @throws JRException
@@ -523,7 +524,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 以rtf的格式导出报表
-	 * 
+	 *
 	 * @param jasperPrint
 	 * @param outputStream
 	 * @throws JRException
@@ -540,7 +541,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 以pdf的格式导出报表
-	 * 
+	 *
 	 * @param jasperPrint
 	 * @param outputStream
 	 * @param pageIndex
@@ -569,7 +570,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 以text的格式导出报表
-	 * 
+	 *
 	 * @param jasperPrint
 	 * @param outputStream
 	 * @throws JRException
@@ -585,7 +586,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 移除除了Column Header和Detail之外的元素
-	 * 
+	 *
 	 * @param jasperDesign
 	 */
 	private void removeUnusedElements(JasperDesign jasperDesign) {
@@ -692,7 +693,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 获取该模板在当前条件下的结果数目
-	 * 
+	 *
 	 * @param jrxmlFilePath
 	 * @param whereCondition
 	 * @param otherParameters
@@ -730,7 +731,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 替换sql语句中的动态参数
-	 * 
+	 *
 	 * @param sql
 	 * @param otherParameters
 	 * @return
@@ -764,7 +765,7 @@ public class PrintServiceImpl implements PrintService {
 
 	/**
 	 * 获取当前查询语句的结果数目
-	 * 
+	 *
 	 * @param connection
 	 * @param sql
 	 * @return

+ 2 - 0
src/main/resources/application.yml

@@ -33,6 +33,8 @@ max-record-size:
  pc: 100000
  phone: 10000
 
+use-xlsx: false
+
 jasperserver:
  rest:
   api:

+ 37 - 4
src/main/webapp/resources/js/preview/app.js

@@ -119,21 +119,54 @@ $("#downloadWord").click(function() {
 });
 
 // 下载excel
-$("#downloadExcel").click(function() {
+$("#downloadExcel").click(function () {
 	if (!pdfDoc) {
 		return;
 	}
-	window.open(downloadUrl("xls"));
+	downloadExcel(false);
 });
 
 // 下载纯数据excel
-$("#downloadExcelData").click(function() {
+$("#downloadExcelData").click(function () {
 	if (!pdfDoc) {
 		return;
 	}
-	window.open(downloadUrl("xls_data"));
+	downloadExcel(true);
 });
 
+/**
+ * 下载 excel
+ * @param onlyData 是否仅下载数据
+ */
+function downloadExcel(onlyData) {
+	$.ajax({
+		type: "get",
+		async: true,
+		url: "print/isUseXlsx",
+		success: function (isUseXlsx) {
+			var excelType;
+			// 根据结果决定使用 xls 还是 xlsx
+			if (isUseXlsx) {
+				if (onlyData) {
+					excelType = "xlsx_data";
+				} else {
+					excelType = "xlsx";
+				}
+			} else {
+				if (onlyData) {
+					excelType = "xls_data";
+				} else {
+					excelType = "xls";
+				}
+			}
+			window.open(downloadUrl(excelType));
+		},
+		error: function (XMLHttpRequest) {
+			showError(XMLHttpRequest);
+		}
+	});
+}
+
 //下载text
 $("#downloadText").click(function() {
 	if (!pdfDoc) {