NPOI导出到EXCEL

///   <summary>
        
///  创建列
        
///   </summary>
        
///   <param name="row"></param>
        
///   <param name="cellIndex"></param>
        
///   <returns></returns>
         public  static ICell CreateDefaultCell( this IRow row,  int cellIndex)
        {
             var cell = row.CreateCell(cellIndex);
             var style = row.Sheet.Workbook.CreateCellStyle();
            style.Alignment = HorizontalAlignment.CENTER;
            style.VerticalAlignment = VerticalAlignment.JUSTIFY;
            cell.CellStyle = style;
             return cell;
        }


#region NPOI导出EXCEL
                IWorkbook workbook =  new HSSFWorkbook();
                ISheet sheet = workbook.CreateSheet( " 班主任龄汇总 ");
                sheet.SetColumnWidth( 020 *  256);
                sheet.SetColumnWidth( 115 *  256);
                sheet.SetColumnWidth( 220 *  256);
                sheet.SetColumnWidth( 320 *  256);
                sheet.SetColumnWidth( 420 *  256);
                sheet.SetColumnWidth( 520 *  256);
                 var row0 = sheet.CreateRow( 0);
                row0.CreateDefaultCell( 0).SetCellValue( " 姓名 ");
                row0.CreateDefaultCell( 1).SetCellValue( " 性别 ");
                row0.CreateDefaultCell( 2).SetCellValue( " 身份证号 ");
                row0.CreateDefaultCell( 3).SetCellValue( " 参加工作时间 ");
                row0.CreateDefaultCell( 4).SetCellValue( " 进入本校时间 ");
                row0.CreateDefaultCell( 5).SetCellValue( " 担任班主任总年限 ");
                row0.CreateDefaultCell( 6).SetCellValue( " 最近担任班主任截止年份 ");

                 var rowInex =  1;
                 foreach ( var m  in listJcjg0101)
                {
                     var row = sheet.CreateRow(rowInex);
                    row.CreateCell( 0, CellType.STRING).SetCellValue(m.XM);
                    row.CreateCell( 1, CellType.STRING).SetCellValue(m.XBM ==  " 1 " ?  " " :  " ");
                    row.CreateCell( 2, CellType.STRING).SetCellValue(m.SFZJH);

                     // 通讯信息
                    
// 根据人员号获取通讯信息
                    Model.JCJG0106 Jcjg0106Model =  new BLL.JCJG0106().GetModel(m.RYH);
                     if (Jcjg0106Model !=  null)
                    {
                        row.CreateCell( 3, CellType.STRING).SetCellValue(Jcjg0106Model.TOWORKTIME == DateTime.Parse( " 0001/1/1 0:00:00 ") ?  "" : Jcjg0106Model.TOWORKTIME.ToString( " yyyy-MM "));
                        row.CreateCell( 4, CellType.STRING).SetCellValue(Jcjg0106Model.ENTERSCHOOLTIME == DateTime.Parse( " 0001/1/1 0:00:00 ") ?  "" : Jcjg0106Model.ENTERSCHOOLTIME.ToString( " yyyy-MM "));
                        row.CreateCell( 5, CellType.STRING).SetCellValue(Jcjg0106Model.AGECLASS.ToString());
                        row.CreateCell( 6, CellType.STRING).SetCellValue(Jcjg0106Model.FROMYEAR == DateTime.Parse( " 0001/1/1 0:00:00 ") ?  "" : Jcjg0106Model.FROMYEAR.ToString( " yyyy-MM "));
                    }
                    rowInex++;
                }
                MemoryStream ms =  new MemoryStream();
                workbook.Write(ms);

                 string filename =  " [ " + Convert.ToDateTime(DateTime.Now).ToString( " yyyy.MM.dd ") +  " ]班主任龄汇总.xls ";
                Response.AppendHeader( " Content-Disposition "" attachment; filename= " + HttpUtility.UrlEncode(filename)); // attachment说明以附件下载,inline说明在线打开
                Response.BinaryWrite(ms.ToArray());
                ms.Close();
                ms.Dispose();
                 #endregion

转载于:https://www.cnblogs.com/zecVip/p/4543895.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值