使用Microsoft.Office.Interop.Excel从Excel读取文本格式 [英] Reading text format from Excel using Microsoft.Office.Interop.Excel

查看:181
本文介绍了使用Microsoft.Office.Interop.Excel从Excel读取文本格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to read text format e.g. the Strikethough property via

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

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

However, the result is always false, not matter what the actual formatting is.

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

Right before that, I read the value of the same cell using:

myworksheet.Cells[row, col].Value;

并获得正确的值。

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

When I try using the debugger to read myworksheet.Cells[row, col].DisplayFormat.Style.Font.Strikethrough I receive false.

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

When I try using the debugger to read the format's Font and see its properties I get:

异常详细信息:

删除线{System.Reflection.TargetInvocationException:调用目标抛出异常。 ---> System.Runtime.InteropServices.COMException:旧格式或无效类型库。 (HRESULT的异常:0x80028018(TYPE_E_INVDATAREAD))
---内部异常堆栈跟踪结束---
在System.RuntimeType.InvokeDispMethod(String name,BindingFlags invokeAttr,Object target,Object [] args ,Boolean [] byrefModifiers,Int32 culture,String [] namedParameters)
在System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder binder,Object target,Object [] providedArgs,ParameterModifier [] modifiers,CultureInfo culture, String [] namedParams)
在System.Dynamic.IDispatchComObject.GetMembers(IEnumerable`1名称)} System.Reflection.TargetInvocationException

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

I have Office 2010 x86 installed on a Windows 7 SP1 x64 OS and I'm referencing Microsoft.Office.Interop.Excel version 14 from a .NET 4.0 project.

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

I created the .xlsx file programmatically using the same library and added the text formatting manually via Excel's UI.

访问通过debu抛出的异常gger(观看和即时)提示图书馆版本可能已过期,但是版本14似乎是正确的版本( http://www.microsoft.com/en-us/download/details.aspx?id=3508 )。

The exception thrown by the access via debugger (watch and immediate) hints that the library version may be out of date, however, version 14 seems to be the correct version (http://www.microsoft.com/en-us/download/details.aspx?id=3508).

我缺少什么?

推荐答案

在VBA中,您通常只需调用 cells(r,c).Font 直接,没有Style。

In VBA you'd typically just call Cells(r,c).Font directly, without the Style.

这篇关于使用Microsoft.Office.Interop.Excel从Excel读取文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆