国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

不安裝excel使用c#創建excel文件

2020-01-24 02:47:48
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

//創建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");

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台北市| 河东区| 安国市| 杭锦旗| 石景山区| 洛扎县| 望奎县| 北票市| 常山县| 海口市| 左云县| 海安县| 德格县| 慈利县| 永嘉县| 巴南区| 兖州市| 斗六市| 万安县| 镶黄旗| 富源县| 焉耆| 太谷县| 涿鹿县| 甘谷县| 镇远县| 慈溪市| 湄潭县| 汉川市| 理塘县| 丹阳市| 精河县| 天津市| 岳西县| 新宁县| 庆安县| 宝应县| 神木县| 元谋县| 五河县| 呼玛县|