2

我正在尝试阅读文本格式,例如通过 Strikethough 属性

myworksheet.Cells[row, col].DisplayFormat.Style.Font.Strikethrough;

但是,无论实际的格式是什么,结果总是错误的。

在此之前,我使用以下方法读取了同一单元格的值:

myworksheet.Cells[row, col].Value;

并得到正确的值。

当我尝试使用调试器读取时,myworksheet.Cells[row, col].DisplayFormat.Style.Font.Strikethrough我收到错误消息。

当我尝试使用调试器读取格式Font并查看其属性时,我得到:

截屏

异常详情:

Strikethrough {System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD)) --- End of inner exception stack trace --- at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at System.Dynamic.IDispatchComObject.GetMembers(IEnumerable`1 names)} System.Reflection.TargetInvocationException

我在 Windows 7 SP1 x64 操作系统上安装了 Office 2010 x86,并且我从 .NET 4.0 项目中引用 Microsoft.Office.Interop.Excel 版本 14。

我使用相同的库以编程方式创建了 .xlsx 文件,并通过 Excel 的 UI 手动添加了文本格式。

通过调试器(监视和立即)访问引发的异常提示库版本可能已过时,但是,版本 14 似乎是正确的版本(http://www.microsoft.com/en-us/download /details.aspx?id=3508)。

我错过了什么?

4

2 回答 2

2

找到原因和解决方法...

显然,格式是每个字符,而不是每个单元格。

检查是否有任何具有删除线格式的单元格有效:

for (int ch = 1; ch <= cell.Characters.Count; ch++)
{
    if (cell.Characters[ch, 1].Font.Strikethrough)
    {
        hasStrikeThrough = true;
        break;
     }
}

但是,这仅适用于单元格的值是一个字符串(例如,如果 value 是 bool 则不起作用)。- 仅适用于字符串,我的意思是对于其他格式我得到一个例外。

于 2012-07-25T12:30:30.710 回答
1

在 VBA 中,您通常只是Cells(r,c).Font直接调用,而不使用 Style。

于 2012-07-25T16:57:52.497 回答