不安装excel使用c#创建excel文件
更新时间:2014年02月21日 10:13:37 作者:
这篇文章主要介绍了使用c#创建excel的示例,刚时给出了不安装excel也可创建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");
您可能感兴趣的文章:
- Asp.Net使用Npoi导入导出Excel的方法
- WinForm项目开发中NPOI用法实例解析
- npoi2.0将datatable对象转换为excel2007示例
- asp.net使用npoi读取excel模板并导出下载详解
- 用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法
- 使用Npoi操作excel的解决办法
- NPOINT免费虚拟主机管理系统windows2003的安装方法【图文】
- C#实现Excel导入sqlite的方法
- C#将Sql数据保存到Excel文件中的方法
- C#利用XML创建Excel文档的实现方法
- C#导入导出EXCEL文件的代码实例
- c#读取excel内容内容示例分享
- C#连接Excel驱动与示例代码分享
- C#基于NPOI生成具有精确列宽行高的Excel文件的方法
相关文章
C#利用delegate实现Javascript的each方法
这篇文章主要为大家介绍了介绍了C#利用delegate实现Javascript的each方法,感兴趣的朋友可以参考一下2016-01-01
最新评论