Excel 2007 from C# get_Value always returns -2146826265

寵の児 提交于 2019-12-23 19:04:48

问题


I have a small C# app that references the Microsoft Excel 12.0 Object Library. Among other things, it reads a value from an Excel cell. It reads this value from some older Excel (.xls) files and some 2007 files (.xlsx). The value comes back properly for all .xls files but I only get the following value for .xlsx files: -2146826265.

The cell I'm reading contains a simple formula: "=A1+B1".

This is the line of code I'm using

((Excel.Range)ws.Cells.get_Item(29, 17)).get_Value(null));

Where ws is the current, active worksheet.

Thanks for any help you can offer.


回答1:


The get_value function returns integer for his error message. Here is the list :

-2146826281 = #Div/0!
-2146826246 - #N/A
-2146826259 = #Name?
-2146826288 = #Null!
-2146826252 = #Num!
-2146826265 = #Ref!     <- yours !
-2146826273 = #Value!

And here is a very good article :

http://xldennis.wordpress.com/2006/11/22/dealing-with-cverr-values-in-net-%E2%80%93-part-i-the-problem/

From this very good answer :

How to know if a cell has an error in the formula in C#



来源:https://stackoverflow.com/questions/4166876/excel-2007-from-c-sharp-get-value-always-returns-2146826265

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!