编程语言
首页 > 编程语言> > c# – 打开从调用Reporting Services Web服务ReportExecution2005创建的Xlsx文件时出错

c# – 打开从调用Reporting Services Web服务ReportExecution2005创建的Xlsx文件时出错

作者:互联网

我无法打开呈现为xlsx文件的SSRS报告,该报告是通过从SSIS脚本任务调用Reporting Services Web服务ReportExecution2005.asmx?wsdl生成的.
但我可以打开由同一方法生成的xls文件.

有人可以告诉我,我需要做些什么才能呈现可用的xlsx文件?

我正在尝试使用脚本任务从SSIS运行Reporting Services报告.
我需要将报告呈现为Excel xlsx文件.
如果我使用.xls扩展名,我的代码可以工作,我的意思是它确实会产生一个可以在Excel中打开的xls文件.但是如果我将文件扩展名更改为xlsx,我会得到一个无法打开的文件,并产生以下错误.
“Excel无法打开文件,因为文件格式或文件扩展名无效.验证文件……… ..“

在我的代码我正在使用

MimeType = “application/vnd.ms-excel” for the xls file

MimeType =“application / vnd.openxmlformats-officedocument.spreadsheetml.sheet

对于xlsx文件

我用来运行报告的Web服务是ReportExecution2005.asmx?wsdl
(我假设它是正确的?)

我在SSIS脚本任务中的代码如下,

        var rsClient = new RSExec.ReportExecutionServiceSoapClient();

        rsClient.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential("username", "password");

        rsClient.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

        byte[] result = null;

        string reportPath = "filepath/filename";  
        string format = "EXCEL"; 
        string historyID = null;
        string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

        string encoding = String.Empty; //"";

        string mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // Excel2010


        //string mimeType = "application/vnd.ms-excel"; // EXCEL

        string extension = "";
        Warning[] warnings = null;
        string[] streamIDs = null;
        ParameterValue[] parameters = new ParameterValue[1];
        parameters[0] = new ParameterValue();
        parameters[0].Name = "ReportDate";
        parameters[0].Value = "12/11/2013";


        RSExec.ExecutionInfo execInfo = new RSExec.ExecutionInfo();
        RSExec.TrustedUserHeader trustedUH = new TrustedUserHeader();
        RSExec.ExecutionHeader execHeader = new RSExec.ExecutionHeader();
        RSExec.ServerInfoHeader serverInfo = new ServerInfoHeader();

        execHeader = rsClient.LoadReport(trustedUH, reportPath, historyID, out serverInfo, out execInfo);

        rsClient.SetExecutionParameters(execHeader, trustedUH, parameters, "en-us", out execInfo);



        rsClient.Render(execHeader, trustedUH, format, devInfo, out result, out extension, out encoding, out mimeType, out warnings, out streamIDs);

        string filename = @"filepath\filename.xlsx";

        FileStream stream = File.OpenWrite(filename);
        stream.Write(result, 0, result.Length);
        stream.Close();

        Dts.TaskResult = (int)ScriptResults.Success;

为了使其作为已部署的程序包运行,进行了以下修订
由于部署的程序包无法引用配置文件,因此Web服务的绑定已在脚本任务中编码

using System;
using System.IO;
using ST_ece9b5f6ee774a84a76c32da60affdef.RSExec;

namespace ST_ece9b5f6ee774a84a76c32da60affdef
{


//using System;
using System.Xml;
using System.Text;
using System.Data;
using System.Web;
using Microsoft.SqlServer.Dts.Tasks;
using Microsoft.SqlServer.Dts.Runtime;
//using System.IO;
using System.ServiceModel;
using System.ServiceModel.Security;
using System.Windows.Forms;

/// <summary>
/// ScriptMain is the entry point class of the script.  Do not change the name, attributes,
/// or parent of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{

    public void Main()
    {

        // set Report Name 
        string ReportName = "My Report";


        // all other variables are set from Package Variables
        string ReportDate = Dts.Variables["User::PreviousBusinessDate"].Value.ToString();
        string reportPath = Dts.Variables["User::ReportServerFolder"].Value.ToString() + ReportName;
        string filename = Dts.Variables["User::DestinationFolder"].Value.ToString() + ReportName + "_" + DateTime.Now.ToString("yyyy-MM-dd_hhmmss") + ".xlsx";


        byte[] result = null;
        string format = "EXCELOPENXML"; // "EXCEL"; //"PDF"; 
        string historyID = null;
        string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
        string encoding = String.Empty; //"";
        string mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // Excel2010


        string extension = "";
        Warning[] warnings = null;
        string[] streamIDs = null;
        ParameterValue[] parameters = new ParameterValue[1];
        parameters[0] = new ParameterValue();
        parameters[0].Name = "ReportDate";
        parameters[0].Value = ReportDate;    //"12/11/2013";


        ExecutionInfo execInfo = new ExecutionInfo();
        TrustedUserHeader trustedUH = new TrustedUserHeader();
        ExecutionHeader execHeader = new ExecutionHeader();
        ServerInfoHeader serverInfo = new ServerInfoHeader();

        ReportExecutionServiceSoapClient serviceClient = this.GetServiceClient();

        execHeader = serviceClient.LoadReport(trustedUH, reportPath, historyID, out serverInfo, out execInfo);
        serviceClient.SetExecutionParameters(execHeader, trustedUH, parameters, "en-us", out execInfo);
        serviceClient.Render(execHeader, trustedUH, format, devInfo, out result, out extension, out encoding, out mimeType, out warnings, out streamIDs);


        FileStream stream = File.OpenWrite(filename);
        stream.Write(result, 0, result.Length);
        stream.Close();

        Dts.TaskResult = (int)ScriptResults.Success;
    }

    private ReportExecutionServiceSoapClient GetServiceClient()
    {
        BasicHttpBinding binding = this.GetBinding();
        var address = new EndpointAddress("http://myReportServer/ReportServer/ReportExecution2005.asmx");
        var serviceClient = new ReportExecutionServiceSoapClient(binding, address);
        serviceClient.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential("username", "password");
        serviceClient.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
        return serviceClient;
    }

    private BasicHttpBinding GetBinding()
    {
        var readerQuotas = new XmlDictionaryReaderQuotas()
        {
            MaxDepth = 32,
            MaxStringContentLength = 8192,
            MaxArrayLength = 16384,
            MaxBytesPerRead = 4096,
            MaxNameTableCharCount = 16384
        };

        var transport = new HttpTransportSecurity()
        {
            ClientCredentialType = HttpClientCredentialType.Ntlm,
            ProxyCredentialType = HttpProxyCredentialType.None,
            Realm = string.Empty
        };

        var message = new BasicHttpMessageSecurity()
        {
            ClientCredentialType = BasicHttpMessageCredentialType.UserName,
            AlgorithmSuite = SecurityAlgorithmSuite.Default
        };

        var security = new BasicHttpSecurity()
        {
            Mode = BasicHttpSecurityMode.TransportCredentialOnly,
            Transport = transport,
            Message = message
        };

        var binding = new BasicHttpBinding()
        {
            Name = "ReportExecutionServiceSoap",
            CloseTimeout = TimeSpan.FromMinutes(1),
            OpenTimeout = TimeSpan.FromMinutes(1),
            ReceiveTimeout = TimeSpan.FromMinutes(10),
            SendTimeout = TimeSpan.FromMinutes(1),
            AllowCookies = false,
            BypassProxyOnLocal = false,
            HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
            MaxBufferSize = 524288,
            MaxBufferPoolSize = 524288,
            MaxReceivedMessageSize = 524288,
            MessageEncoding = WSMessageEncoding.Text,
            TextEncoding = Encoding.UTF8,
            TransferMode = TransferMode.Buffered,
            UseDefaultWebProxy = true,
            ReaderQuotas = readerQuotas,
            Security = security
        };

        return binding;
    }


}

}

解决方法:

您不能互换使用XLS和XLSX – 它们是完全不同的格式.

XLS是基于二进制的格式,XLSX是基于XML模式的格式 – 它们将是完全不同的文件类型.您可以自己测试 – 在Excel中创建一个XLS文件,将其重命名为XLSX,当您尝试打开它时它会出错.

SSRS EXCEL格式只能保存到XLS文件中.

SSRS 2012可以导出到XLSX,但您需要使用EXCELOPENXML格式,而不是EXCEL.

标签:c,soap,sql-server,reporting-services,ssis
来源: https://codeday.me/bug/20190529/1176040.html