2017年6月11日 星期日

所有基態null處理(泛型+擴充方法)

    ///
    /// 擴充方法ObjectIsNullOrEmpty
    /// 適用於所有型態有null值時直接回傳DBNull.Value
    ///
    public static class Extension
    {
        public static object ObjectIsNullOrEmpty(this T value)
        {
            if (value == null || string.IsNullOrEmpty(value.ToString()))
            { return DBNull.Value; }
            else
                return value;

        }

    }

2017年1月11日 星期三

Report Server報表轉pdf

  web service寫法

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.Reporting.WebForms;
using System.IO;
using System.Net;
using System.Security;
using System.Configuration;



namespace ReportPdfGenerater
{
    ///

    ///Service1 的摘要描述
    ///
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string ReportPdfGenerater()
        {
         
            try
            {
                ReportViewer ReportViewer1 = new ReportViewer();
                ServerReport SR = ReportViewer1.ServerReport;//Server Report

                ReportViewer1.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["ReportServerUrl"]);
                IReportServerCredentials rsnc = new ReportCredential(ConfigurationManager.AppSettings["ReportServerUser"], ConfigurationManager.AppSettings["ReportServerPassword"], ConfigurationManager.AppSettings["ReportServerDomain"]);
                ReportViewer1.ServerReport.ReportServerCredentials = rsnc;
                ReportViewer1.ServerReport.ReportPath = @"/報表資料夾名稱/TEST";
                ReportViewer1.Height = 700;
                ReportViewer1.Width = 1000;

                ReportViewer1.ShowParameterPrompts = true;

                ReportParameter[] para = new ReportParameter[1];
                para[0] = new ReportParameter("EMPLOYEE_NUMBER", "999008");


                ReportViewer1.ServerReport.SetParameters(para);

                Microsoft.Reporting.WebForms.Warning[] tWarnings;
                string[] tStreamids;
                string tMimeType;
                string tEncoding;
                string tExtension;
                string[] streamids;
                byte[] bytes = ReportViewer1.ServerReport.Render("PDF", null, out tMimeType, out tEncoding, out tExtension, out streamids, out tWarnings);


                //設定匯出的路徑
                System.IO.FileStream fs = System.IO.File.Create(ConfigurationManager.AppSettings["ReportServerPDFLocation"] + "report.pdf");

                fs.Write(bytes, 0, bytes.Length);

                fs.Close();

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
 


            return "OK";

        }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Web.config設定
 <appSettings>
    <add key="ReportServerUser" value="帳號"/>
    <add key="ReportServerPassword" value="密碼"/>
    <add key="ReportServerDomain" value=""/>
    <add key="ReportServerPDFLocation" value="路徑"/>
    <add key="ReportServerUrl" value="ip位置/reportserver"/>
  </appSettings>