VS2013中Winform导出Excel文件时报“object”未包含“get_Range”的定义解决方法
来源:互联网 发布:校园网 虚拟机 ubuntu 编辑:程序博客网 时间:2024/04/19 11:33
由于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]];
在VS2013中调用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
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的实现
- VS2013中Winform导出Excel文件时报“object”未包含“get_Range”的定义解决方法
- 写入EXCEL “object”未包含“get_Range”的定义
- asp.net 将数据导入到excel中 出现 “object”未包含“get_Range”的定义
- C#将数据导入到excel中 出现 “object”未包含“get_Range”的定义报错
- C#将数据导入到excel中 出现 “object”未包含“get_Range”的定义
- “object”未包含“get_Range”的定义
- object 未包含get_range的定义
- Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: “object”未包含“get_Range”的定义
- asp.net 导出EXCEL 报错未包含 get_Range 的定义
- E小cel 异常 “object”未包含“get_Range”的定义 解决方案
- IFeatureBuffer.set_Value出现错误:“object”未包含“set_Value”的定义
- Resources中未包含。。。的定义
- winform 导出excel文件
- WinForm中导出Excel
- winForm中C#实现Excel的导出
- 在VS2010中调用COM Interop DLL操作Excel通过get_Range去获取Range时,会发生Object does not contain a definition for get_Range的错误。其原因和解决方案
- [C#]WinForm中DataGrid扩展 - 快速导出Excel文件
- [C#]WinForm中DataGrid扩展 - 快速导出Excel文件
- Android shape的使用实现圆角,虚线,渐变等效果
- 华为C8813Q刷机包 基于乐蛙OS5 优化内核 提升性能 稳定流畅
- WEB开发初学者
- Mysql查出前10位消费次数小于2的会员(聚焦面试题)
- 14周阅读程序部分
- VS2013中Winform导出Excel文件时报“object”未包含“get_Range”的定义解决方法
- Mysql_JDBC查询操作
- ElasticSearch入门笔记
- php面试题
- 腾讯微薄分享一(授权)
- 【解决Hiberate】 query specified join fetching, but the owner of the fetched association..
- IOS 内存管理
- 模板类声明和定义为何要写一起
- mysql中如何删外键