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的。
/// <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.
object does not contain a definition for get_range的更多相关文章
- .NET的ExcelOperate
using System; using System.Web; using Excel = Microsoft.Office.Interop.Excel; namespace Comm { /// & ...
- JavaBean,POJO,VO,DTO的区别和联系
JavaBeans A JavaBean is a class that follows the JavaBeans conventions as defined by Sun. Wikipedia ...
- Python函数参数默认值的陷阱和原理深究"
本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...
- CMSIS OS None
/* ---------------------------------------------------------------------- * Copyright (C) 2011 ARM L ...
- 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- ...
- 小菜鸟学 Spring-bean scope (一)
this information below just for study record of mine. 默认情况下:Spring 创建singleton bean 以便于错误能够被发现. 延迟加载 ...
- 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 ...
- CMSIS RTOS -- embOS segger
#ifndef __CMSIS_OS_H__ #define __CMSIS_OS_H__ #include <stdint.h> #include <stddef.h> #i ...
- spring--事务原理
Spring支持以下7种事务传播行为. 传播行为 XML文件 propagation值 含义 PROPAGATION_REQUIRED REQUIRED 表示当前方法必须在一个具有事务的上下文中运行. ...
随机推荐
- 错误:error libGL.so: cannot open shared object file: No such file or directory
Failed to load libGL.soerror libGL.so: cannot open shared object file: No such file or directory 启动e ...
- Struts2笔记——通配符和动态方法调用
通配符映射 * 一个 Web应用可能有成百上千个 action 声明. 可以利用 struts提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系 * 通配符映射规则 > 若 ...
- linux shell 命令学习(3) split - split a file into pieces
split 用来进行文件分割的指令 split [OPTION]... [INPUT [PREFIX]] 发现这个命令是因为有个需求,有个10W行的文本文件,需要分成5个2w行的文本文件, 查了一下资 ...
- ORACLE基本语法
ORACLE基本语法 一.ORACLE的启动和关闭1.在单机环境下要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下su - oraclea.启动ORACLE系统oracle>s ...
- 关于MySQL
DBMS - 数据库管理系统(Database Management System) RDBMS - 关系数据库管理系统(Relational Database Management System) ...
- win8找到程序员计算器
最近想用计算器的十进制和十六进制转化的功能,发现win8没有开始菜单了,从网上查了查,原来指令如此简单,特此做笔记,谨防忘记! 操作:win+r打开运行,输入calc,确定就出来了!
- VMware中第一次启动mac遇到的错误及解决方案
本文部分参考 http://blog.sina.com.cn/s/blog_938d86e90100z5ep.html 虚拟机版本:VMware-workstation-full-7.1.3-3242 ...
- 在AChartEngine上绘图,手指标记当前位置
最近要做一个绘图项目,需要在ACE折线图上再绘出一条红标记当前坐标,经过这几天研究,可以给大家分享一下了.先上效果图吧! 代码里的注释还是比较清楚,就不作说明了. package com.exampl ...
- 《OD学算法》常用算法集合
1. 排序 (1)冒泡 (2)选择 (3)插入 (4)归并 2. 位运算 Bitmask provide an efficient way to manipulate a small set of B ...
- hdu 1226 超级密码(bfs+余数判重)
题意:略过 分析:用m个数字组成一个能够被n整除的c进制数的最小值,实际上本题的关键就在于这个最小值上. 首先确定我们的思路是从小到大寻找.先查看一位数,即查看着m个数字是否能被n整除:若不能,就查 ...