---
# System prepended metadata

title: 報表列印
tags: [國興實習]

---

---
tags: 國興實習
---
# 報表列印

#### 預存程序
```sql=
-- ================================================
IF OBJECT_ID ( 'dbo.RP_QA_SHEET', 'P' ) IS NOT NULL --刪除原檔再建立
    DROP PROCEDURE dbo.RP_QA_SHEET;
GO
--EXEC RP_QA_SHEET

-- =============================================
-- Author:      陳思妤
-- Create date: 2021/03/05
-- Description: 提供查詢、列印問題單資料功能
-- =============================================

---參數
 CREATE PROCEDURE [dbo].[RP_QA_SHEET]
  @CUST_DEPT nvarchar(10),
  @CUST_NO varchar(20),
  @UNIT_NO nvarchar(20),
  @POST_DATE date,
  @END_DATE date,
  @MEMO nvarchar(500)
AS

---
BEGIN
 select M.*, N.CUST_NM, L.NAME, K.code_txt from QA_SHEET M left join QA_CUST N on M.CUST_NO = N.CUST_NO left join QA_CUST_UNIT L on M.UNIT_NO = L.UNIT_NO left join SYSCOMMI K on M.STATUS = K.code_no AND K.comm_no = 'SYS_STATUS'
 where (M.CUST_DEPT = @CUST_DEPT or @CUST_DEPT = '') 
 AND (M.CUST_NO = @CUST_NO or @CUST_NO = '') 
 AND (M.UNIT_NO = @UNIT_NO or @UNIT_NO = '') 
 AND (M.POST_DATE = @POST_DATE or @POST_DATE = '') 
 AND (M.END_DATE = @END_DATE or @END_DATE = '')
 AND (M.MEMO like '%' + @MEMO + '%' or @MEMO = '')
 ---避免SearchData參數為""，造成不正確的報表結果。
END

GO
```
---
#### 後端Controller
```C#=
[System.Web.Http.HttpPost]
public IHttpActionResult Print(SearchClass a_Search)
{
    string RptName = "";
    string PdfName = "QAS002";

    DBController dbc = new DBController("UseDB");
    Hashtable ht = new Hashtable();

    //設定資料庫連線字串
    string l_UseDB = ConfigurationManager.AppSettings["UseDB"].ToString();
    ConnectionStringSettings l_conString = ConfigurationManager.ConnectionStrings[l_UseDB];
    string connectionString = l_conString.ToString();

    //設定參數
    Dictionary<string, string> param = new Dictionary<string, string>();
    param.Add("@CUST_DEPT", (a_Search.CUST_DEPT ?? "").ToString());
    param.Add("@CUST_NO", (a_Search.CUST_NO ?? "").ToString());
    param.Add("@UNIT_NO", (a_Search.UNIT_NO ?? "").ToString());
    param.Add("@POST_DATE", (a_Search.POST_DATE ?? "").ToString());
    param.Add("@END_DATE", (a_Search.END_DATE ?? "").ToString());
    param.Add("@MEMO", (a_Search.MEMO ?? "").ToString());
    param.Add("@PAGE", "1");


    if (a_Search.CUST_NO != "" && a_Search.CUST_NO != null) //For客戶
    {
        ht.Add("@CUST_NO", new StructureSQLParameter(a_Search.CUST_NO, SqlDbType.NVarChar));
        string CUST_NM = dbc.DbExecuteScalar_Str("select CUST_NM FROM QA_CUST where CUST_NO = @CUST_NO ORDER BY CUST_NO DESC", ht);
        param.Add("@TITLE", a_Search.CUST_NO + "_" + CUST_NM);//PAGE
        //宣告報表名稱
        RptName = "QAS002_CUST.rpt";
    }
    else
    {
        ht.Add("@DEPT_NO", new StructureSQLParameter(a_Search.CUST_DEPT, SqlDbType.NVarChar));
        string DEPT_NM = dbc.DbExecuteScalar_Str("select DEPT_NM FROM QA_DEPT where DEPT_NO = @DEPT_NO ORDER BY DEPT_NO DESC", ht);
        param.Add("@TITLE", a_Search.CUST_DEPT + "_" + DEPT_NM);//PAGE
        //宣告報表名稱
        RptName = "QAS002.rpt";
    }

    //設定報表
    List<CrystalReportContent> crystalReportContents = new List<CrystalReportContent>
    {
        //設定報表名稱&參數&起始頁碼&資料
        new CrystalReportContent(RptName, param, 0, connectionString),
    };

    CrystalReportTool crystalReportTool = new CrystalReportTool(crystalReportContents, PdfName + ".pdf");
    return DoPrintResult(crystalReportTool);
}
```
:::info
* **Controller之param.Add()不可輸入null值**
:::

---

#### Crystal參數設定
* 右鍵【資料庫欄位】>選擇【資料庫專家】

    ![](https://i.imgur.com/XQKVuIK.png)
* 建立新連線>連線到你的資料庫

    ![](https://i.imgur.com/amSzOfH.png)
    ![](https://i.imgur.com/sIF7EaY.png)
* 在你加入的資料庫下選擇加入命令，並按【>】

    ![](https://i.imgur.com/kKYFrni.png)
* EXEC你對應的SP，並加入參數

    ![](https://i.imgur.com/eaOC428.png)
* 即可建立你的報表欄位
    
    ![](https://i.imgur.com/d1KVKIo.png)

---

#### 一些補充
* 資料超出欄位

    1. 右鍵你設計的線條>選擇【格式物件】
    
        ![](https://i.imgur.com/7PhalmM.png)
    2. 選擇【列印時延伸至區段的底部】（若為水平線則為移至區段的底部）
    
        ![](https://i.imgur.com/rgwJ5No.png)

