C#

ReportViewer應用

#遠端帳號登入及只顯示匯出excel

if (!this.IsPostBack)
{
    ReportViewer1.ServerReport.ReportServerCredentials = new CustomReportCredentials("administrator", "password", ".");
    ReportViewer1.ServerReport.Refresh();

    //EXPORT只保留EXCEL的選項
    foreach (RenderingExtension extension in ReportViewer1.ServerReport.ListRenderingExtensions())
    {
        if (extension.Name != "EXCEL")
        {
            System.Reflection.FieldInfo fieldInfo = extension.GetType().GetField("m_isVisible", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            fieldInfo.SetValue(extension, false);
        }
    }
}

在同一個cs檔最下方加入即可

public class CustomReportCredentials : Microsoft.Reporting.WebForms.IReportServerCredentials
{
    // local variable for network credential
    private string strUserName;
    private string strPassWord;
    private string strDomainName;
    public CustomReportCredentials(string UserName, string PassWord, string DomainName)
    {
        strUserName = UserName;
        strPassWord = PassWord;
        strDomainName = DomainName;
    }
    public System.Security.Principal.WindowsIdentity ImpersonationUser
    {
        // not use ImpersonationUser
        get { return null; }
    }
    public System.Net.ICredentials NetworkCredentials
    {
        // use NetworkCredentials
        get { return new System.Net.NetworkCredential(strUserName, strPassWord, strDomainName); }
    }
    public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName, out string password, out string authority)
    {
        // not use FormsCredentials unless you have implements a custom autentication.
        authCookie = null;
        userName = null;
        password = null;
        authority = null;
        return false;
    }
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。