2010年4月8日 星期四

Reporting Service

一、設計報表
新增一個報表檔(.rdl)
<Ⅰ>資料-->設定資料來源(dbserver、dbname、username、password等),然後填寫sql或讀取stored procedure去篩選報表資料
<Ⅱ>配置-->拉好所需的報表介面後,開始填入報表內容 。
報表內容:
    頁首、頁尾:參數中的全域參數(ex:pagenum、reportname)只能用在此處,不能放在主體。
                    table元件不可以放在此處,欄位的值也不可以放在此處。
                    使用textbox時,若內容的值一定要用欄位,可以將該欄位的值設成報表參數,再引用該報表參數到textbox。

ex:CarCategory是sql產生的欄位值之一,因為在頁首不能使用,所以加入一個參數,然後將隱藏、允許Null值勾選, 允許空白值預設
        就有勾選,接著可用的值跟預設值都設成"從查詢",然後資料集、值欄位設定好就可以了



    主體:使用table元件時,要注意header跟footer只能欄合併,不能列合併,所以要設計比較複制的header或footer,
            可以在頁首或頁尾設計,運算式運用可以參考http://technet.microsoft.com/zh-tw/library/ms157328.aspx
            若需要固定excel的列高時,需要將table元件的detail每個欄位CanGrow、CanShrink皆設為false
            匯出excel時才有效果,參考http://www.kodyaz.com/articles/reporting-services-excel-rendering-autogrow.aspx

<Ⅲ>預覽-->觀看結果

二、在.NET上使用報表
<Ⅰ>以url方式直接產生報表
String scriptString = string.Format(@"
                            <script language=JavaScript>
                                <!-- begin
                                    window.open('http://localhost/ReportServer?/DirectoryName/FileName&rs:Format=excel&rs:ClearSession=true&rc:Parameters=false&ApplyNum='123456','','Width:400,Height:300,center:yes,status:no,scrollbars=no');
                                //end -->
                            </script> ");
            this.RegisterStartupScript("s", scriptString);
//DirectoryName 設置在report server上的資料夾
//FileName 報表名稱
//rs:Format=excel 這個參數是控制文件的格式,要轉出pdf則改為pdf,省略不寫則show出報表畫面,可以從所有格式中選擇其一匯出
//rc:Parameters=false 設為false時不會出現輸入參數的訊息
<Ⅱ>以ReportViewer元件呈現
        類似DataSource元件,拉進去後,填好資料來源、報表連結等資料就可以呈現。

-----------------------------------------分  格  線----------------------------------------分  格  線---------------------------------------------------
一般製作報表只需要上述兩點即可,接下來是要進行其他用途時才使用。

進行下面步驟的目的:
原本報表匯出時是直接給user點選匯出,但為了進行檔案內容的變更,所以先行將報表改成匯出到server
上(就是下述第三點),
然後再使用應用程式去開啟excel,變更格式後關閉(第四點),接著匯出(第五點)給user。                                                             



三、將報表直接存於server上
1.在專案新增web參考(webReference)=>ReportExecution2005
2.程式段
            localhost.ReportExecutionService rs = new localhost.ReportExecutionService();  //localhost為引用的webReference Name
            rs.Credentials = new System.Net.NetworkCredential("username", "password");

            string reportPath = "/Certificate/RptCertificate_G4"; //在report server上的報表檔案路徑
            string format = "EXCEL";
            string savePath = "D:\\Temp\\ReportTest.xls"; //儲存在server的資料夾路徑

            Byte[] result;
            localhost.ParameterValue[] parameters = new localhost.ParameterValue[1]; //這邊是報表參數,視其所需來增減
            parameters[0] = new localhost.ParameterValue();
            parameters[0].Name = "ApplyNum";
            parameters[0].Value = lblApplyNum.Text.Trim();
            string mimeType;
            localhost.Warning[] warnings = null;
            localhost.ParameterValue[] reportHistoryParameters = null;

            string[] streamIDs;
            string encoding;
            string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>"; //false為不顯示報表工具
            string historyId = null;
            localhost.ExecutionInfo execInfo = new localhost.ExecutionInfo();
            localhost.ExecutionHeader execHeader = new localhost.ExecutionHeader();
            string SessionId;
            string extenstion = "";
            rs.ExecutionHeaderValue = execHeader;

            try
            {
                execInfo = rs.LoadReport(reportPath, historyId);
                rs.SetExecutionParameters(parameters, "en-us");
                SessionId = rs.ExecutionHeaderValue.ExecutionID;

                result = rs.Render(format, devInfo, out extenstion, out mimeType, out encoding, out warnings, out streamIDs);
                execInfo = rs.GetExecutionInfo();
                FileStream stream = new FileStream(savePath, FileMode.Create);
                stream.Write(result, 0, result.Length);
                stream.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
 
四、將儲存於server上的excel檔進行修改
<Ⅰ>直接使用Application開啟excel進行修改

加入參考=>Microsoft.Excel 11.0 Object Library

命名空間
using Microsoft.Office.Interop.Excel;
using System.Configuration;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;

程式段
object missing = Missing.Value;
Excel.ApplicationClass app;
Excel.Workbook book;
Excel.Worksheet sheet;
Excel.Range ra;

app = new Excel.ApplicationClass();
try
{
    book = app.Workbooks.Open(savePath, missing, missing, missing, missing, missing,
    missing, missing, missing, missing, missing, missing, missing, missing, missing);

    //得到WorkSheet
    for (int i = 0; i < book.Sheets.Count; i++)
    {
        sheet = (Excel.Worksheet)book.Sheets.get_Item(i + 1);

        sheet.Rows.RowHeight = 36;
        sheet.UsedRange.Rows.RowHeight = 36;
        string aa = sheet.Cells[28, 1].ToString();
        string ab = sheet.get_Range(sheet.Cells[28, 1], sheet.Cells[28, 1]).Text.ToString();
        string ac = sheet.get_Range(sheet.Cells[28, 1], sheet.Cells[28, 1]).RowHeight.ToString();
        sheet.Cells[26, 1] = "1";
        sheet.Cells[29, 1] = "1";
        //調第27列(車型名稱最下面那列)
        if (reportPath == "/Certificate/RptCertificate_G4")
        {
            sheet.get_Range(sheet.Cells[27, 1], sheet.Cells[27, 1]).RowHeight = 24;
            //調內容那八列
            sheet.get_Range(sheet.Cells[28, 1], sheet.Cells[35, 1]).RowHeight = 27;
        }
        //設定顏色
        sheet.get_Range("B:B", Type.Missing).Font.Color = 255;
        //合併儲存格
        sheet.get_Range(sheet.Cells[37, 1], sheet.Cells[39, 1]).MergeCells = true;
        //得到使用過的欄位數目
        System.Runtime.InteropServices.Marshal.ReleaseComObject((object)sheet);
    }
    book.Close(true, Type.Missing, Type.Missing);
    app.Workbooks.Close();
    System.Runtime.InteropServices.Marshal.ReleaseComObject((object)book);
}
catch (Exception es)
{
    throw es;
}
finally
{
    app.Quit();
    try
    {
        IntPtr t = new IntPtr(app.Hwnd);
        int k = 0;
        GetWindowThreadProcessId(t, out k);
        System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
        p.Kill();
    }
    catch(Exception et)
    {
        string bo = et.Message;
    }

優點:可以直接修改excel的屬性,不會變動到整個資料格式。
缺點:process關不掉,會造成記憶體空間不足

<Ⅱ>使用NPOI修改excel
    利用記憶體讀取excel的資料後,另外匯出到另一個excel,而且可以設定excel的屬性。
參考如下
http://selene-tw.blogspot.com/2010/01/npoi.html

優點:不用擔心process關不掉的結果。
缺點:如果是複雜的報表格式,要一一重新設定格式,非常不適用,與其用報表設計完再從這裡進行修改,倒不如直接在這裡土法設計出報表格式,所以這個方法比較適用簡單的報表資料匯出


五、匯出檔案
            //開啟在server上的excel檔提供使用者下載或存檔
            System.IO.FileInfo fileinfo;
            string sFileName = "D://Temp//" + lblApplyNum.Text.Trim() + ".xls";
            fileinfo = new System.IO.FileInfo(sFileName);
            if (fileinfo.Exists)
            {
                Response.ClearHeaders();
                Response.Clear();
                string attachment = "attachment; filename=" + lblApplyNum.Text.Trim() + ".xls";
                Response.AddHeader("Accept-Language", "zh-tw");
                Response.AddHeader("Content-Disposition", attachment);
                Response.AddHeader("Content-Length", fileinfo.Length.ToString());
                Response.ContentType = "application/ms-excel";
                Response.WriteFile(fileinfo.FullName);
                Response.Flush();
                //刪除temp中該檔案
                fileinfo.Delete();
                Response.End();
            }
            string scriptString = string.Format(@"
                    <script language=JavaScript>
                    <!-- begin            
                     window.open('{0}');
                    //end -->
                    </script>
                    ", savePath);
            this.RegisterStartupScript("s", scriptString);

結論:由於第四點的兩種方法在實作上各有困難點,所以可以欣賞欣賞就好,慶幸的是最後格式的問題
         在報表中找到了解決方法,就是第一點在配置時的本體找到的固定excel列高方法。