开发者

#value! error in cells containing user defined functions

I need to open an Excel workbook using C# as read-only. No problems there. But if that workbook was saved with autofilter on, I need to turn it off using c# so I can read the worksheet values into my class. The problem is, if a cell has a user-defined function in it, 开发者_JAVA百科that cell will show the #VALUE! error when autofilter is turned off instead of it's correct value. This is not a problem if I manually turn off autofilter just using Excel. So I know there is no problem with the user-defined functions themselves.

Here's what I am doing:

   Excel.Application app = new Excel.Application();
   Excel.Workbook CIRworkbook;
   var missing = Missing.Value;

   bool openAsReadOnly = true;
   CIRworkbook = (Excel.Workbook)(app.Workbooks.Open(xlsFile, missing, openAsReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing));

   app.get_Range("A:IV", missing).Worksheet.AutoFilterMode = false;
   app.CalculateFull();

The app.CalculateFull() line has no effect on removing the #VALUE! error.


Try using the following syntax to get all the cells that contains values in the sheet regardless of the auto-filter on/off or the actual filter used.

I think this is what you're looking for:

Worksheet currSheet = excelApp.ActiveWorkbook.ActiveSheet;
object[,] objData = (object[,])currSheet.UsedRange.Value2;

the Value2 member holds the underline content of the cell/Range. if you're looking for the visible string displayed to the user, use Text property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜