예제 #1
0
        private static void CreateCell(ExcelRange cell, object value, CustomExcelStyle excelStyle)
        {
            cell.Merge                = excelStyle.IsMerge;
            cell.Value                = value;
            cell.Style.Font.Name      = "Arial";
            cell.Style.Font.Bold      = excelStyle.IsBold;
            cell.Style.Font.Italic    = excelStyle.IsItalic;
            cell.Style.Font.UnderLine = excelStyle.IsUnderLine;
            cell.Style.Font.Size      = excelStyle.FontSize.HasValue ? excelStyle.FontSize.Value : (float)10;
            if (!string.IsNullOrWhiteSpace(excelStyle.Formula))
            {
                cell.Formula = excelStyle.Formula;
            }

            if (excelStyle.Color.HasValue)
            {
                cell.Style.Font.Color.SetColor(excelStyle.Color.Value);
            }

            if (excelStyle.BackgroundColor.HasValue)
            {
                var fill = cell.Style.Fill;
                fill.PatternType = ExcelFillStyle.Solid;
                fill.BackgroundColor.SetColor(excelStyle.BackgroundColor.Value);
            }

            cell.Style.HorizontalAlignment = excelStyle.HorizontalAlign.HasValue ? excelStyle.HorizontalAlign.Value : ExcelHorizontalAlignment.Center;
            cell.Style.VerticalAlignment   = excelStyle.VerticleAlign.HasValue ? excelStyle.VerticleAlign.Value : ExcelVerticalAlignment.Center;

            if (excelStyle.IsWrapText.HasValue)
            {
                cell.Style.WrapText = excelStyle.IsWrapText.Value;
            }
            if (!excelStyle.AutoFit.HasValue || excelStyle.AutoFit.Value)
            {
                cell.AutoFitColumns();
            }

            if (!string.IsNullOrEmpty(excelStyle.NumberFormat))
            {
                cell.Style.Numberformat.Format = excelStyle.NumberFormat;
            }
            cell.Calculate();
            SetBorder(excelStyle, cell);
        }
예제 #2
0
        private static void SetBorder(CustomExcelStyle excelStyle, ExcelRange range)
        {
            var border = range.Style.Border;

            if (excelStyle.Border.HasValue)
            {
                border.Top.Style    = excelStyle.Border.Value;
                border.Bottom.Style = excelStyle.Border.Value;
                border.Left.Style   = excelStyle.Border.Value;
                border.Right.Style  = excelStyle.Border.Value;
            }
            else
            {
                border.Top.Style    = excelStyle.BorderTop.HasValue ? excelStyle.BorderTop.Value : ExcelBorderStyle.None;
                border.Bottom.Style = excelStyle.BorderBottom.HasValue ? excelStyle.BorderBottom.Value : ExcelBorderStyle.None;
                border.Left.Style   = excelStyle.BorderLeft.HasValue ? excelStyle.BorderLeft.Value : ExcelBorderStyle.None;
                border.Right.Style  = excelStyle.BorderRight.HasValue ? excelStyle.BorderRight.Value : ExcelBorderStyle.None;
            }
            if (excelStyle.BorderColor.HasValue)
            {
                border.Top.Color.SetColor(excelStyle.BorderColor.Value);
                border.Bottom.Color.SetColor(excelStyle.BorderColor.Value);
                border.Left.Color.SetColor(excelStyle.BorderColor.Value);
                border.Right.Color.SetColor(excelStyle.BorderColor.Value);
            }
            else
            {
                if (excelStyle.BorderTopColor.HasValue)
                {
                    border.Top.Color.SetColor(excelStyle.BorderTopColor.Value);
                }
                if (excelStyle.BorderBottomColor.HasValue)
                {
                    border.Bottom.Color.SetColor(excelStyle.BorderBottomColor.Value);
                }
                if (excelStyle.BorderLeftColor.HasValue)
                {
                    border.Left.Color.SetColor(excelStyle.BorderLeftColor.Value);
                }
                if (excelStyle.BorderRightColor.HasValue)
                {
                    border.Right.Color.SetColor(excelStyle.BorderRightColor.Value);
                }
            }
        }
예제 #3
0
 public static void CreateCellTable(ExcelWorksheet sheet, int row, int col, object value, CustomExcelStyle excelStyle)
 {
     CreateCell(sheet.Cells[row, col], value, excelStyle);
 }
예제 #4
0
 public static void CreateCellTable(ExcelWorksheet sheet, int fromRow, int fromCol, int toRow, int toCol, object value, CustomExcelStyle excelStyle)
 {
     CreateCell(sheet.Cells[fromRow, fromCol, toRow, toCol], value, excelStyle);
 }
예제 #5
0
        public static void CreateCellTable(ExcelWorksheet sheet, string range, object value, CustomExcelStyle excelStyle)
        {
            ExcelRange cellRange = sheet.Cells[range];

            CreateCell(cellRange, value, excelStyle);
        }