手机
当前位置:查字典教程网 >编程开发 >C#教程 >不安装excel使用c#创建excel文件
不安装excel使用c#创建excel文件
摘要:复制代码代码如下://创建excelobjectmissing=System.Reflection.Missing.Value;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");

【不安装excel使用c#创建excel文件】相关文章:

C#中fixed关键字的作用总结

解析使用C# lock同时访问共享数据

C#中使用反射获取结构体实例及思路

VB.NET中Caching的使用方法

C#中使用Socket获取网页源代码的代码

C#中使用快速排序按文件创建时间将文件排序的源码

使用C#开源文件实时监控工具Tail&TailUI介绍

C#中的yield关键字的使用方法介绍

解析c#操作excel后关闭excel.exe的方法

C#访问应用程序配置文件的方法

精品推荐
分类导航