不安装excel使用c#创建excel文件
时间:2023-02-20 07:03:25
//创建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] = "身份证号(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;//"身份证号(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("身份证号(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);//"身份证号(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");
标签:c#,excel
0
投稿
猜你喜欢
常用的C语言排序算法(两种)
2022-12-28 02:28:37
Java基础学习之字符串知识总结
2021-06-30 10:23:56
JAVA实现红包分发的示例代码
2022-10-08 06:18:15
带你一文深入认识Java String类
2023-10-15 10:55:54
深入理解TextView实现Rich Text--在同一个TextView设置不同字体风格
2023-05-25 04:52:01
Android UI效果之绘图篇(三)
2022-01-03 03:01:08
简单了解Thymeleaf语法 数据延迟加载使用实例
2021-11-27 04:05:49
Android设置透明状态栏和透明导航栏
2021-09-22 07:19:41
C#应用ToolStrip控件使用方法
2023-05-23 09:10:18
Android 文件存储与SharedPreferences存储方式详解用法
2021-07-22 20:11:54
MyBatisPlus3.x中使用代码生成器(全注释)
2022-04-06 19:34:38
C#基于socket模拟http请求的方法
2022-09-12 09:55:11
Java中使用StackWalker和Stream API进行堆栈遍历
2023-04-12 11:29:07
C#判断上传文件是否是图片以防止木马上传的方法
2023-10-02 13:13:50
详解Android App中创建ViewPager组件的方法
2023-07-12 00:46:14
Android选择图片或拍照图片上传到服务器
2022-12-15 01:48:41
Android实现视图轮播效果
2023-04-14 08:52:10
Java Valhalla Project项目介绍
2021-10-03 00:29:28
深入剖析Java中String类的concat方法
2023-06-02 07:07:13
Android实现通用筛选栏
2021-10-16 05:31:42