不裝置excel應用c#創立excel文件。本站提示廣大學習愛好者:(不裝置excel應用c#創立excel文件)文章只能為提供參考,不一定能成為您想要的結果。以下是不裝置excel應用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");