asp.net 将自定义区域性与ReportViewer一起使用将返回CultureNotFoundException

sqougxex  于 2023-10-21  发布在  .NET
关注(0)|答案(4)|浏览(134)

我的网站使用如下创建的自定义区域性:

var cib = new CultureAndRegionInfoBuilder("en-gb-xxxx", CultureAndRegionModifiers.None);
cib.LoadDataFromCultureInfo(new CultureInfo("en-gb"));
cib.LoadDataFromRegionInfo(new RegionInfo("en"));
cib.Register();

它们的用法是这样的:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-gb-xxxx");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-gb-xxxx");

在我想用ReportViewer控件呈现报表之前,一切都工作得很好。报告文本呈现正常,但图像失败,我得到以下错误:

  • System.Globalization.CultureNotFoundException:不支持区域性。参数名称:区域性4096(0x1000)是无效的区域性标识符 *

经过大量的谷歌搜索,我发现很多其他人问同样的问题,不幸的是总是没有答案。This article非常清楚地描述了问题,但没有提供任何解决方案。
有没有人可以提供,如果不是一个修复,一个变通办法?

oxosxuxt

oxosxuxt1#

由于没有答案,我不得不通过我的方式:
首先,我创建了一个CustomReportViewer,它继承了原生Microsoft.Reporting.WebForms.ReportViewer,覆盖了OnPreRender方法。它检查已登录用户的区域性(保存在会话中),并回滚到父区域性(如果有)。

protected override void OnPreRender(EventArgs e)
{
    var culture = new CultureInfo(HttpContext.Current.Session["UserCulture"]);
    if (culture.CompareInfo.LCID != culture.LCID){
        var baseCulture = new CultureInfo(culture.CompareInfo.LCID);
        System.Threading.Thread.CurrentThread.CurrentCulture = baseCulture;
        System.Threading.Thread.CurrentThread.CurrentUICulture = baseCulture;
    }
    base.OnPreRender(e);
}

然后我改变了aspx页面中的对象:

<web:CustomReportViewer ID="ReportViewer1" runat="server" AsyncRendering="false" SizeToReportContent="true" ProcessingMode="Remote">

这就对了
我不接受这个答案,希望有人能想出点什么来...吸尘器

qncylg1j

qncylg1j2#

我找到了解决上述问题的方法:

问题:

在报表查看器中,导出时不支持区域性。参数名称:区域性4096(0x 1000)是无效的区域性标识符。

解决方案:

1.)在顶部添加下面的指令

<% @Import Namespace="System.Globalization" %> 
<% @Import Namespace="System.Threading" %>

using System.Threading;
using System.Globalization;

2.)添加事件如下:

protected void FPReportViewer_PreRender(object sender, EventArgs e)
{
    CultureInfo ci = new CultureInfo("en-US");
    Thread.CurrentThread.CurrentCulture = ci;
    Thread.CurrentThread.CurrentUICulture = ci;
}

3.)更新Reportviewer html标记如下:

<rsweb:ReportViewer runat="server" ID="FPReportViewer" OnPreRender="FPReportViewer_PreRender" AsyncRendering="false" width="100%"  SizeToReportContent="True"> </rsweb:ReportViewer>

这段代码可能会帮助你。
非常感谢!

0s7z1bwu

0s7z1bwu3#

在发送到SSRS之前尝试此操作

Thread.CurrentThread.CurrentCulture =
        Thread.CurrentThread.CurrentUICulture =
            CultureInfo.DefaultThreadCurrentCulture =
                CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo("en-US");

例如:

[SessionExpire]
        public ActionResult ReportProduct(string ExpenseCenter)
        {                   
            Thread.CurrentThread.CurrentCulture =
                Thread.CurrentThread.CurrentUICulture =
                    CultureInfo.DefaultThreadCurrentCulture =
                        CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo("en-US");

            ReportViewer reportViewer = new ReportViewer();
            reportViewer.ProcessingMode = ProcessingMode.Remote;
            reportViewer.SizeToReportContent = true;
            reportViewer.AsyncRendering = false;

            reportViewer.ZoomMode = ZoomMode.FullPage;
            reportViewer.Width = Unit.Percentage(100);
            reportViewer.Height = Unit.Percentage(100);

            var serverReport = reportViewer.ServerReport;

            serverReport.ReportPath = "/Reports/ReportPrice";
            serverReport.ReportServerUrl = new Uri("http://ssrs-server:94/ReportServer_MSSQLSERVER2016/");
            serverReport.SetParameters(GetParametersServer());
            serverReport.Refresh();

            ViewBag.ReportViewer = reportViewer;

            return View();
        }
czq61nw1

czq61nw14#

然后,当您不使用ReportViewer直接将rdlc报表渲染/导出为其他格式时,会有以下答案:Culture of thread for method render of LocalReport

相关问题