日期:2014-05-17  浏览次数:21001 次

“object”未包含“get_Range”的定义
各位大大 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的定义
求解决

------解决方案--------------------
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.Fon