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的定义
求解决
12 个解决方案
#1
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;
}
{
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;
}
#2
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了
把这句话改成
Range range = excel.Range[excel.Cells[1, 1], excel.Cells[1, 10]];
就OK了
#3
三楼的,我试过没用,直接报错。
#4
Excel.Range c1 = oSheet.Cells[1, 1];
Excel.Range c2 = oSheet.Cells[4, 4];
oRange = (Excel.Range)oSheet.get_Range(c1, c2);
Excel.Range c2 = oSheet.Cells[4, 4];
oRange = (Excel.Range)oSheet.get_Range(c1, c2);
#5
你是反编译的代码?
换ilspy试试看,用.NET Reflector的时候,不要忽略引用的库,版本也要匹配。
这个问题的本质是,事实上C#定义的属性,在IL层面上是对应一个 get_属性名 和一个 set_属性名 的方法。
换ilspy试试看,用.NET Reflector的时候,不要忽略引用的库,版本也要匹配。
这个问题的本质是,事实上C#定义的属性,在IL层面上是对应一个 get_属性名 和一个 set_属性名 的方法。
#6
这个注意“[”中括号就行了
#7
把对操作excel添加的程序集的属性;嵌入式操作改为False就行了
#8
真NB
#9
膜拜2楼和6楼大神!!
#10
谢谢!非常感谢!
#11
非常感谢!谢谢!
#12
与.net版本2.0、Office.Core及Microsoft.Office.Interop.Excel设置复制到本地为True,可解决楼主问题。
#1
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;
}
{
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;
}
#2
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了
把这句话改成
Range range = excel.Range[excel.Cells[1, 1], excel.Cells[1, 10]];
就OK了
#3
三楼的,我试过没用,直接报错。
#4
Excel.Range c1 = oSheet.Cells[1, 1];
Excel.Range c2 = oSheet.Cells[4, 4];
oRange = (Excel.Range)oSheet.get_Range(c1, c2);
Excel.Range c2 = oSheet.Cells[4, 4];
oRange = (Excel.Range)oSheet.get_Range(c1, c2);
#5
你是反编译的代码?
换ilspy试试看,用.NET Reflector的时候,不要忽略引用的库,版本也要匹配。
这个问题的本质是,事实上C#定义的属性,在IL层面上是对应一个 get_属性名 和一个 set_属性名 的方法。
换ilspy试试看,用.NET Reflector的时候,不要忽略引用的库,版本也要匹配。
这个问题的本质是,事实上C#定义的属性,在IL层面上是对应一个 get_属性名 和一个 set_属性名 的方法。
#6
这个注意“[”中括号就行了
#7
把对操作excel添加的程序集的属性;嵌入式操作改为False就行了
#8
真NB
#9
膜拜2楼和6楼大神!!
#10
谢谢!非常感谢!
#11
非常感谢!谢谢!
#12
与.net版本2.0、Office.Core及Microsoft.Office.Interop.Excel设置复制到本地为True,可解决楼主问题。