예제 #1
0
        private void SetCellStyle(ReportCell cell, int rowIndex, int columnIndex)
        {
            Range range = (Range)excelWorkSheet.Range[
                excelWorkSheet.Cells[rowIndex, columnIndex],
                excelWorkSheet.Cells[rowIndex + cell.RowSpan, columnIndex + cell.ColumnSpan]];

            //Ширина ячейки
            if (cell.Width != 0)
            {
                range.ColumnWidth = cell.Width;
            }

            //Высота ячейки
            if (cell.Height != 0)
            {
                range.RowHeight = cell.Height;
            }

            //Объединение ячеек по вертикали
            if (cell.RowSpan != 0)
            {
                range.Merge();
            }

            //Объеинение ячеек по горизонтали
            if (cell.ColumnSpan != 0)
            {
                range.Merge();
            }

            //Цвет фона ячейки
            if (cell.BackgroundColor != null)
            {
                range.Interior.Color = ColorTranslator.ToOle((Color)cell.BackgroundColor);
            }

            //Цвет текста ячейки
            if (cell.TextColor != null)
            {
                range.Font.Color = ColorTranslator.ToOle((Color)cell.TextColor);
            }

            //Размер шрифта ячейки
            if (cell.FontSize != 0)
            {
                range.Font.Size = cell.FontSize;
            }

            //Формат
            switch (cell.Format)
            {
            case Format.Money:
                range.NumberFormat = "#,##0.00$"; break;
            }

            /* Форматы ячеек
             * '000000'
             * '0.000'  числовой с 3 цифрами после точки
             * '0.0_ ;[Red]-0.0' отрицательные числа красным цветом
             * 'General' общий
             * '#,##0.00$' денежный
             * 'd/m/yyyy;@' дата зависит от настроек ОС
             * 'm/d/yyyy' дата
             * '@' текстовый
             */

            //Горизонтальное положение текста в ячейке
            switch (cell.HorizontalAlignment)
            {
            case HorizontalAlignment.Left:
                range.Cells.HorizontalAlignment = XlHAlign.xlHAlignLeft; break;

            case HorizontalAlignment.Center:
                range.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter; break;

            case HorizontalAlignment.Right:
                range.Cells.HorizontalAlignment = XlHAlign.xlHAlignRight; break;

            default:
                range.Cells.HorizontalAlignment = XlHAlign.xlHAlignLeft; break;
            }

            //Вертикальное положение текста в ячейке
            switch (cell.VerticalAlignment)
            {
            case VerticalAlignment.Top:
                range.Cells.VerticalAlignment = XlVAlign.xlVAlignTop; break;

            case VerticalAlignment.Center:
                range.Cells.VerticalAlignment = XlVAlign.xlVAlignCenter; break;

            case VerticalAlignment.Bottom:
                range.Cells.VerticalAlignment = XlVAlign.xlVAlignBottom; break;

            default:
                range.Cells.VerticalAlignment = XlVAlign.xlVAlignBottom; break;
            }

            //Стили текста
            if (cell.TextStyle != null && cell.TextStyle.Count > 0)
            {
                for (int i = 0; i < cell.TextStyle.Count; i++)
                {
                    switch (cell.TextStyle[i])
                    {
                    case TextStyle.Bold:
                        range.Font.Bold = true; break;

                    case TextStyle.Italic:
                        range.Font.Italic = true; break;
                    }
                }
            }

            //Границы ячейки
            if (cell.Border != null && cell.Border.Count > 0)
            {
                for (int i = 0; i < cell.Border.Count; i++)
                {
                    switch (cell.Border[i])
                    {
                    case Border.All:
                        if (cell.BorderColor != null)
                        {
                            range.Borders.Color = ColorTranslator.ToOle((Color)cell.BorderColor);
                        }
                        PrepareCellBorderWeight(cell, range);
                        PrepareCellBorderStyle(cell, range);

                        break;

                    case Border.Left:
                        if (cell.BorderColor != null)
                        {
                            range.Borders[XlBordersIndex.xlEdgeLeft].Color = ColorTranslator.ToOle((Color)cell.BorderColor);
                        }
                        PrepareCellBorderWeight(cell, range, XlBordersIndex.xlEdgeLeft);
                        PrepareCellBorderStyle(cell, range, XlBordersIndex.xlEdgeLeft);

                        break;

                    case Border.Top:
                        if (cell.BorderColor != null)
                        {
                            range.Borders[XlBordersIndex.xlEdgeTop].Color = ColorTranslator.ToOle((Color)cell.BorderColor);
                        }
                        PrepareCellBorderWeight(cell, range, XlBordersIndex.xlEdgeTop);
                        PrepareCellBorderStyle(cell, range, XlBordersIndex.xlEdgeTop);

                        break;

                    case Border.Right:
                        if (cell.BorderColor != null)
                        {
                            range.Borders[XlBordersIndex.xlEdgeRight].Color = ColorTranslator.ToOle((Color)cell.BorderColor);
                        }
                        PrepareCellBorderWeight(cell, range, XlBordersIndex.xlEdgeRight);
                        PrepareCellBorderStyle(cell, range, XlBordersIndex.xlEdgeRight);

                        break;

                    case Border.Bottom:
                        if (cell.BorderColor != null)
                        {
                            range.Borders[XlBordersIndex.xlEdgeBottom].Color = ColorTranslator.ToOle((Color)cell.BorderColor);
                        }
                        PrepareCellBorderWeight(cell, range, XlBordersIndex.xlEdgeBottom);
                        PrepareCellBorderStyle(cell, range, XlBordersIndex.xlEdgeBottom);

                        break;
                    }
                }
            }
            else
            {
                if (cell.BorderColor != null)
                {
                    range.Borders.Color = ColorTranslator.ToOle((Color)cell.BorderColor);
                }
                PrepareCellBorderStyle(cell, range);
                PrepareCellBorderWeight(cell, range);
            }
        }