C# 创建Excel或需不安装Office

第一种、Aspose.Cells.dll

//如果需要饶过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 + @"	est.xls");
  大致如此~!

第二种、NPOI

http://npoi.codeplex.com/

NPOI无需Office COM组件且不依赖Office,使用NPOI能够帮助开发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, doc, ppt等。NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office的情况下对Word/Excel文档进行读写操作。
被人称为操作EXCEL的终极方案,例子如下:

//引用
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
//将WorkBook指到我们原本设计好的Templete Book1.xls
using (IWorkbook wb = new HSSFWorkbook(new FileStream("D:/Book1.xls", FileMode.Open)))
{
     try
    {
        //设定要使用的Sheet为第0个Sheet
        ISheet TempSheet = wb.GetSheetAt(0);
        int StartRow = 4;
        //tDS为Query回来的资料
    DataSet tDS = new DataSet();    
for (int i = 0; i < tDS.Tables[0].Rows.Count; i++)
       {
            //第一个Row要用Create的
           TempSheet.CreateRow(StartRow + i).CreateCell(0).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][0]));
            //第二个Row之后直接用Get的 
           TempSheet.GetRow(StartRow + i).CreateCell(1).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][1]));
           TempSheet.GetRow(StartRow + i).CreateCell(2).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][2]));
            TempSheet.GetRow(StartRow + i).CreateCell(3).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][3]));
            TempSheet.GetRow(StartRow + i).CreateCell(4).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][4]));
            TempSheet.GetRow(StartRow + i).CreateCell(5).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][5]));
             TempSheet.GetRow(StartRow + i).CreateCell(6).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][6]));
             TempSheet.GetRow(StartRow + i).CreateCell(7).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][7]));
              TempSheet.GetRow(StartRow + i).CreateCell(8).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][8]));
              TempSheet.GetRow(StartRow + i).CreateCell(9).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][9]));
              TempSheet.GetRow(StartRow + i).CreateCell(10).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][10]));
              TempSheet.GetRow(StartRow + i).CreateCell(11).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][11]));
              TempSheet.GetRow(StartRow + i).CreateCell(12).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][12]));
             }
             //将文档写到指定位置
    using (FileStream file = new FileStream("H:/Test_NPOI4.xls", FileMode.Create))
    {
        wb.Write(file);
         file.Close();
         file.Dispose();
     }
}
catch (Exception e)
{
string a = e.ToString();
}
}
原文地址:https://www.cnblogs.com/pingming/p/4409101.html