ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

表结构1

2022-09-16 01:03:44  阅读:260  来源: 互联网

标签:count PutValue sheet Cells item ToString 结构


代码如下:

//创建excel
object missing = System.Reflection.Missing.Value;
Excel.Application app = new Excel.Application();
app.Application.Workbooks.Add(true);
Excel.Workbook book = (Excel.Workbook)app.ActiveWorkbook;
Excel.Worksheet sheet = (Excel.Worksheet)book.ActiveSheet;

#region 第一行
sheet.Cells[1, 1] = "登录名(loginID)";
sheet.Cells[1, 2] = "密码(passWord)";
sheet.Cells[1, 3] = "姓(familyName)";
sheet.Cells[1, 4] = "名(firstName)";
sheet.Cells[1, 5] = "性别(gender)";
sheet.Cells[1, 6] = "出生时间(dateofBirth)";
sheet.Cells[1, 7] = "手机号(cellphoneNum)";
sheet.Cells[1, 8] = "SFZ号(identityID)";
sheet.Cells[1, 9] = "就职状态(jobStatus)";
sheet.Cells[1, 10] = "公司电话(telephoneNum)";
sheet.Cells[1, 11] = "邮箱(email)";
sheet.Cells[1, 12] = "祖籍(nativeHome)";
sheet.Cells[1, 13] = "毕业学校(graduateSchool)";
sheet.Cells[1, 14] = "专业(major)";
sheet.Cells[1, 15] = "毕业时间(graduateTime)";
sheet.Cells[1, 16] = "学历(education)";
sheet.Cells[1, 17] = "邮编(zipCode)";
sheet.Cells[1, 18] = "地址(address)";
sheet.Cells[1, 19] = "入职时间(entryTime)";
sheet.Cells[1, 20] = "离开时间(leaveTime)";
sheet.Cells[1, 21] = "备注(remarks)";
sheet.Cells[1, 22] = "部门(departmentID)";
sheet.Cells[1, 23] = "职位(JobTypeID";
#endregion

#region 循环写入内容
int count = 1;
foreach (EmployeeInfo_tbl item in enterpriseInfo.Employees)
{
count = count+1;
sheet.Cells[count, 1] = item.loginID;
sheet.Cells[count, 2] = item.passWord;
sheet.Cells[count, 3] = item.familyName;//"姓(familyName)";
sheet.Cells[count, 4] = item.firstName; //"名(firstName)";
sheet.Cells[count, 5] = item.gender; //"性别(gender)";
sheet.Cells[count, 6] = item.dateofBirth; //"出生时间(dateofBirth)";
sheet.Cells[count, 7] = item.cellphoneNum;//"手机号(cellphoneNum)";
sheet.Cells[count, 8] = item.identityID;//"SFZ号(identityID)";
sheet.Cells[count, 9] = item.jobStatus;//"就职状态(jobStatus)";
sheet.Cells[count, 10] = item.telephoneNum;//"公司电话(telephoneNum)";
sheet.Cells[count, 11] = item.email;//"邮箱(email)";
sheet.Cells[count, 12] = item.nativeHome;//"祖籍(nativeHome)";
sheet.Cells[count, 13] = item.graduateSchool;// "毕业学校(graduateSchool)";
sheet.Cells[count, 14] = item.major;// "专业(major)";
sheet.Cells[count, 15] = item.graduateTime;//"毕业时间(graduateTime)";
sheet.Cells[count, 16] = item.education;// "学历(education)";
sheet.Cells[count, 17] = item.zipCode;// "邮编(zipCode)";
sheet.Cells[count, 18] = item.address;//"地址(address)";
sheet.Cells[count, 19] = item.entryTime;//"入职时间(entryTime)";
sheet.Cells[count, 20] = item.leaveTime;// "离开时间(leaveTime)";
sheet.Cells[count, 21] = item.remarks;// "备注(remarks)";
sheet.Cells[count, 22] = item.Department.departmentName;// "部门(departmentID)";
sheet.Cells[count, 23] = item.JobType.jobName;// "职位(JobTypeID";
}
#endregion
//保存
//book.SaveCopyAs(_FolderBrowserDialog.SelectedPath + @"\test.xls");
//关闭文件
//book.Close(false, missing, missing);
//退出excel
//app.Quit();
需要引用com里的Microsoft Excel 14.0 Object Libary(其它版本方法大致相同)

当然就意味着做这件事情就必须安装office Excel,

如果需要饶过office Excel那么就看我最后的实现方法吧~!

我最后的实现是使用的第三方Aspose.Cells.dll

具了解这个dll一直免费,(第三方有风险,使用需谨慎)

代码如下:

//创建excel
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
sheet.FreezePanes(1, 1, 1, 0);//冻结第一行

#region 第一行
sheet.Cells["A1"].PutValue("登录名(loginID)");
sheet.Cells["B1"].PutValue("密码(passWord)");
sheet.Cells["C1"].PutValue("姓(familyName)");
sheet.Cells["D1"].PutValue("名(firstName)");
sheet.Cells["E1"].PutValue("性别(gender)");
sheet.Cells["F1"].PutValue("出生时间(dateofBirth)");
sheet.Cells["G1"].PutValue("手机号(cellphoneNum)");
sheet.Cells["H1"].PutValue("SFZ号(identityID)");
sheet.Cells["I1"].PutValue("就职状态(jobStatus)");
sheet.Cells["J1"].PutValue("公司电话(telephoneNum)");
sheet.Cells["K1"].PutValue("邮箱(email)");
sheet.Cells["L1"].PutValue("祖籍(nativeHome)");
sheet.Cells["M1"].PutValue("毕业学校(graduateSchool)");
sheet.Cells["N1"].PutValue("专业(major)");
sheet.Cells["O1"].PutValue("毕业时间(graduateTime)");
sheet.Cells["P1"].PutValue("学历(education)");
sheet.Cells["Q1"].PutValue("邮编(zipCode)");
sheet.Cells["R1"].PutValue("地址(address)");
sheet.Cells["S1"].PutValue("入职时间(entryTime)");
sheet.Cells["T1"].PutValue("离开时间(leaveTime)");
sheet.Cells["U1"].PutValue("备注(remarks)");
sheet.Cells["V1"].PutValue("部门(departmentID)");
sheet.Cells["W1"].PutValue("职位(JobTypeID");
#endregion

#region 循环写入内容
int count = 1;
foreach (EmployeeInfo_tbl item in enterpriseInfo.Employees)
{
count = count + 1;
sheet.Cells["A" + count].PutValue(item.loginID);
sheet.Cells["B" + count].PutValue(item.passWord);
sheet.Cells["C" + count].PutValue(item.familyName);//"姓(familyName)";
sheet.Cells["D" + count].PutValue(item.firstName); //"名(firstName)";
sheet.Cells["E" + count].PutValue(item.gender == 0 ? "女" : "男"); //"性别(gender)";
sheet.Cells["F" + count].PutValue(item.dateofBirth.ToString() == "" ? null : item.dateofBirth.ToString()); //"出生时间(dateofBirth)";
sheet.Cells["G" + count].PutValue(item.cellphoneNum.ToString());//"手机号(cellphoneNum)";
sheet.Cells["H" + count].PutValue(item.identityID);//"SFZ号(identityID)";
sheet.Cells["I" + count].PutValue(item.jobStatus == 1 ? "在职" : "离职");//"就职状态(jobStatus)";
sheet.Cells["J" + count].PutValue(item.telephoneNum);//"公司电话(telephoneNum)";
sheet.Cells["K" + count].PutValue(item.email);//"邮箱(email)";
sheet.Cells["L" + count].PutValue(item.nativeHome);//"祖籍(nativeHome)";
sheet.Cells["M" + count].PutValue(item.graduateSchool);// "毕业学校(graduateSchool)";
sheet.Cells["N" + count].PutValue(item.major);// "专业(major)";
sheet.Cells["O" + count].PutValue(item.graduateTime.ToString() == "" ? null : item.graduateTime.ToString());//"毕业时间(graduateTime)";
string ed = "";
switch (item.education)
{
case 1:
ed = "初中/小学";
break;
case 2:
ed = "高中/中专";
break;
case 3:
ed = "本科/专科";
break;
case 4:
ed = "研究生以上";
break;
default:
ed = null;
break;
}
sheet.Cells["P" + count].PutValue(ed);// "学历(education)";
sheet.Cells["Q" + count].PutValue(item.zipCode);// "邮编(zipCode)";
sheet.Cells["R" + count].PutValue(item.address);//"地址(address)";
sheet.Cells["S" + count].PutValue(item.entryTime.ToString() == "" ? null : item.entryTime.ToString());//"入职时间(entryTime)";
sheet.Cells["T" + count].PutValue(item.leaveTime.ToString() == "" ? null : item.leaveTime.ToString());// "离开时间(leaveTime)";
sheet.Cells["U" + count].PutValue(item.remarks);// "备注(remarks)";
sheet.Cells["V" + count].PutValue(item.Department.departmentName);// "部门(departmentID)";
sheet.Cells["W" + count].PutValue(item.JobType.jobName);// "职位(JobTypeID";
}
#endregion

//保存
workbook.Save(_FolderBrowserDialog.SelectedPath + @"\test.xls");

http://www.qb5200.com/article/275707.html

标签:count,PutValue,sheet,Cells,item,ToString,结构
来源: https://www.cnblogs.com/firespeed/p/16698486.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有