Пример #1
0
        /// <summary>
        /// 获取当前节点所有的叶子节点
        /// </summary>
        /// <param name="headcfg">配置</param>
        /// <returns></returns>
        /// <remarks>
        /// 创建:王春宝 2020-10-28
        /// </remarks>
        private static List <head> GetAllLeafNode(head headcfg)
        {
            List <head> heads = new List <head>();

            if (headcfg != null)
            {
                if (headcfg.Children.Count > 0)
                {
                    for (int i = 0; i < headcfg.Children.Count; i++)
                    {
                        var hds = GetAllLeafNode(headcfg.Children[i]);//递归调用
                        if (hds.Count > 0)
                        {
                            heads.AddRange(hds);
                        }
                    }
                    return(heads);
                }
                else
                {
                    heads.Add(headcfg);
                    return(heads);
                }
            }
            else
            {
                return(heads);
            }
        }
Пример #2
0
        /// <summary>
        /// 设置单元格样式
        /// 风格请自己按headCfg参数编写
        /// </summary>
        /// <param name="book"></param>
        /// <param name="headCfg">表头配置</param>
        /// <returns></returns>
        /// <remarks>
        /// 创建:王春宝 2020-10-26
        /// </remarks>
        private static ICellStyle GetCellStyle(XSSFWorkbook book, head headCfg)
        {
            ICellStyle style0 = book.CreateCellStyle();

            // 2、行高
            // row.Height = 30 * 20;    //行高为30
            // excelRow.Height = 25 * 20;
            // 单元格 列宽:
            //if (headCfg.Width > 0) {
            //    cell.Row.Sheet.SetColumnWidth(cell.ColumnIndex, headCfg.Width * 256);
            //}
            //三、设置居中:
            //cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
            style0.Alignment         = HorizontalAlignment.Center;
            style0.VerticalAlignment = VerticalAlignment.Center;
            //四、设置字体:
            IFont font = book.CreateFont();

            font.FontName           = "黑体";        //.SetFontName("黑体");
            font.FontHeightInPoints = (short)11.5; //设置字体大小
            style0.SetFont(font);                  //选择需要用到的字体格式

            //必须设置单元格背景色 FillForegroundColor 和 FillPattern 的值才能正确显示背景色
            style0.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightCornflowerBlue.Index; //(short)1灰色  NPOI.HSSF.Util.HSSFColor.LightBlue.Index;
            style0.FillPattern         = FillPattern.SolidForeground;                        // CellStyle.SOLID_FOREGROUND

            //二、设置边框:
            //cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
            //cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
            //cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
            //cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

            style0.BorderBottom = BorderStyle.Medium;// CellStyle.SOLID_FOREGROUND
            style0.BorderRight  = BorderStyle.Medium;

            return(style0);
        }
Пример #3
0
        /// <summary>
        /// 创建表头单元格,(支持递归调用)
        /// </summary>
        /// <param name="preHeadRow">上一行</param>
        /// <param name="startColIndex">开始列索引</param>
        /// <param name="rowIndex">行索引</param>
        /// <param name="outRowIndex">输出最新行索引</param>
        /// <param name="headCfg">表头配置</param>
        /// <returns>返回最新的列索引</returns>
        /// <remarks>
        /// 创建:王春宝 2020-10-24
        /// </remarks>
        private static int CreateHeadCell(IRow preHeadRow, int startColIndex, int rowIndex, out int outRowIndex, head headCfg)
        {
            int preRowIndex = rowIndex;

            rowIndex   += 1;
            outRowIndex = rowIndex;
            var          sheet = preHeadRow.Sheet;
            XSSFWorkbook book  = (XSSFWorkbook)sheet.Workbook;
            var          style = GetCellStyle(book, headCfg);
            //
            IRow curHeadRow = null;

            if (sheet.LastRowNum >= rowIndex)
            {
                curHeadRow = sheet.GetRow(rowIndex);
            }
            else
            {
                curHeadRow = sheet.CreateRow(rowIndex);//创建空行
                for (int i = 0; i < startColIndex; i++)
                {
                    ICell cell = curHeadRow.CreateCell(i); //创建单元格
                    cell.CellStyle = style;

                    ICell mycell = preHeadRow.GetCell(i); //获取单元格
                    if (mycell != null)
                    {
                        cell.SetCellValue(mycell.StringCellValue);//设置单元格内容
                    }
                }
            }
            int newColIndex = startColIndex;

            for (int i = 0; i < headCfg.Children.Count; i++)
            {
                if (headCfg.Children[i].Children.Count > 0)
                {                    //
                    int _outRowIndex = 0;
                    int old_ColIndex = newColIndex;
                    //
                    int new_ColIndex = CreateHeadCell(curHeadRow, newColIndex, rowIndex, out _outRowIndex, headCfg.Children[i]);//递归调用
                    //
                    for (int j = old_ColIndex; j < new_ColIndex; j++)
                    {
                        if (curHeadRow.GetCell(j) == null)
                        {
                            ICell _cell = curHeadRow.CreateCell(j);          //创建单元格
                            _cell.SetCellValue(headCfg.Children[i].ColName); //设置单元格内容
                            _cell.CellStyle = style;
                        }
                    }
                    //合并单元格
                    //参数1:起始行 参数2:终止行 参数3:起始列 参数4:终止列
                    CellRangeAddress region1 = new CellRangeAddress(curHeadRow.RowNum, curHeadRow.RowNum, (short)old_ColIndex, (short)(new_ColIndex - 1));
                    sheet.AddMergedRegion(region1);
                    //
                    if (_outRowIndex > outRowIndex)
                    {
                        outRowIndex = _outRowIndex;
                    }
                    newColIndex = new_ColIndex;
                }
                else
                {
                    ICell _cell = curHeadRow.CreateCell(newColIndex); //创建单元格
                    _cell.SetCellValue(headCfg.Children[i].ColName);  //设置单元格内容
                    _cell.CellStyle = style;
                    // 设置列宽
                    if (headCfg.Width > 0)
                    {
                        sheet.SetColumnWidth(_cell.ColumnIndex, headCfg.Width * 256);
                    }
                    else
                    {
                        sheet.SetColumnWidth(_cell.ColumnIndex, 13 * 256);
                    }
                    //
                    newColIndex += 1;
                }
            }
            //
            return(newColIndex);
        }