62,268
社区成员
发帖
与我相关
我的任务
分享public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}public void GetStudentInfoDataToExcel(DataGridView dgvStuInfor,int ype)
{
Microsoft.Office.Interop.Excel.Application excel =
new Microsoft.Office.Interop.Excel.Application();
excel.SheetsInNewWorkbook = 1;
excel.Workbooks.Add();
//设置Excel列名
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] = "邮箱";
excel.Cells[1, 10] = "身份证号";
//获取标题行的单元格,即Range
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;
//循环将DataGridView中的数据赋值到Excel中
int i = 0, j = 0;
for (i = 0; i < dgvStuInfor.Rows.Count; i++)
{
for (j = 0; j < 3; j++)
{
excel.Cells[i + 2, j + 1] = dgvStuInfor.Rows[i].Cells[j].Value.ToString();
}
//设置性别
excel.Cells[i + 2, 4] =
dgvStuInfor.Rows[i].Cells["Gender"].Value.ToString() == "False" ? "男" : "女";
//设置显示的学生年级
DataGridViewComboBoxCell dgvCbo =
(DataGridViewComboBoxCell)dgvStuInfor.Rows[i].Cells["GradeId"];
excel.Cells[i + 2, 5] = dgvCbo.FormattedValue.ToString();
for (j = 5; j < 10; j++)
{
excel.Cells[i + 2, j + 1] = dgvStuInfor.Rows[i].Cells[j].Value.ToString();
}
}
//设置出生年月日的格式
excel.get_Range(excel.Cells[2, 8], excel.Cells[i + 2, 8]).NumberFormat = "yyyy-m-d";
//设置身份证号的格式
excel.get_Range(excel.Cells[2, 10], excel.Cells[i + 2, 10]).NumberFormatLocal = "0";
//设置Excel水平对齐方式
excel.get_Range(excel.Cells[1, 1], excel.Cells[i + 2, j + 2]).HorizontalAlignment
= XlHAlign.xlHAlignLeft;
//显示当前窗口
excel.Visible = true;
}