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
posted @ 2015-06-01 14:35  Tz__C  阅读(192)  评论(0编辑  收藏  举报