不安装excel使用c#创建excel文件
程序员文章站
2023-12-20 21:19:10
复制代码 代码如下://创建excelobject missing = system.reflection.missing.value;excel.application...
复制代码 代码如下:
//创建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");
推荐阅读
-
不安装excel使用c#创建excel文件
-
c#读写excel文件使用示例
-
C#使用Aspose.Cells创建和读取Excel文件
-
C#使用Aspose.Cells创建和读取Excel文件
-
【C#常用方法】2.DataTable(或DataSet)与Excel文件之间的导出与导入(使用NPOI)
-
使用实现XlsxWriter创建Excel文件并编辑
-
C# WinForm创建Excel文件的实例
-
使用SXSSFWorkbook创建大型excel文件
-
C#使用oledb操作excel文件的方法
-
【C#常用方法】2.DataTable(或DataSet)与Excel文件之间的导出与导入(使用NPOI)