object does not contain a definition for get_range

简介:

原因[1]

在VS2012中调用COM Interop DLL操作Excel通过get_Range去获取Range时,会发生Object does not contain a definition for get_Range的错误。其原因和解决方案:

Misha's explanation is correct - when using No PIA, methods returning object are treated as if they return dynamic in order to simulate the VBA semantics of COM Variants. Because the return value of sh.Cells is Object, sh.get_Range is dispatched dynamically, and the dynamic COM binder does not support the get_Range syntax exposed in C# before indexed properties were supported. We've tried to maintain backwards compatibility wherever possible when you turn on Embed Interop Types for a COM reference, but this is one place where some further tweaking is required.


The workaround you proposed will work - a cleaner workaround is the one Mike Rosenblum pointed out to use C# 4.0's new indexed properties syntax, which the dynamic COM binder does understand. You can then represent the operation with the following code:

Excel.Range r = sh.Range[sh.Cells[1, 1], sh.Cells[2, 2]];

See Also
get_Range method missing with embedded interop assembly

 

具体来讲[2]

由于Framework版本不同,因此支持的也不一样

例如:
在 .NET Framework 3.5 語法

Excel.Range r = sh.Range(sh.Cells[1, 1], sh.Cells[2, 2]);
在 .NET Framework 4.0-4.5 改用
Excel.Range r = sh.Range[sh.Cells[1, 1], sh.Cells[2, 2]];

 

winform导出Excel代码:

使用方法:    ExportExcel("条形码数据一览", GetSearchData);//GetSearchData可以传datatable 或者datagridview下面代码是传递Datatable的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/// <summary> 
           /// 查询的数据导出为Excel 
           /// </summary> 
           /// <param name="fileName">导出文件名</param> 
           /// <param name="myDGV">导出的datatable数据</param> 
           private  void  ExportExcel( string  fileName, MDataTable myDGV) 
          
                string  saveFileName =  ""
                SaveFileDialog saveDialog =  new  SaveFileDialog(); 
                saveDialog.DefaultExt =  "xls"
                saveDialog.Filter =  "Excel文件|*.xls"
                saveDialog.FileName = fileName; 
                saveDialog.ShowDialog(); 
                saveFileName = saveDialog.FileName; 
                if  (saveFileName.IndexOf( ":" ) < 0)  return //被点了取消 
                Microsoft.Office.Interop.Excel.Application xlApp =  new  Microsoft.Office.Interop.Excel.Application(); 
                if  (xlApp ==  null
               
                     MessageBox.Show( "无法创建Excel对象,可能您的机子未安装Excel" ); 
                     return
               
   
                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; 
                Microsoft.Office.Interop.Excel.Workbook workbook = 
  workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); 
                Microsoft.Office.Interop.Excel.Worksheet worksheet = 
  (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; //取得sheet1 
   
   
                //写入标题 
                for  ( int  i = 0; i < myDGV.Columns.Count; i++) 
               
                     worksheet.Cells[1, i + 1] = myDGV.Columns[i].ColumnName; 
                     //标题 
                     Microsoft.Office.Interop.Excel.Range titleRange = worksheet.Range[worksheet.Cells[1, 1],  
worksheet.Cells[1, i + 1]]; //选中标题 
                     titleRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;  //水平居中 
               
                //写入数值 
                for  ( int  r = 0; r < myDGV.Rows.Count; r++) 
               
                     for  ( int  i = 0; i < myDGV.Columns.Count; i++) 
                    
                          worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r][i].Value; 
                          //设置边框 
                          Microsoft.Office.Interop.Excel.Range allRange = worksheet.Range[worksheet.Cells[1, 1],  
worksheet.Cells[r + 1, i + 1]]; 
                          allRange.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; 
                    
                     System.Windows.Forms.Application.DoEvents(); 
               
                //设置最后一行边框 
                Microsoft.Office.Interop.Excel.Range endRange = worksheet.Range[worksheet.Cells[1, 1],  
worksheet.Cells[myDGV.Rows.Count + 1,myDGV.Columns.Count]]; 
                endRange.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; 
                worksheet.Columns.EntireColumn.AutoFit(); //列宽自适应 
                if  (saveFileName !=  ""
               
                     try 
                    
                          workbook.Saved =  true
                          workbook.SaveCopyAs(saveFileName); 
                    
                     catch  (Exception ex) 
                    
                          MessageBox.Show( "导出文件时出错,文件可能正被打开!\n"  + ex.Message); 
                    
   
               
                xlApp.Quit(); 
                GC.Collect(); //强行销毁 
                MessageBox.Show( "文件: "  + fileName +  ".xls 保存成功" "信息提示" , MessageBoxButtons.OK, MessageBoxIcon.Information); 
          

 

其他DataGridview导出Excel的资料:

http://www.360doc.com/content/11/1211/17/8147167_171489298.shtml  winform应用使用DataGridView数据导出到Excel

http://ruantnt.blog.163.com/blog/static/190525452201110185199346/  winform(c#) DataGridView导出Excel 
http://hi.baidu.com/wenshangang/item/1227f415fab1a35a2a3e229f  Winform中dataGridView控件导出到excel表格中

http://blog.sina.com.cn/s/blog_62cd5a980101905a.html  WinForm中DataGridView导出为Excel(快速版)

http://www.cr173.com/html/7906_1.html  WinForm下DataGridView导出Excel的实现

 

 

参考文章

1. ST@N 原文地址 How to: 解决 Object does not contain a definition for get_Range.

2.kongwei521, VS2013中Winform导出Excel文件时报“object”未包含“get_Range”的定义解决方法,2014-5.

 

 

 

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。




    本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/5787411.html ,如需转载请自行联系原作者


相关文章
|
6月前
|
JavaScript
[Vue warn]: Method “components“ has type “object“ in the component definition. Did you reference the
[Vue warn]: Method “components“ has type “object“ in the component definition. Did you reference the
|
JavaScript
[Vue warn]: Method “components“ has type “object“ in the component definition. Did you reference the
[Vue warn]: Method “components” has type “object” in the component definition. Did you reference the
[Vue warn]: Method “components“ has type “object“ in the component definition. Did you reference the
|
JavaScript 前端开发
vue-watch报错[[Vue warn]: Method “watch“ has type “object“ in the component definition. Did you refer]
[Vue warn]: Method “watch” has type “object” in the component definition. Did you reference the function correctly?
1466 0
vue-watch报错[[Vue warn]: Method “watch“ has type “object“ in the component definition. Did you refer]
|
4天前
|
存储 Java 程序员
Java基础的灵魂——Object类方法详解(社招面试不踩坑)
本文介绍了Java中`Object`类的几个重要方法,包括`toString`、`equals`、`hashCode`、`finalize`、`clone`、`getClass`、`notify`和`wait`。这些方法是面试中的常考点,掌握它们有助于理解Java对象的行为和实现多线程编程。作者通过具体示例和应用场景,详细解析了每个方法的作用和重写技巧,帮助读者更好地应对面试和技术开发。
32 4
|
1月前
|
Java
Java Object 类详解
在 Java 中,`Object` 类是所有类的根类,每个 Java 类都直接或间接继承自 `Object`。作为所有类的超类,`Object` 定义了若干基本方法,如 `equals`、`hashCode`、`toString` 等,这些方法在所有对象中均可使用。通过重写这些方法,可以实现基于内容的比较、生成有意义的字符串表示以及确保哈希码的一致性。此外,`Object` 还提供了 `clone`、`getClass`、`notify`、`notifyAll` 和 `wait` 等方法,支持对象克隆、反射机制及线程同步。理解和重写这些方法有助于提升 Java 代码的可读性和可维护性。
|
6月前
|
Java
Java Object 类
5月更文挑战第16天
|
3月前
|
Java
【Java基础面试二十】、介绍一下Object类中的方法
这篇文章介绍了Java中Object类的常用方法,包括`getClass()`、`equals()`、`hashCode()`、`toString()`、`wait()`、`notify()`、`notifyAll()`和`clone()`,并提到了不推荐使用的`finalize()`方法。
【Java基础面试二十】、介绍一下Object类中的方法
|
2月前
|
Python
类与面向对象编程(Object-Oriented Programming, OOP)
类与面向对象编程(Object-Oriented Programming, OOP)
|
3月前
|
前端开发 Java 编译器
【前端学java】java中的Object类和前端中的Object有什么区别(9)
【8月更文挑战第10天】java中的Object类和前端中的Object有什么区别
41 0
【前端学java】java中的Object类和前端中的Object有什么区别(9)
|
3月前
|
算法 Java
12 Java常用类(一)(内部类+object类+包装类)
12 Java常用类(一)(内部类+object类+包装类)
35 5
下一篇
无影云桌面