最近,因為總經理提出了一個需求,每個禮拜收到的 Report Alert 中的 Excel 檔案需要加密,原本的 SSRS 所發送的 Excel 是 SSRS 本身自動產生的無加密檔案,但是這問題如何解決呢?經過我這個無理頭的思考後,突發奇想,使用 SSIS 呼叫 SSRS 的 Web Service 後產生 Excel 檔案,在利用以前管理 AD/Exchange 時常做的 VBScript 來匯出匯入到 Excel 中的加密檔案程式片段,最後,在把 Excel 檔案給 Mail 出去,終於在多番測試後,真的被我給實踐出來了。流程如下圖:
我所使用的系統為 SISS 2008 & SSRS 2008,在 SSIS 2005 中是無法呼叫 Web Service,另外也不支援 C#,不過我還是有做 SSIS 2005 的版本,有需要在跟我說吧~因為 2005 實在是有夠麻煩,我們所用的的元件其實只有兩種,主要是使用 ”指令碼工作”以及一個 ”傳送郵件工作”。
首先,先簡單介紹一下第一個步驟,呼叫 SSRS 的 Web Service,請由工具箱拉取一個 ”指令碼工作”元件,點擊兩下進入編輯設定,在 ScriptLanguage 中因為這次我使用C# 程式,所以請選擇 “Microsoft Visual C# 2008”,如果有設定變數的話,請自行選取你要用的變數,否則就點選 ”編輯指令碼”進入編輯程式介面。
1. 進入程式編輯畫面後,第一步驟,加入 SSRS 的 Web Service,在專案總管上按滑鼠右鍵,選擇 ”加入 Web 參考”,在 URL 上打上您的 SSRS 的 Web Service 路徑,http://localhost/ReportServer/ReportExecution2005.asmx?wsdl,然後在 Web 參考名稱 打上ReportExecutionService (名稱可以自定),之後按下 ”加入參考”,如下圖。
2. 加入 ReportExecution2005 命名空間,由於在這個 Script Task 中的 Package 名稱是一個常串的亂碼名稱,所以加入 ReportExecution2005 時需要注意,請跟您的命名空間一致。
3. 接下來就是主要的程式內容,內容我就不再詳細敘述了,有空在來說明:
public void Main() { ReportExecutionService res = new ReportExecutionService(); res.Credentials = System.Net.CredentialCache.DefaultCredentials; res.Url = "http://localhost/reportserver/ReportExecution2005.asmx"; // Setting Report Name string ReportName = Dts.Variables["ReportName"].Value.ToString(); string historyID = null; Byte[] results; string format = "Excel"; //XML, NULL, CSV, IMAGE, PDF, HTML4.0, HTML3.2, MHTML, EXCEL, and HTMLOWC string deviceInfo = null; string extension = String.Empty; string mimeType = String.Empty; string encoding = String.Empty; ReportExecution2005.Warning[] warnings = null; string[] streamIDs = null; // Save Location and File Name string fileName = Dts.Variables["FileName"].Value.ToString(); try { ExecutionInfo ei = res.LoadReport(ReportName, historyID); // Setting Report Parameters ReportExecution2005.ParameterValue[] rptParameters = new ReportExecution2005.ParameterValue[1]; rptParameters[0] = new ReportExecution2005.ParameterValue(); rptParameters[0].Name = "ParameterName"; rptParameters[0].Value = "ParameterValue"; res.SetExecutionParameters(rptParameters, "en-us"); results = res.Render(format, deviceInfo, out extension, out mimeType, out encoding, out warnings, out streamIDs); // Save the result file using (FileStream stream = File.Create(fileName, results.Length)) { stream.Write(results, 0, results.Length); } } catch (Exception ex) { Dts.TaskResult = (int)ScriptResults.Failure; } Dts.TaskResult = (int)ScriptResults.Success; }
繼續閱讀請參考,在 SSIS 上產生 SSRS 的 Excel 加密檔案並且發送 E-mail, PART II
留言列表