|
|
@@ -1,13 +1,14 @@
|
|
|
package com.uas.report.controller;
|
|
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
-import org.apache.commons.lang.ArrayUtils;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
@@ -44,13 +45,15 @@ public class PrintController {
|
|
|
* 可为null;where之后的条件(包括where)
|
|
|
* @param otherParameters
|
|
|
* 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数
|
|
|
+ * @param exportFileType
|
|
|
+ * 报表打印的格式,默认为pdf
|
|
|
* @param response
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping
|
|
|
@ResponseBody
|
|
|
public String print(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
|
|
|
- HttpServletResponse response) {
|
|
|
+ String exportFileType, HttpServletResponse response) {
|
|
|
String message = "";
|
|
|
if (StringUtils.isEmpty(userName)) {
|
|
|
message = "未传入当前账套用户名!";
|
|
|
@@ -64,18 +67,14 @@ public class PrintController {
|
|
|
}
|
|
|
|
|
|
logger.info("开始打印报表: " + reportName);
|
|
|
- byte[] results = printService.print(userName, reportName, whereCondition, otherParameters);
|
|
|
- if (ArrayUtils.isEmpty(results)) {
|
|
|
- message = "报表 " + reportName + " 打印失败!";
|
|
|
- logger.error(message);
|
|
|
- return message;
|
|
|
+ if (StringUtils.isEmpty(exportFileType)) {
|
|
|
+ exportFileType = "pdf";
|
|
|
}
|
|
|
|
|
|
- OutputStream outputStream = null;
|
|
|
try {
|
|
|
- response.setContentType("application/pdf");
|
|
|
- outputStream = response.getOutputStream();
|
|
|
- outputStream.write(results);
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + reportName + "." + exportFileType);
|
|
|
+ OutputStream outputStream = response.getOutputStream();
|
|
|
+ printService.print(userName, reportName, whereCondition, otherParameters, exportFileType, outputStream);
|
|
|
outputStream.flush();
|
|
|
outputStream.close();
|
|
|
} catch (IOException e) {
|
|
|
@@ -105,7 +104,8 @@ public class PrintController {
|
|
|
String whereCondition = "where pu_code = 'YFMP160600001'";
|
|
|
Map<String, Object> otherParameters = new HashMap<>();
|
|
|
otherParameters.put("OTHER_PARAMETER_TEST", "天气真好!");
|
|
|
- return print(userName, reportName, whereCondition, otherParameters, response);
|
|
|
+ String exportFileType = "xls";
|
|
|
+ return print(userName, reportName, whereCondition, otherParameters, exportFileType, response);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -119,13 +119,33 @@ public class PrintController {
|
|
|
* 可为null;where之后的条件(包括where)
|
|
|
* @param otherParameters
|
|
|
* 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数
|
|
|
- * @param response
|
|
|
+ * @param request
|
|
|
* @return
|
|
|
*/
|
|
|
- @RequestMapping(params = "type=preview")
|
|
|
- public String preview(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
|
|
|
- HttpServletResponse response) {
|
|
|
+ @RequestMapping(value = "/preview")
|
|
|
+ public String preview(String userName, String reportName, String whereCondition,
|
|
|
+ Map<String, Object> otherParameters, HttpServletRequest request) {
|
|
|
// TODO
|
|
|
- return "preview.jsp";
|
|
|
+ String message = "";
|
|
|
+ if (StringUtils.isEmpty(userName)) {
|
|
|
+ message = "未传入当前账套用户名!";
|
|
|
+ logger.error(message);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(reportName)) {
|
|
|
+ message = "报表名称无效!";
|
|
|
+ logger.error(message);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info("开始打印报表: " + reportName);
|
|
|
+ String exportFileType = "pdf";
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ printService.print(userName, reportName, whereCondition, otherParameters, exportFileType, outputStream);
|
|
|
+ request.setAttribute("data", outputStream.toByteArray());
|
|
|
+ request.setAttribute("message", "test");
|
|
|
+ message = "报表 " + reportName + " 打印完成!";
|
|
|
+ logger.info(message);
|
|
|
+ return "preview";
|
|
|
}
|
|
|
}
|