设置轴标题时出现 COMException 0x800A03EC

发布于 2024-10-14 13:00:06 字数 2171 浏览 3 评论 0原文

通过 COM Interop 将数据导出到 Excel 时,尝试设置 AxisTitle.Text 属性时出现错误(代码 0x800A03EC)。这种情况仅发生在一台装有 Windows 7 x64 Professional 和 Excel 2003 的特定计算机上。我已经在各种不同的 PC(包括 Win7 x64 Professional + Excel 2003)上尝试过,但无法在任何其他计算机上重现该错误。

private static void setAxisTitle(_Chart tChart, string aszTimeUnit)
    {
        Axis tAxis = (Axis)tChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
        try
        {
            tAxis.HasTitle = true;
            tAxis.AxisTitle.Text = "Messwert [um/m]";
            tAxis = (Axis)tChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
            tAxis.HasTitle = true;
            tAxis.AxisTitle.Text = string.Format("Zeit [{0}]", aszTimeUnit);
        }
        catch (Exception aEx)
        {
            cLogger.ErrorFormat("error setting axis title for time unit '{0}' on Axis '{1}'", aszTimeUnit, tAxis.AxisTitle);
            cLogger.Error("error stack trace:", aEx);
            throw;
        }
    }

有人知道如何解决这个困境吗?

编辑: 关于不同文化的话题: 操作系统和 Excel 具有相同的文化。 但是,我(认为我)确实使用以下代码处理可能由此引起的任何问题:

 static Excel2007Export()
    {
        Microsoft.Office.Interop.Excel.Application tExcel = new Application();
        cSystemCulture = Thread.CurrentThread.CurrentCulture;
        cExcelCulture = new CultureInfo(tExcel.LanguageSettings.get_LanguageID(
            Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI));

        try
        {
            Thread.CurrentThread.CurrentCulture = cExcelCulture;
            int tVersion;
            bool tParseSucceded = Int32.TryParse(tExcel.Version.Substring(0, tExcel.Version.IndexOf('.')), out tVersion);

            // 12 is the first version with .xlsx extension
            if (tVersion >= 12)
                cDefaultExtension = ".xlsx";
            else
                cDefaultExtension = ".xls";

        }
        catch (Exception aException)
        {
            cLogger.Debug("error retrieving excel version.", aException);
            cLogger.Error("error retrieving excel version.");
        }
        finally
        {
            Thread.CurrentThread.CurrentCulture = cSystemCulture;
        }
    }

When exporting data to excel via COM Interop I get an error (code 0x800A03EC) when trying to set the AxisTitle.Text property. This happens ONLY on one specific computer with Windows 7 x64 Professional and Excel 2003. I've tried it on a variety of different PCs (incuding Win7 x64 Professional + Excel 2003) but can't reproduce that error on any other machine.

private static void setAxisTitle(_Chart tChart, string aszTimeUnit)
    {
        Axis tAxis = (Axis)tChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
        try
        {
            tAxis.HasTitle = true;
            tAxis.AxisTitle.Text = "Messwert [um/m]";
            tAxis = (Axis)tChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
            tAxis.HasTitle = true;
            tAxis.AxisTitle.Text = string.Format("Zeit [{0}]", aszTimeUnit);
        }
        catch (Exception aEx)
        {
            cLogger.ErrorFormat("error setting axis title for time unit '{0}' on Axis '{1}'", aszTimeUnit, tAxis.AxisTitle);
            cLogger.Error("error stack trace:", aEx);
            throw;
        }
    }

Someone got any idea how to solve this dilemma?

EDIT:
On the topic of differing cultures:
Both OS and Excel have the same culture.
HOWEVER, I (think I) do handle any problems that may arise from this with the following code:

 static Excel2007Export()
    {
        Microsoft.Office.Interop.Excel.Application tExcel = new Application();
        cSystemCulture = Thread.CurrentThread.CurrentCulture;
        cExcelCulture = new CultureInfo(tExcel.LanguageSettings.get_LanguageID(
            Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI));

        try
        {
            Thread.CurrentThread.CurrentCulture = cExcelCulture;
            int tVersion;
            bool tParseSucceded = Int32.TryParse(tExcel.Version.Substring(0, tExcel.Version.IndexOf('.')), out tVersion);

            // 12 is the first version with .xlsx extension
            if (tVersion >= 12)
                cDefaultExtension = ".xlsx";
            else
                cDefaultExtension = ".xls";

        }
        catch (Exception aException)
        {
            cLogger.Debug("error retrieving excel version.", aException);
            cLogger.Error("error retrieving excel version.");
        }
        finally
        {
            Thread.CurrentThread.CurrentCulture = cSystemCulture;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

涙—继续流 2024-10-21 13:00:06

当调用进程具有与 Office 安装相关的不同文化时,有时会发生此错误。如果是这种情况,您可以更改调用线程区域性以匹配 Excel 区域性,如下所示:

CultureInfo MyCulture = new CultureInfo("en-US"); // 这里有你的文化
Thread.CurrentThread.CurrentCulture = MyCulture;

这可能会解决问题。

This error sometimes happen when the calling process has a different culture in respect to the office installation. If this is your case you can change the calling thread culture to match the Excel culture like this:

CultureInfo MyCulture = new CultureInfo("en-US"); // your culture here
Thread.CurrentThread.CurrentCulture = MyCulture;

This could possibly fix the problem.

不必你懂 2024-10-21 13:00:06
For Each Obj As Excel.ChartObject In xlsSheet.ChartObjects()
    ' Obj.Copy()
    If Obj.Name.ToString = "Chart gainloose" Then
        Obj.Chart.ChartArea.Copy()

    End If
    ' Console.WriteLine(Obj.Name)
    ' Console.WriteLine(Obj.TopLeftCell.Row.ToString & " : " & Obj.TopLeftCell.Column.ToString)
Next
For Each Obj As Excel.ChartObject In xlsSheet.ChartObjects()
    ' Obj.Copy()
    If Obj.Name.ToString = "Chart gainloose" Then
        Obj.Chart.ChartArea.Copy()

    End If
    ' Console.WriteLine(Obj.Name)
    ' Console.WriteLine(Obj.TopLeftCell.Row.ToString & " : " & Obj.TopLeftCell.Column.ToString)
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文