Пример #1
0
        private void BuildStyle_Border(IWorkbook workbook, StringBuilder style,
                                       String type, BorderStyle xlsBorder, short borderColor)
        {
            if (xlsBorder == BorderStyle.None)
            {
                return;
            }

            StringBuilder borderStyle = new StringBuilder();

            borderStyle.Append(ExcelToHtmlUtils.GetBorderWidth(xlsBorder));
            borderStyle.Append(' ');
            borderStyle.Append(ExcelToHtmlUtils.GetBorderStyle(xlsBorder));

            if (workbook is HSSFWorkbook)
            {
                var       customPalette = ((HSSFWorkbook)workbook).GetCustomPalette();
                HSSFColor color         = null;
                if (customPalette != null)
                {
                    color = customPalette.GetColor(borderColor);
                }
                if (color != null)
                {
                    borderStyle.Append(' ');
                    borderStyle.Append(ExcelToHtmlUtils.GetColor(color));
                }
            }
            else
            {
                IndexedColors clr = IndexedColors.ValueOf(borderColor);
                if (clr != null)
                {
                    borderStyle.Append(' ');
                    borderStyle.Append(clr.HexString);
                }
                else
                {
                    XSSFColor color        = null;
                    var       stylesSource = ((XSSFWorkbook)workbook).GetStylesSource();
                    if (stylesSource != null)
                    {
                        var theme = stylesSource.GetTheme();
                        if (theme != null)
                        {
                            color = theme.GetThemeColor(borderColor);
                        }
                    }
                    if (color != null)
                    {
                        borderStyle.Append(' ');
                        borderStyle.Append(ExcelToHtmlUtils.GetColor(color));
                    }
                }
            }
            style.AppendFormat("border-{0}: {1}; ", type, borderStyle);
        }
Пример #2
0
        void BuildStyle_Font(IWorkbook workbook, StringBuilder style,
                             IFont font)
        {
            switch (font.Boldweight)
            {
            case (short)FontBoldWeight.Bold:
                style.Append("font-weight: bold; ");
                break;

            case (short)FontBoldWeight.Normal:
                // by default, not not increase HTML size
                // style.Append( "font-weight: normal; " );
                break;
            }

            if (workbook is HSSFWorkbook)
            {
                HSSFColor fontColor = ((HSSFWorkbook)workbook).GetCustomPalette()?.GetColor(font.Color);
                if (fontColor != null)
                {
                    style.AppendFormat("color:{0}; ", ExcelToHtmlUtils.GetColor(fontColor));
                }
            }
            else
            {
                IndexedColors clr       = IndexedColors.ValueOf(font.Color);
                string        hexstring = null;
                if (clr != null)
                {
                    hexstring = clr.HexString;
                }
                else
                {
                    StylesTable st        = ((XSSFWorkbook)workbook).GetStylesSource();
                    XSSFColor   fontColor = null;
                    if (st != null && st.GetTheme() != null)
                    {
                        fontColor = st.GetTheme().GetThemeColor(font.Color);
                    }
                    else
                    {
                        fontColor = ((XSSFFont)font).GetXSSFColor();
                    }
                    if (fontColor != null)
                    {
                        hexstring = ExcelToHtmlUtils.GetColor(fontColor);
                    }
                }
                if (hexstring != null)
                {
                    style.AppendFormat("color:{0}; ", hexstring);
                }
            }
            if (font.FontHeightInPoints != 0)
            {
                style.Append("font-size: " + font.FontHeightInPoints + "pt; ");
            }

            if (font.IsItalic)
            {
                style.Append("font-style: italic; ");
            }
        }
Пример #3
0
        protected String BuildStyle(IWorkbook workbook, ICellStyle cellStyle)
        {
            StringBuilder style = new StringBuilder();

            if (workbook is HSSFWorkbook)
            {
                HSSFPalette palette = ((HSSFWorkbook)workbook).GetCustomPalette();
                style.Append("white-space: pre-wrap; ");
                ExcelToHtmlUtils.AppendAlign(style, cellStyle.Alignment);

                if (cellStyle.FillPattern == FillPattern.NoFill)
                {
                    // no fill
                }
                else if (cellStyle.FillPattern == FillPattern.SolidForeground)
                {
                    //cellStyle.
                    //HSSFColor.
                    HSSFColor foregroundColor = palette.GetColor(cellStyle.FillForegroundColor);
                    if (foregroundColor != null)
                    {
                        style.AppendFormat("background-color:{0}; ", ExcelToHtmlUtils.GetColor(foregroundColor));
                    }
                }
                else
                {
                    HSSFColor backgroundColor = palette.GetColor(cellStyle.FillBackgroundColor);
                    if (backgroundColor != null)
                    {
                        style.AppendFormat("background-color:{0}; ", ExcelToHtmlUtils.GetColor(backgroundColor));
                    }
                }
            }
            else
            {
                style.Append("white-space: pre-wrap; ");
                ExcelToHtmlUtils.AppendAlign(style, cellStyle.Alignment);

                if (cellStyle.FillPattern == FillPattern.NoFill)
                {
                    // no fill
                }
                else if (cellStyle.FillPattern == FillPattern.SolidForeground)
                {
                    //cellStyle
                    IndexedColors clr       = IndexedColors.ValueOf(cellStyle.FillForegroundColor);
                    string        hexstring = null;
                    if (clr != null)
                    {
                        hexstring = clr.HexString;
                    }
                    else
                    {
                        XSSFColor foregroundColor = (XSSFColor)cellStyle.FillForegroundColorColor;
                        if (foregroundColor != null)
                        {
                            hexstring = ExcelToHtmlUtils.GetColor(foregroundColor);
                        }
                    }
                    if (hexstring != null)
                    {
                        style.AppendFormat("background-color:{0}; ", hexstring);
                    }
                }
                else
                {
                    IndexedColors clr       = IndexedColors.ValueOf(cellStyle.FillBackgroundColor);
                    string        hexstring = null;
                    if (clr != null)
                    {
                        hexstring = clr.HexString;
                    }
                    else
                    {
                        XSSFColor backgroundColor = (XSSFColor)cellStyle.FillBackgroundColorColor;
                        if (backgroundColor != null)
                        {
                            hexstring = ExcelToHtmlUtils.GetColor(backgroundColor);
                        }
                    }
                    if (hexstring != null)
                    {
                        style.AppendFormat("background-color:{0}; ", hexstring);
                    }
                }
            }

            BuildStyle_Border(workbook, style, "top", cellStyle.BorderTop, cellStyle.TopBorderColor);
            BuildStyle_Border(workbook, style, "right", cellStyle.BorderRight, cellStyle.RightBorderColor);
            BuildStyle_Border(workbook, style, "bottom", cellStyle.BorderBottom, cellStyle.BottomBorderColor);
            BuildStyle_Border(workbook, style, "left", cellStyle.BorderLeft, cellStyle.LeftBorderColor);

            IFont font = cellStyle.GetFont(workbook);

            BuildStyle_Font(workbook, style, font);

            return(style.ToString());
        }