不安装excel使用c#创建excel文件
发布时间:2023-02-20 07:03:25
标签: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");


猜你喜欢
- 首先,思考这么一种场景,假如现在A类继承了B类和C类,并且B类和C类中,都存在test()方法,那么当A类对象调用test()方法时,该调用
- 再使用整型转string的时候感觉有点棘手,因为itoa不是标准C里面的,而且即便是有itoa,其他类型转string不是很方便。后来去网上
- 泛型与类型擦除泛型,JDK 1.5新特性,本质是参数化类型(Parametersized Type) 的应用,即所操作的数据类型被指定为一个
- 安装JDK 向导进行相关参数设置。如图: 正在安装程序的相关功能,如图: 选择安装的路径,可以自定义,也可以默认路径。如图: 成功安装之
- Linux+Docker+SpringBoot+IDEA一键自动化部署的步骤记录从打包到服务器配置上线全流程安装docker详细步骤请戳这里
- 直接调用HashKit.sha1(String str)方法就可以了,,返回的是16进制的字符串长度是40,也就是用md.digest()方
- 本文实例讲述了C#图像处理之木刻效果实现方法。分享给大家供大家参考。具体如下://木刻效果public Bitmap PFilterMuKe
- SSH(Secure Shell)是一套协议标准,可以用来实现两台机器之间的安全登录以及安全的数据传送,其保证数据安全的原理是非对称加密。传
- 1、Jetbrains官网下载IntelliJ IDEA1.1 官方网站http://www.jetbrains.com/idea/&nbs
- 一、需求Jenkins大多数情况下都是用来部署Java项目,Java项目有一个特点是>需要编译和打包的,一般情况下编译和打包都是用ma
- 先给出具体代码(当前目录为:D:\pro):package org.test;public class TestJava{ pu
- 在Spring Boot的众多Starter POMs中有一个特殊的模块,它不同于其他模块那样大多用于开发业务功能或是连接一些其他外部资源。
- 前言泛型在java中有很重要的地位,无论是开源框架还是JDK源码都能看到它。毫不夸张的说,泛型是通用设计上必不可少的元素,所以真正理解与正确
- 一般启动一个新的Activity都默认有切换的动画效果,比如界面从右至左的移动。但是有些时候我们不需要这个动画,怎么办?操作方法比较麻烦,这
- CircleImageView实现圆形头像代码分享,供大家参考,具体内容如下一、创建属性文件(attrs.xml)具体操作:1、在项目的va
- Android中webview与JS交互、互调方法实例详解前言:对于试水的功能,一般公司都会采用H5的方式来开发,可以用很少的资源与很短的项
- java 线程池详解什么是线程池?提供一组线程资源用来复用线程资源的一个池子为什么要用线程池?线程的资源是有限的,当处理一组业务的时候,我们
- dll的编写,首先是打开VS新建一个C++的控制台程序,下一步后选择dll以及空文档即可。然后就是添加一个类添加一个方法。方法排头固定格式
- 静态变量静态变量位于栈上,它是一个全局变量,在编译期就已经生成。public class Cow{public static int cou
- 前言在我们用户登录的时候,为了安全性考虑,会增加验证码的功能,这里采用的是google的kaptcha;spirngboot是轻便,独立,使