不安裝excel使用c#創(chuàng)建excel文件
更新時間:2014年02月21日 10:13:37 作者:
這篇文章主要介紹了使用c#創(chuàng)建excel的示例,剛時給出了不安裝excel也可創(chuàng)建excel的方法,需要的朋友可以參考下
復制代碼 代碼如下:
//創(chuàng)建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] = "就職狀態(tài)(jobStatus)";
sheet.Cells[1, 10] = "公司電話(telephoneNum)";
sheet.Cells[1, 11] = "郵箱(email)";
sheet.Cells[1, 12] = "祖籍(nativeHome)";
sheet.Cells[1, 13] = "畢業(yè)學校(graduateSchool)";
sheet.Cells[1, 14] = "專業(yè)(major)";
sheet.Cells[1, 15] = "畢業(yè)時間(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 循環(huán)寫入內(nèi)容
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;//"就職狀態(tài)(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;// "畢業(yè)學校(graduateSchool)";
sheet.Cells[count, 14] = item.major;// "專業(yè)(major)";
sheet.Cells[count, 15] = item.graduateTime;//"畢業(yè)時間(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那么就看我最后的實現(xiàn)方法吧~!
我最后的實現(xiàn)是使用的第三方Aspose.Cells.dll
具了解這個dll一直免費,(第三方有風險,使用需謹慎)
復制代碼 代碼如下:
//創(chuàng)建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("就職狀態(tài)(jobStatus)");
sheet.Cells["J1"].PutValue("公司電話(telephoneNum)");
sheet.Cells["K1"].PutValue("郵箱(email)");
sheet.Cells["L1"].PutValue("祖籍(nativeHome)");
sheet.Cells["M1"].PutValue("畢業(yè)學校(graduateSchool)");
sheet.Cells["N1"].PutValue("專業(yè)(major)");
sheet.Cells["O1"].PutValue("畢業(yè)時間(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 循環(huán)寫入內(nèi)容
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 ? "在職" : "離職");//"就職狀態(tài)(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);// "畢業(yè)學校(graduateSchool)";
sheet.Cells["N" + count].PutValue(item.major);// "專業(yè)(major)";
sheet.Cells["O" + count].PutValue(item.graduateTime.ToString() == "" ? null : item.graduateTime.ToString());//"畢業(yè)時間(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項目開發(fā)中NPOI用法實例解析
- npoi2.0將datatable對象轉換為excel2007示例
- asp.net使用npoi讀取excel模板并導出下載詳解
- 用NPOI創(chuàng)建Excel、合并單元格、設置單元格樣式、邊框的方法
- 使用Npoi操作excel的解決辦法
- NPOINT免費虛擬主機管理系統(tǒng)windows2003的安裝方法【圖文】
- C#實現(xiàn)Excel導入sqlite的方法
- C#將Sql數(shù)據(jù)保存到Excel文件中的方法
- C#利用XML創(chuàng)建Excel文檔的實現(xiàn)方法
- C#導入導出EXCEL文件的代碼實例
- c#讀取excel內(nèi)容內(nèi)容示例分享
- C#連接Excel驅動與示例代碼分享
- C#基于NPOI生成具有精確列寬行高的Excel文件的方法
相關文章
C#利用delegate實現(xiàn)Javascript的each方法
這篇文章主要為大家介紹了介紹了C#利用delegate實現(xiàn)Javascript的each方法,感興趣的朋友可以參考一下2016-01-01C#并發(fā)編程之a(chǎn)sync和await關鍵字詳解
對于?async?和?await?兩個關鍵字,對于一線開發(fā)人員再熟悉不過了,到處都是它們的身影,下面小編就來和大家記錄匯總下它們的使用吧2023-07-07