NPOI导出EXCEl通用方法

 /// <summary>
        /// NPOI导出EXCEl
        /// </summary>
        /// <param name="sheetName">工作表名</param>
        /// <param name="arrHead">表头</param>
        /// <param name="arrColWidth">列宽</param>
        /// <param name="headHeight">表头高度</param>
        /// <param name="colHeight">列高度</param>
        /// <param name="dt">数据</param>
        /// <returns>工作簿</returns>
        public static HSSFWorkbook ExcelToNpoiCustom(string sheetName, string[] arrHead,int[] arrColWidth,int headHeight,int colHeight,DataTable dt)
        {
            //建立空白工作簿
            HSSFWorkbook book = new HSSFWorkbook();
            //在工作簿中:建立空白工作表
            Sheet sheet = book.CreateSheet(sheetName);

            Font font = book.CreateFont();
            font.Boldweight = short.MaxValue;
            font.FontHeightInPoints = 11;

            //设置单元格的样式:水平垂直对齐居中
            CellStyle cellStyle = book.CreateCellStyle();
            cellStyle.Alignment = HorizontalAlignment.CENTER;
            cellStyle.VerticalAlignment = VerticalAlignment.CENTER;
            cellStyle.BorderBottom = CellBorderType.THIN;
            cellStyle.BorderLeft = CellBorderType.THIN;
            cellStyle.BorderRight = CellBorderType.THIN;
            cellStyle.BorderTop = CellBorderType.THIN;
            cellStyle.BottomBorderColor = HSSFColor.BLACK.index;
            cellStyle.LeftBorderColor = HSSFColor.BLACK.index;
            cellStyle.RightBorderColor = HSSFColor.BLACK.index;
            cellStyle.TopBorderColor = HSSFColor.BLACK.index;

            
            cellStyle.WrapText = true;//自动换行
            

            //设置表头的样式:水平垂直对齐居中,加粗
            CellStyle titleCellStyle = book.CreateCellStyle();
            titleCellStyle.Alignment = HorizontalAlignment.CENTER;
            titleCellStyle.VerticalAlignment = VerticalAlignment.CENTER;
            titleCellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREY_25_PERCENT.index; //图案颜色
            titleCellStyle.FillPattern = FillPatternType.SPARSE_DOTS; //图案样式
            titleCellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.GREY_25_PERCENT.index; //背景颜色
            //设置边框
            titleCellStyle.BorderBottom = CellBorderType.THIN;
            titleCellStyle.BorderLeft = CellBorderType.THIN;
            titleCellStyle.BorderRight = CellBorderType.THIN;
            titleCellStyle.BorderTop = CellBorderType.THIN;
            titleCellStyle.BottomBorderColor = HSSFColor.BLACK.index;
            titleCellStyle.LeftBorderColor = HSSFColor.BLACK.index;
            titleCellStyle.RightBorderColor = HSSFColor.BLACK.index;
            titleCellStyle.TopBorderColor = HSSFColor.BLACK.index;
            //设置字体
            titleCellStyle.SetFont(font);
            
            //表头
            Row headRow = sheet.CreateRow(0);
            headRow.HeightInPoints = headHeight;
            for (int i = 0; i < arrHead.Length; i++)
            {
                headRow.CreateCell(i).SetCellValue(arrHead[i]);
                headRow.GetCell(i).CellStyle = titleCellStyle;
            }

            //列宽
            for (int j = 0; j < arrColWidth.Length; j++)
            {
                sheet.SetColumnWidth(j, arrColWidth[j]*256);
            }

            //循环添加行
            for (int k = 0; k < dt.Rows.Count; k++)
            {
                Row row = sheet.CreateRow(k + 1);
                row.HeightInPoints = colHeight;
                //循环列赋值
                for (int l = 0; l < dt.Columns.Count; l++)
                {

                    row.CreateCell(l).SetCellValue(dt.Rows[k][l].ToString());
                    row.GetCell(l).CellStyle = cellStyle;


                }
            };

            return book;

        }

调用示例:

     string[] arrHead =
                {
                    "发布时间", "业务编号", "紧急度", "业务类型", "发布部门", "发布人", "责任人", "确认部门", "接收部门", "抄送部门", "联络方式",
                    "标题", "发布内容", "是否有附件", "实施期限", "是否需要回复", "回复部门", "回复内容", "回复时间", "状态"
                };
                int[] arrColWidth = {12, 15, 10, 15, 15, 12, 12, 15, 30, 20, 20, 20, 30, 15, 12, 15, 12, 20, 12, 12};



                HSSFWorkbook book = ExcelHelper.ExcelToNpoiCustom("业务联络", arrHead, arrColWidth, 25, 18, dt);
                string datetime = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                string path = Server.MapPath("~/Manager/UserModule/DINA/fileUpload/UpFile/");
                FileStream file = new FileStream(path + datetime + ".xls", FileMode.OpenOrCreate);
                book.Write(file);
                file.Flush();
                file.Close();
                book = null;

                HttpContext.Current.Response.Write(datetime + ".xls");
                HttpContext.Current.Response.End();                



猜你喜欢

转载自blog.csdn.net/qq_27462223/article/details/77836910