|
|
@@ -0,0 +1,115 @@
|
|
|
+package com.uas.report.controller;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+
|
|
|
+import com.uas.report.service.PrintService;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 报表打印
|
|
|
+ *
|
|
|
+ * @author sunyj
|
|
|
+ * @since 2016年8月16日 下午3:49:02
|
|
|
+ */
|
|
|
+
|
|
|
+@Controller
|
|
|
+@RequestMapping("/print")
|
|
|
+public class PrintController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PrintService printService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 打印报表
|
|
|
+ *
|
|
|
+ * @param reportName
|
|
|
+ * 不为null;需要打印的报表的名称,不带任何后缀(如打印采购单,即为"Purchase")
|
|
|
+ * @param whereCondition
|
|
|
+ * 可为null;where之后的条件(包括where)
|
|
|
+ * @param otherParameters
|
|
|
+ * 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数
|
|
|
+ * @param response
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("")
|
|
|
+ @ResponseBody
|
|
|
+ public String print(String reportName, String whereCondition, Map<String, Object> otherParameters,
|
|
|
+ HttpServletResponse response) {
|
|
|
+ String message = "";
|
|
|
+ if (StringUtils.isEmpty(reportName)) {
|
|
|
+ message = "报表名称无效!";
|
|
|
+ System.out.println(message);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println("\n开始打印报表: " + reportName + "...");
|
|
|
+ byte[] results = printService.print(reportName, whereCondition, otherParameters);
|
|
|
+ if (results == null || results.length < 1) {
|
|
|
+ message = "报表 " + reportName + " 打印失败!";
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ OutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ response.setContentType("application/pdf");
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ outputStream.write(results);
|
|
|
+ outputStream.flush();
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println("连接被关闭!");
|
|
|
+ }
|
|
|
+
|
|
|
+ message = "报表 " + reportName + " 打印完成!";
|
|
|
+ System.out.println(message);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/test")
|
|
|
+ @ResponseBody
|
|
|
+ public String testPrint(HttpServletResponse response) {
|
|
|
+ String reportName = "Purchase";
|
|
|
+ String whereCondition = "where pu_code = 'MP160800017'";
|
|
|
+ Map<String, Object> otherParameters = new HashMap<>();
|
|
|
+ otherParameters.put("OTHER_PARAMETER_1", "天气真好!");
|
|
|
+
|
|
|
+ String message = "";
|
|
|
+ if (StringUtils.isEmpty(reportName)) {
|
|
|
+ message = "报表名称无效!";
|
|
|
+ System.out.println(message);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println("\n开始打印报表: " + reportName + "...");
|
|
|
+ byte[] results = printService.print(reportName, whereCondition, otherParameters);
|
|
|
+ if (results == null || results.length < 1) {
|
|
|
+ message = "报表 " + reportName + " 打印失败!";
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ OutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ response.setContentType("application/pdf");
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ outputStream.write(results);
|
|
|
+ outputStream.flush();
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println("连接被关闭!");
|
|
|
+ }
|
|
|
+
|
|
|
+ message = "报表 " + reportName + " 打印完成!";
|
|
|
+ System.out.println(message);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+}
|