“object”未包含“get_Range”的定义

a2562447 2012-03-24 02:15:02
各位大大 VS2010 EXCEL2003 版本 想把DATAGRIDVIEW数据导出成EXCEL 代码如下:
public static void GetStudentExcel(System.Windows.Forms.DataGridView dgvStuInfo)
{
Application excel = new Application();
excel.SheetsInNewWorkbook = 1;
excel.Workbooks.Add();

excel.Cells[1, 1] = "学号";
excel.Cells[1, 2] = "密码";
excel.Cells[1, 3] = "姓名";
excel.Cells[1, 4] = "性别";
excel.Cells[1, 5] = "年级";
excel.Cells[1, 6] = "电话";
excel.Cells[1, 7] = "地址";
excel.Cells[1, 8] = "出生日期";
excel.Cells[1, 9] = "邮箱";
//获取标题行的单元格
Range range = excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 10]);
//设置字体加粗
range.Font.Bold = true;
//设置字体颜色
range.Font.ColorIndex = 0;
//设置颜色背景
range.Interior.ColorIndex = 15;
//设置边框样式
range.Borders.LineStyle = XlLineStyle.xlContinuous;

//循环DateGridView中的数据赋值到Excel中
int i = 0, j = 0;
for (i = 0; i < dgvStuInfo.Rows.Count; i++)
{
for (j = 0; j < 3; j++)
{
excel.Cells[i + 2, j + 1] =
dgvStuInfo.Rows[i].Cells[j].Value.ToString();
}
//设置性别
excel.Cells[i + 2, 4] =
dgvStuInfo.Rows[i].Cells["Sex"].Value.ToString()
== "False" ? "男" : "女";

//设置显示的学生年级
System.Windows.Forms.DataGridViewComboBoxCell dgvCbo = (System.Windows.Forms.DataGridViewComboBoxCell)
dgvStuInfo.Rows[i].Cells["GradeId"];
excel.Cells[i + 2, 5] = dgvCbo.FormattedValue.ToString();

for (j = 5; j < 10; j++)
{
excel.Cells[i + 2, j + 1] =
dgvStuInfo.Rows[i].Cells[j].Value.ToString();
}
}
excel.get_Range(excel.Cells[2, 8], excel.Cells[i + 2, 8]).NumberFormat = "yyyy-m-d";

//显示当前窗口
excel.Visible = true;
}
总是说未包含get_Range的定义
求解决
...全文
3846 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
levis_me 2014-09-20
  • 打赏
  • 举报
回复
与.net版本2.0、Office.Core及Microsoft.Office.Interop.Excel设置复制到本地为True,可解决楼主问题。
yunhewangzi 2014-09-10
  • 打赏
  • 举报
回复
非常感谢!谢谢!
yunhewangzi 2013-10-19
  • 打赏
  • 举报
回复
谢谢!非常感谢!
tongfu204 2013-09-15
  • 打赏
  • 举报
回复
膜拜2楼和6楼大神!!
waterfisher2012 2013-04-24
  • 打赏
  • 举报
回复
引用 6 楼 gyb333 的回复:
引用 2 楼 a535571592 的回复: Range range = excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 10]); 把这句话改成 Range range = excel.Range[excel.Cells[1, 1], excel.Cells[1, 10]]; 就OK了 这个注意“[”中括号就行了
真NB
hutao93 2013-03-06
  • 打赏
  • 举报
回复
把对操作excel添加的程序集的属性;嵌入式操作改为False就行了
gyb333 2012-12-20
  • 打赏
  • 举报
回复
引用 2 楼 a535571592 的回复:
Range range = excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 10]); 把这句话改成 Range range = excel.Range[excel.Cells[1, 1], excel.Cells[1, 10]]; 就OK了
这个注意“[”中括号就行了
devmiao 2012-10-16
  • 打赏
  • 举报
回复
你是反编译的代码?
换ilspy试试看,用.NET Reflector的时候,不要忽略引用的库,版本也要匹配。

这个问题的本质是,事实上C#定义的属性,在IL层面上是对应一个 get_属性名 和一个 set_属性名 的方法。
raaaazer 2012-10-16
  • 打赏
  • 举报
回复
Excel.Range c1 = oSheet.Cells[1, 1];
Excel.Range c2 = oSheet.Cells[4, 4];
oRange = (Excel.Range)oSheet.get_Range(c1, c2);
kyo412466275 2012-10-16
  • 打赏
  • 举报
回复
三楼的,我试过没用,直接报错。
a535571592 2012-06-07
  • 打赏
  • 举报
回复
Range range = excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 10]);
把这句话改成
Range range = excel.Range[excel.Cells[1, 1], excel.Cells[1, 10]];
就OK了
mathieuxiao 2012-03-24
  • 打赏
  • 举报
回复
public static void GetStudentExcel(System.Windows.Forms.DataGridView dgvStuInfo)
{
Application excel = new Application();
Workbooks workbooks = excel.Workbooks;
Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet worksheet = (Worksheet)workbook.Worksheets.Add(miss, miss, miss, miss);

worksheet.Cells[1, 1] = "学号";
worksheet.Cells[1, 2] = "密码";
worksheet.Cells[1, 3] = "姓名";
worksheet.Cells[1, 4] = "性别";
worksheet.Cells[1, 5] = "年级";
worksheet.Cells[1, 6] = "电话";
worksheet.Cells[1, 7] = "地址";
worksheet.Cells[1, 8] = "出生日期";
worksheet.Cells[1, 9] = "邮箱";
//获取标题行的单元格
Range range = worksheet.get_Range(excel.Cells[1, 1], excel.Cells[1, 10]);
//设置字体加粗
range.Font.Bold = true;
//设置字体颜色
range.Font.ColorIndex = 0;
//设置颜色背景
range.Interior.ColorIndex = 15;
//设置边框样式
range.Borders.LineStyle = XlLineStyle.xlContinuous;

//循环DateGridView中的数据赋值到Excel中
int i = 0, j = 0;
for (i = 0; i < dgvStuInfo.Rows.Count; i++)
{
for (j = 0; j < 3; j++)
{
excel.Cells[i + 2, j + 1] =
dgvStuInfo.Rows[i].Cells[j].Value.ToString();
}
//设置性别
worksheet.Cells[i + 2, 4] =
dgvStuInfo.Rows[i].Cells["Sex"].Value.ToString()
== "False" ? "男" : "女";

//设置显示的学生年级
System.Windows.Forms.DataGridViewComboBoxCell dgvCbo = (System.Windows.Forms.DataGridViewComboBoxCell)
dgvStuInfo.Rows[i].Cells["GradeId"];
worksheet.Cells[i + 2, 5] = dgvCbo.FormattedValue.ToString();

for (j = 5; j < 10; j++)
{
worksheet.Cells[i + 2, j + 1] =
dgvStuInfo.Rows[i].Cells[j].Value.ToString();
}
}
worksheet.get_Range(excel.Cells[2, 8], excel.Cells[i + 2, 8]).NumberFormat = "yyyy-m-d";

//显示当前窗口
worksheet.Visible = true;
}

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧