设置轴标题时COMException 0x800A03EC

时间:2011-01-26 15:21:18

标签: c# .net excel com excel-2003

通过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;
        }
    }

有人知道如何解决这个难题吗?

编辑: 关于不同文化的话题: OS和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;
        }
    }

2 个答案:

答案 0 :(得分:1)

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

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

这可能解决问题。

答案 1 :(得分:0)

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