原因[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的。

     /// <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.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. 重温《js权威指南》 第4、5、6章

    第四章 表达式和运算符         4.2 对象和数组的初始化表达式                数组: []   [3,7] [1+2,3+4] [[1,2,3,],[4,5,6],[7,8, ...

  2. Android核心分析之二十三Andoird GDI之基本原理及其总体框架

     Android GDI基本框架 在Android中所涉及的概念和代码最多,最繁杂的就是GDI相关的代码了.但是本质从抽象上来讲,这么多的代码和框架就干了一件事情:对显示缓冲区的操作和管理. GDI主 ...

  3. Android 核心分析 之七Service深入分析

    Service深入分析 上一章我们分析了Android IPC架构,知道了Android服务构建的一些基本理念和原理,本章我们将深入分析Android的服务.Android体系架构中三种意义上服务: ...

  4. STM32的GPIO口的输出开漏输出和推挽输出

    本文来自cairang45的博客,讲述了STM32的GPIO口的输出开漏输出和推挽输出, 作者博客:http://blog.ednchina.com/cairang45 本文来自: 高校自动化网(Ww ...

  5. Struts2笔记——struts常用标签

    使用struts标签前,首先要配置struts2架构,然后导入标签库,jsp插入如下语句: <%@taglib uri="/struts-tags" prefix=" ...

  6. LightOj 1245 --- Harmonic Number (II)找规律

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1245 题意就是求 n/i (1<=i<=n) 的取整的和这就是到找规律的题 ...

  7. TEET

    [{"PROCESS_STORE_TIME":"3min 11s","PROCESS_GET_FILE_TIME":"3min&q ...

  8. Linux-0.00运行环境搭建【转】

    转自:http://blog.csdn.net/rosetta/article/details/8933240 这里的Linux-0.00由Linus Torvalds写的Linux最初版本,只是打印 ...

  9. Eclipse下运行Maven项目提示缺少maven-resources-plugin:2.4.3

    将一个手动创建的Maven项目(命令行下可正常运行)导入到Eclipse中,运行时提示这样的错误信息:[ERROR] Plugin org.apache.maven.plugins:maven-res ...

  10. asp开发微信扫码支付

    这个任务已经给了.现在正在学习开发中.主要注意的是2点. 1:返回参数的验证. 2:通知后业务处理和处理后返回财付通.大部分操作,api中已经处理好. 现在需要的业务逻辑部分. 需要正确3个参数  r ...