1. 程式人生 > >NPOI 匯出 Excel 【c#】

NPOI 匯出 Excel 【c#】

效果圖:


程式碼

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()