.net 在WebApiCall的HtmlResponseMessage中返回时,由ReportingService编码问题创建的xls

.net 在WebApiCall的HtmlResponseMessage中返回时,由ReportingService编码问题创建的xls,.net,asp.net-web-api,reporting-services,encoding,xls,.net,Asp.net Web Api,Reporting Services,Encoding,Xls,我在导出xls文件时遇到问题。由于其结果,这似乎是一个编码问题: 我已经尝试过指定不同的编码(UTF-8、UTF-16),但似乎都不起作用。我们使用Reporting Services创建xls文件,仅在目录中保存时,xls文件是正确的 PS:xls文件有图像 响应标题: Access-Control-Allow-Credentials:true Cache-Control:no-cache Connection:keep-alive Content-Disposition:at

我在导出xls文件时遇到问题。由于其结果,这似乎是一个编码问题:

我已经尝试过指定不同的编码(UTF-8、UTF-16),但似乎都不起作用。我们使用Reporting Services创建xls文件,仅在目录中保存时,xls文件是正确的

PS:xls文件有图像


响应标题:

Access-Control-Allow-Credentials:true  
Cache-Control:no-cache  
Connection:keep-alive  
Content-Disposition:attachment; filename=Test.xls  
Content-Length:901056  
**Content-Type:application/vnd.ms-excel**  
Date:Thu, 12 May 2016 11:55:48 GMT  
Expires:-1  
Pragma:no-cache  
Server:Microsoft-IIS/8.5  
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET

公共HttpResponseMessage PostExport() { var reportingservicecextractor=新的reportingservicecextractor(ConfigurationManager.AppSettings[“reporting.services.name.cart”],FormatExtractor.Excel); var userInfo=_operationServices.userInfo(); var user=_userRepo.GetUserRolesByEmail(userInfo.email); var参数=新列表 { 新参数值 { Name=“User”, Value=user.Id.ToString() } }; 字符串mimeType=null; 字符串编码=空; var extract=reportingservicecextractor.extract(参数、输出mimeType、输出编码); var结果=新的HttpResponseMessage(HttpStatusCode.OK); 结果.内容=新的流内容(摘录); result.Content.Headers.ContentType=新的MediaTypeHeaderValue(mimeType); Add(“内容处置”,“附件;文件名=Test.xls”); result.Content.Headers.Add(“内容编码”,编码); 返回结果; }
公共流提取(ICollection参数值、输出字符串mimeType、输出字符串编码)
{
使用(var reportExecutionServiceSoapClient=new reportExecutionServiceSoapClient())
{
reportExecutionServiceSoapClient.ClientCredentials.Windows.ClientCredential=\u凭据;
reportExecutionServiceSoapClient.ClientCredentials.Windows.AllowedImpersonationLevel=
TokenImpersonationLevel.Impersonation;
字节[]结果=空;
字符串reportPath=$“/{{u reportName}”;
字符串格式=_formatExtractor.ToString();
常量字符串devInfo=@“False”;
var ExecutionInfo=新的ExecutionInfo();
var execHeader=new ServerInfoHeader();
尝试
{
var executionHeader=reportExecutionServiceSoapClient.LoadReport(null,reportPath,null,out executeheader,
执行信息);
reportExecutionServiceSoapClient.SetExecutionParameters(executionHeader,null,parameterValues.ToArray(),
“en-us”,out-execInfo);
字符串扩展;
警告[]警告=null;
字符串[]streamIDs=null;
reportExecutionServiceSoapClient.Render(executionHeader,null,format,devInfo,
输出结果、输出扩展、输出mimeType、输出编码、输出警告、输出流ID);
}
捕获(例外情况除外)
{
Log.Error($“{ex}”);
投掷;
}
MemoryStream stream=新的MemoryStream(result.Length);
stream.Write(result,0,result.Length);
流位置=0;
回流;
}
    public HttpResponseMessage PostExport()
    {
        var reportingServiceExtractor = new ReportingServiceExtractor(ConfigurationManager.AppSettings["reporting.services.name.cart"], FormatExtractor.Excel);
        var userInfo = _operationServices.userInfo();

        var user = _userRepo.GetUserRolesByEmail(userInfo.email);

        var parameters = new List<ParameterValue>
        {
            new ParameterValue
            {
                Name = "User",
                Value = user.Id.ToString()
            }
        };

        string mimeType = null;
        string encoding = null;

        var extract = reportingServiceExtractor.Extract(parameters, out mimeType, out encoding);

        var result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new StreamContent(extract);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);
        result.Content.Headers.Add("Content-Disposition", "attachment;filename=Test.xls");
        result.Content.Headers.Add("Content-Encoding", encoding);

        return result;
    }
    public Stream Extract(ICollection<ParameterValue> parameterValues, out string mimeType, out string encoding)
    {
        using (var reportExecutionServiceSoapClient = new ReportExecutionServiceSoapClient())
        {
            reportExecutionServiceSoapClient.ClientCredentials.Windows.ClientCredential = _credentials;
            reportExecutionServiceSoapClient.ClientCredentials.Windows.AllowedImpersonationLevel =
                TokenImpersonationLevel.Impersonation;

            byte[] result = null;
            string reportPath = $"/{_reportName}";
            string format = _formatExtractor.ToString();

            const string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

            var execInfo = new ExecutionInfo();
            var execHeader = new ServerInfoHeader();

            try
            {
                var executionHeader = reportExecutionServiceSoapClient.LoadReport(null, reportPath, null, out execHeader,
                out execInfo);


                reportExecutionServiceSoapClient.SetExecutionParameters(executionHeader, null, parameterValues.ToArray(),
                    "en-us", out execInfo);

                string extension;
                Warning[] warnings = null;
                string[] streamIDs = null;

                reportExecutionServiceSoapClient.Render(executionHeader, null, format, devInfo,
                    out result, out extension, out mimeType, out encoding, out warnings, out streamIDs);
            }
            catch (Exception ex)
            {
                Log.Error($"{ex}");
                throw;
            }
            MemoryStream stream = new MemoryStream(result.Length);
            stream.Write(result, 0, result.Length);
            stream.Position = 0;

            return stream;
        }