-
C#教程之不安装excel使用c#创建excel文件
代码如下:
//创建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");
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式