设置轴标题时出现 COMException 0x800A03EC
通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当调用进程具有与 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.