Пример #1
0
        private void DrawCellFontAndStyle(ICell cell, ColumnDrawing drawing, bool alternate)
        {
            if (_useTemplate)
            {
                return;
            }
            ICellStyle style;
            IFont      font;

            if (alternate)
            {
                style = drawing.AlternateCellStyle;
                font  = drawing.AlternateCellFont;
            }
            else
            {
                style = drawing.CellStyle;
                font  = drawing.CellFont;
            }
            if (style == null)
            {
                style = cell.Sheet.Workbook.GetCellStyleAt(0);
            }
            cell.CellStyle = style;
            if (font != null)
            {
                cell.CellStyle.SetFont(font);
            }
        }
Пример #2
0
 private void DrawHeaderFontAndStyle(ICell cell, ColumnDrawing drawing)
 {
     if (_useTemplate)
     {
         return;
     }
     cell.CellStyle = drawing.HeaderStyle ?? cell.Sheet.Workbook.GetCellStyleAt(0);
     if (drawing.HeaderFont != null)
     {
         cell.CellStyle.SetFont(drawing.HeaderFont);
     }
 }
Пример #3
0
        private ColumnDrawing[] GetColumnDrawings(Type classType)
        {
            var cellList        = new List <ColumnDrawing>();
            var classProperties = classType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);

            if (classProperties.Length <= 0)
            {
                return(new ColumnDrawing[0]);
            }
            foreach (var property in classProperties)
            {
                var ignoreAttr = property.GetCustomAttribute <DrawingIgnoreAttribute>();
                var propAttr   = property.GetCustomAttribute <NPOIColumnAttribute>();
                if (ignoreAttr != null || propAttr == null)
                {
                    continue;
                }
                var cellInfo = new ColumnDrawing();
                // Column basic information
                var columnIndexes = new List <int>();
                if (propAttr.Index >= 0)
                {
                    if (columnIndexes.Contains(propAttr.Index))
                    {
                        throw new Exception("Duplicate column index " + propAttr.Index);
                    }
                    cellInfo.ColumnIndex = propAttr.Index;
                    columnIndexes.Add(propAttr.Index);
                }
                else
                {
                    throw new Exception("Column Index is out of range.\r\nThe value must not be smaller than 0.");
                }
                if (!string.IsNullOrEmpty(propAttr.Name))
                {
                    cellInfo.ColumnName = propAttr.Name;
                }
                if (string.IsNullOrEmpty(cellInfo.ColumnName))
                {
                    cellInfo.ColumnName = property.Name;
                }
                // Column style information
                var headerStyleAttr = property.GetCustomAttribute <HeaderStyleAttribute>();
                if (headerStyleAttr != null)
                {
                    cellInfo.ColumnWidth = headerStyleAttr.ColumnWidth;
                }
                var headerStyle = FillStyle(headerStyleAttr);
                if (headerStyle != null)
                {
                    cellInfo.HeaderStyle = headerStyle;
                }
                var headerFont = FillFont(headerStyleAttr);
                if (headerFont != null)
                {
                    cellInfo.HeaderFont = headerFont;
                }

                var cellStyleAttr = property.GetCustomAttribute <CellStyleAttribute>();
                if (cellStyleAttr != null)
                {
                    var cellStyle = FillStyle(cellStyleAttr);
                    if (cellStyle != null)
                    {
                        cellInfo.CellStyle = cellStyle;
                    }
                    var cellFont = FillFont(cellStyleAttr);
                    if (cellFont != null)
                    {
                        cellInfo.CellFont = cellFont;
                    }
                }
                var alternateCellStyleAttr = property.GetCustomAttribute <AlternateCellStyleAttribute>();
                if (alternateCellStyleAttr != null)
                {
                    cellInfo.HasAlternate = true;
                    var cellStyle = FillStyle(alternateCellStyleAttr);
                    if (cellStyle != null)
                    {
                        cellInfo.AlternateCellStyle = cellStyle;
                    }
                    var cellFont = FillFont(alternateCellStyleAttr);
                    if (cellFont != null)
                    {
                        cellInfo.AlternateCellFont = cellFont;
                    }
                }
                else
                {
                    cellInfo.HasAlternate = false;
                }
                cellInfo.Property = property;
                cellList.Add(cellInfo);
            }

            return(cellList.OrderBy(x => x.ColumnIndex).ToArray());
        }