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

其他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.kongwei521VS2013中Winform导出Excel文件时报“object”未包含“get_Range”的定义解决方法,2014-5.

object does not contain a definition for get_range的更多相关文章

  1. .NET的ExcelOperate

    using System; using System.Web; using Excel = Microsoft.Office.Interop.Excel; namespace Comm { /// & ...

  2. JavaBean,POJO,VO,DTO的区别和联系

    JavaBeans A JavaBean is a class that follows the JavaBeans conventions as defined by Sun. Wikipedia ...

  3. Python函数参数默认值的陷阱和原理深究"

    本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...

  4. CMSIS OS None

    /* ---------------------------------------------------------------------- * Copyright (C) 2011 ARM L ...

  5. Watch out for these 10 common pitfalls of experienced Java developers & architects--转

    原文地址:http://zeroturnaround.com/rebellabs/watch-out-for-these-10-common-pitfalls-of-experienced-java- ...

  6. 小菜鸟学 Spring-bean scope (一)

    this information below just for study record of mine. 默认情况下:Spring 创建singleton bean 以便于错误能够被发现. 延迟加载 ...

  7. Troubleshooting 'library cache: mutex X' Waits.

    What is a 'library cache: mutex X' wait? The mutex feature is a mechanism to control access to in me ...

  8. CMSIS RTOS -- embOS segger

    #ifndef __CMSIS_OS_H__ #define __CMSIS_OS_H__ #include <stdint.h> #include <stddef.h> #i ...

  9. spring--事务原理

    Spring支持以下7种事务传播行为. 传播行为 XML文件 propagation值 含义 PROPAGATION_REQUIRED REQUIRED 表示当前方法必须在一个具有事务的上下文中运行. ...

随机推荐

  1. java消息队列

    来个个人通俗的解释吧.消息队列,顾名思义 首先是个队列.队列的操作有入队和出队 也就是你有一个程序在产生内容然后入队(生产者) 另一个程序读取内容,内容出队(消费者) 我想你应该是缺乏一个使用场景. ...

  2. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-003使用@AttributeOverrides

    Each @AttributeOverride for a component property is “complete”: any JPA or Hibernate annotation on t ...

  3. 图解TCP/IP读书笔记(三)

    第三章.数据链路 数据链路层是计算机网络最基本的内容. 数据链路层的协议定义了通过通信媒介互连的设备之间传输的规范. 一.数据链路相关技术 1.MAC地址 关于MAC地址的几个要点: ①MAC地址长度 ...

  4. Android:开发环境

    一.JAVA SDK(JDK)的安装 http://www.cnblogs.com/tinyphp/p/3664598.html 二.ADT-Bundle 包含了Eclipse.ADT插件和SDK T ...

  5. CentOS7安装配置DNS服务器

    准备工作(假设名称为bigcloud.local) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #更改主机名称 #vi /etc/sysconfig/netwo ...

  6. Java:Collection集合类

    特点:只能用于存储对象,集合长度时可变的,集合可以存储不同类型的对象. 数组和集合类同时容器,不同的是: 1.数组虽然也可以存储对象,但是长度是固定的:集合长度时可变的. 2.数组中可以存储基本数据类 ...

  7. Socket基础编程

    地址结构sockaddr_in 其中包含:IP地址,端口号,协议族推荐使用sockaddr_in,而不建议使用sockaddrsockaddr_in与sockaddr是等价的,但sockaddr_in ...

  8. lib-qqwry v1.0 发布 nodejs解析纯真IP库(qqwry.dat)

    lib-qqwry是当初学习node时用来练手的一个模块,用来解析纯真IP库的 现在发一个v1.0版本弥补我当时稚嫩的代码. 意外收获是,整理代码后发现,相比v0.x版本 急速模式下的效率提升大概20 ...

  9. AE 栅格处理

    由RasterDataset得到RasterLayer RasterDataset->RasterLayer IRasterLayer pRasterLayer = new RasterLaye ...

  10. win7下制作ubuntu系统安装启动盘和U盘安装ubuntu全过程

    在我搞坏了两个系统之后,一切都得从头开始了,这回好了,电脑就是一台裸机了.没办法,重新下win7吧.这个要先做一个win7的启动盘,然后再安装,只能说我技术不行,没能把win7搞定.让大神给装的win ...