NPOI 导出 Excel 【c#】

效果图:

NPOI 导出 Excel 【c#】_第1张图片


代码

if (String.IsNullOrEmpty(ddlZph.SelectedValue))
        {
            MessageBox.ShowMessage("请选择一场招聘会");
            return;
        }
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = (HSSFSheet)wb.CreateSheet("sheet1"); //创建工作表
        sheet.CreateFreezePane(0, 5); //冻结列头行

        #region 设置列宽
        sheet.SetColumnWidth(0, 30 * 256);
        sheet.SetColumnWidth(1, 30 * 256);
        sheet.SetColumnWidth(2, 20 * 256);
        sheet.SetColumnWidth(3, 20 * 256);
        sheet.SetColumnWidth(4, 30 * 256);
        #endregion

        #region 定义字体
        HSSFFont cs_Title_Font = (HSSFFont)wb.CreateFont(); //创建字体
        cs_Title_Font.Boldweight = 800; //字体加粗
        cs_Title_Font.FontHeightInPoints = 12; //字体大小

        HSSFFont font_head = (HSSFFont)wb.CreateFont(); //创建字体
        font_head.Boldweight = 800; //字体加粗
        font_head.FontHeightInPoints = 10; //字体大小
        #endregion 

        #region 定义样式      
        HSSFCellStyle cs_Title = (HSSFCellStyle)wb.CreateCellStyle(); //创建列头样式
        cs_Title.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; //水平居中
        cs_Title.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; //垂直居中
        cs_Title.SetFont(cs_Title_Font); //将字体绑定到样式

        HSSFCellStyle cs_Head = (HSSFCellStyle)wb.CreateCellStyle(); //创建列头样式
        cs_Head.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; //水平居中
        cs_Head.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; //垂直居中
        cs_Head.SetFont(font_head); //将字体绑定到样式

        HSSFCellStyle cs_center = (HSSFCellStyle)wb.CreateCellStyle();
        cs_center.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; //水平居中
        cs_center.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; //垂直居中

        HSSFCellStyle cs_left = (HSSFCellStyle)wb.CreateCellStyle();
        cs_left.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT; //水平居中
        cs_left.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; //垂直居中

        HSSFCellStyle cs_right = (HSSFCellStyle)wb.CreateCellStyle();
        cs_right.Alignment = NPOI.SS.UserModel.HorizontalAlignment.RIGHT; //水平居中
        cs_right.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; //垂直居中

        HSSFCellStyle cs_addr = (HSSFCellStyle)wb.CreateCellStyle();
        cs_addr.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
        cs_addr.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; //垂直居中
        cs_addr.WrapText = true; //自动换行
        #endregion

        #region 数据绑定
        //取数
        string recId = ddlZph.SelectedValue;
        B_Z_Recruitment_List bll = new B_Z_Recruitment_List();
        Model.Z_Recruitment_List mod = bll.GetModel(int.Parse(recId));
        //第一行(标题)
        HSSFRow row_Title = (HSSFRow)sheet.CreateRow(0); //创建列头行
        row_Title.HeightInPoints = 19.5F; //设置列头行高
        HSSFCell cell_Title = (HSSFCell)row_Title.CreateCell(0); //创建单元格
        cell_Title.CellStyle = cs_Title; //将样式绑定到单元格
        cell_Title.SetCellValue("招聘会信息");
        //第二行
        HSSFRow row2 = (HSSFRow)sheet.CreateRow(1); //创建列头行
        HSSFCell cell0_row2 = (HSSFCell)row2.CreateCell(0); //创建单元格
        cell0_row2.SetCellValue("招聘会名称:");
        cell0_row2.CellStyle = cs_right;
        HSSFCell cell1_row2 = (HSSFCell)row2.CreateCell(1); //创建单元格
        cell1_row2.SetCellValue(mod.Rec_Title);

        //第三行
        HSSFRow row3 = (HSSFRow)sheet.CreateRow(2); //创建列头行
        HSSFCell cell0_row3 = (HSSFCell)row3.CreateCell(0); //创建单元格
        cell0_row3.SetCellValue("举办时间:");
        cell0_row3.CellStyle = cs_right;
        HSSFCell cell1_row3 = (HSSFCell)row3.CreateCell(1); //创建单元格
        cell1_row3.SetCellValue(mod.ZPTime.ToString());

        //第四行
        HSSFRow row4 = (HSSFRow)sheet.CreateRow(3); //创建列头行
        HSSFCell cell0_row4 = (HSSFCell)row4.CreateCell(0); //创建单元格
        cell0_row4.SetCellValue("举办地点:");
        cell0_row4.CellStyle = cs_right;
        HSSFCell cell1_row4 = (HSSFCell)row4.CreateCell(1); //创建单元格
        cell1_row4.SetCellValue(mod.addr);

        //第五行
        HSSFRow row5 = (HSSFRow)sheet.CreateRow(4); //创建列头行
        HSSFCell cell0_row5 = (HSSFCell)row5.CreateCell(0); //创建单元格
        cell0_row5.SetCellValue("招聘单位名称");
        cell0_row5.CellStyle = cs_Head;
        HSSFCell cell1_row5 = (HSSFCell)row5.CreateCell(1); //创建单元格
        cell1_row5.SetCellValue("招聘岗位");
        cell1_row5.CellStyle = cs_Head;
        HSSFCell cell2_row5 = (HSSFCell)row5.CreateCell(2); //创建单元格
        cell2_row5.SetCellValue("联系人姓名");
        cell2_row5.CellStyle = cs_Head;
        HSSFCell cell3_row5 = (HSSFCell)row5.CreateCell(3); //创建单元格
        cell3_row5.SetCellValue("联系电话");
        cell3_row5.CellStyle = cs_Head;
        HSSFCell cell4_row5 = (HSSFCell)row5.CreateCell(4); //创建单元格
        cell4_row5.SetCellValue("联系地址");
        cell4_row5.CellStyle = cs_Head;

        //单位信息数据
        string sqlCmp = "select * from z_company_info";
        DataTable dtCmpInfo = DbHelperSQL.GetDataTable(sqlCmp);
        //招聘单位数据
        string sql = "select * from Z_Company_List where state = 1 and rec_id = "+ recId + "";
        DataTable dt = DbHelperSQL.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            int k = 4;//从第5行开始
            int cmpRowS = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                k ++;
                cmpRowS = k;
                int cmpAddId = int.Parse(dt.Rows[i]["id"].ToString());
                int infoId = int.Parse(dt.Rows[i]["infoid"].ToString());
                string cmpName = dt.Rows[i]["CompanyName"].ToString();
                HSSFRow row = (HSSFRow)sheet.CreateRow(k);
                HSSFCell cell0_row = (HSSFCell)row.CreateCell(0); //创建单元格
                cell0_row.SetCellValue(cmpName);
                cell0_row.CellStyle = cs_center;
                //岗位数据
                string sqlJob = "select * from Z_Job_List where CompanyAddID = "+ cmpAddId + "";
                DataTable dtJob = DbHelperSQL.GetDataTable(sqlJob);
                //
                int x = k;
                for (int j = 0; j < dtJob.Rows.Count; j++)
                {
                    string jobName = dtJob.Rows[j]["JobName"].ToString();
                    if (j == 0)
                    {
                        string linkMan = dtCmpInfo.Select("id = " + infoId + "")[0]["LinkMan"].ToString();
                        string linkTel = dtCmpInfo.Select("id = " + infoId + "")[0]["Tel"].ToString();
                        string addr = dtCmpInfo.Select("id = " + infoId + "")[0]["Addr"].ToString();
                        HSSFCell cell1_row = (HSSFCell)row.CreateCell(1);
                        cell1_row.SetCellValue(jobName);
                        HSSFCell cell2_row = (HSSFCell)row.CreateCell(2);
                        cell2_row.SetCellValue(linkMan);
                        cell2_row.CellStyle = cs_center;
                        HSSFCell cell3_row = (HSSFCell)row.CreateCell(3);
                        cell3_row.SetCellValue(linkTel);
                        cell3_row.CellStyle = cs_center;
                        HSSFCell cell4_row = (HSSFCell)row.CreateCell(4);
                        cell4_row.SetCellValue(addr);
                        cell4_row.CellStyle = cs_addr;
                    }
                    else {
                        //职位创建行的下标
                        int z = x + j;
                        HSSFRow rowJob = (HSSFRow)sheet.CreateRow(z);
                        HSSFCell cell1_rowJob = (HSSFCell)rowJob.CreateCell(1); //创建单元格
                        cell1_rowJob.SetCellValue(jobName);
                        k++;
                    }
                }
                sheet.AddMergedRegion(new CellRangeAddress(cmpRowS, k, 0, 0));
                sheet.AddMergedRegion(new CellRangeAddress(cmpRowS, k, 2, 2));
                sheet.AddMergedRegion(new CellRangeAddress(cmpRowS, k, 3, 3));
                sheet.AddMergedRegion(new CellRangeAddress(cmpRowS, k, 4, 4));
            }
        }
        #endregion

        #region 合并单元格
        sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 4));
        sheet.AddMergedRegion(new CellRangeAddress(1, 1, 1, 4));
        sheet.AddMergedRegion(new CellRangeAddress(2, 2, 1, 4));
        sheet.AddMergedRegion(new CellRangeAddress(3, 3, 1, 4));
        #endregion

        //发送到客户端
        MemoryStream ms = new MemoryStream();
        wb.Write(ms);
        Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", HttpUtility.UrlEncode("WS" + "_" + DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8)));
        Response.BinaryWrite(ms.ToArray());
        Response.Flush();

        Response.End();
        wb = null;
        ms.Close();
        ms.Dispose()



你可能感兴趣的:(C#,NPOI)