Ejemplo n.º 1
0
        protected String BuildStyle(HSSFWorkbook workbook, HSSFCellStyle cellStyle)
        {
            StringBuilder style   = new StringBuilder();
            HSSFPalette   palette = workbook.GetCustomPalette();

            style.Append("white-space: pre-wrap; ");
            ExcelToHtmlUtils.AppendAlign(style, cellStyle.Alignment);

            if (cellStyle.FillPattern == FillPatternType.NO_FILL)
            {
                // no fill
            }
            else if (cellStyle.FillPattern == FillPatternType.SOLID_FOREGROUND)
            {
                //cellStyle.
                //HSSFColor.
                HSSFColor foregroundColor = palette.GetColor(cellStyle.FillForegroundColor);
                if (foregroundColor != null)
                {
                    style.Append("background-color: " + ExcelToHtmlUtils.GetColor(foregroundColor) + "; ");
                }
            }
            else
            {
                HSSFColor backgroundColor = palette.GetColor(cellStyle.FillBackgroundColor);
                if (backgroundColor != null)
                {
                    style.Append("background-color: " + ExcelToHtmlUtils.GetColor(backgroundColor) + "; ");
                }
            }

            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);

            HSSFFont font = cellStyle.GetFont(workbook) as HSSFFont;

            BuildStyle_Font(workbook, style, font);

            return(style.ToString());
        }
Ejemplo n.º 2
0
        protected bool ProcessCell(HSSFCell cell, XmlElement tableCellElement,
                                   int normalWidthPx, int maxSpannedWidthPx, float normalHeightPt)
        {
            HSSFCellStyle cellStyle = cell.CellStyle as HSSFCellStyle;

            string value;

            switch (cell.CellType)
            {
            case CellType.STRING:
                // XXX: enrich
                value = cell.RichStringCellValue.String;
                break;

            case CellType.FORMULA:
                switch (cell.CachedFormulaResultType)
                {
                case CellType.STRING:
                    HSSFRichTextString str = cell.RichStringCellValue as HSSFRichTextString;
                    if (str != null && str.Length > 0)
                    {
                        value = (str.String);
                    }
                    else
                    {
                        value = string.Empty;
                    }
                    break;

                case CellType.NUMERIC:
                    HSSFCellStyle style = cellStyle;
                    if (style == null)
                    {
                        value = cell.NumericCellValue.ToString();
                    }
                    else
                    {
                        value = (_formatter.FormatRawCellContents(cell.NumericCellValue, style.DataFormat, style.GetDataFormatString()));
                    }
                    break;

                case CellType.BOOLEAN:
                    value = cell.BooleanCellValue.ToString();
                    break;

                case CellType.ERROR:
                    value = ErrorEval.GetText(cell.ErrorCellValue);
                    break;

                default:
                    logger.Log(POILogger.WARN, "Unexpected cell cachedFormulaResultType (" + cell.CachedFormulaResultType.ToString() + ")");
                    value = string.Empty;
                    break;
                }
                break;

            case CellType.BLANK:
                value = string.Empty;
                break;

            case CellType.NUMERIC:
                value = _formatter.FormatCellValue(cell);
                break;

            case CellType.BOOLEAN:
                value = cell.BooleanCellValue.ToString();
                break;

            case CellType.ERROR:
                value = ErrorEval.GetText(cell.ErrorCellValue);
                break;

            default:
                logger.Log(POILogger.WARN, "Unexpected cell type (" + cell.CellType.ToString() + ")");
                return(true);
            }

            bool noText     = string.IsNullOrEmpty(value);
            bool wrapInDivs = !noText && UseDivsToSpan && !cellStyle.WrapText;

            short cellStyleIndex = cellStyle.Index;

            if (cellStyleIndex != 0)
            {
                HSSFWorkbook workbook     = cell.Row.Sheet.Workbook as HSSFWorkbook;
                string       mainCssClass = GetStyleClassName(workbook, cellStyle);
                if (wrapInDivs)
                {
                    tableCellElement.SetAttribute("class", mainCssClass + " "
                                                  + cssClassContainerCell);
                }
                else
                {
                    tableCellElement.SetAttribute("class", mainCssClass);
                }

                if (noText)
                {
                    /*
                     * if cell style is defined (like borders, etc.) but cell text
                     * is empty, add " " to output, so browser won't collapse
                     * and ignore cell
                     */
                    value = "\u00A0"; //“ ”全角空格
                }
            }

            if (OutputLeadingSpacesAsNonBreaking && value.StartsWith(" "))
            {
                StringBuilder builder = new StringBuilder();
                for (int c = 0; c < value.Length; c++)
                {
                    if (value[c] != ' ')
                    {
                        break;
                    }
                    builder.Append('\u00a0');
                }

                if (value.Length != builder.Length)
                {
                    builder.Append(value.Substring(builder.Length));
                }

                value = builder.ToString();
            }

            XmlText text = htmlDocumentFacade.CreateText(value);

            if (wrapInDivs)
            {
                XmlElement outerDiv = htmlDocumentFacade.CreateBlock();
                outerDiv.SetAttribute("class", this.cssClassContainerDiv);

                XmlElement    innerDiv      = htmlDocumentFacade.CreateBlock();
                StringBuilder innerDivStyle = new StringBuilder();
                innerDivStyle.Append("position:absolute;min-width:");
                innerDivStyle.Append(normalWidthPx);
                innerDivStyle.Append("px;");
                if (maxSpannedWidthPx != int.MaxValue)
                {
                    innerDivStyle.Append("max-width:");
                    innerDivStyle.Append(maxSpannedWidthPx);
                    innerDivStyle.Append("px;");
                }
                innerDivStyle.Append("overflow:hidden;max-height:");
                innerDivStyle.Append(normalHeightPt);
                innerDivStyle.Append("pt;white-space:nowrap;");
                ExcelToHtmlUtils.AppendAlign(innerDivStyle, cellStyle.Alignment);
                htmlDocumentFacade.AddStyleClass(outerDiv, "d", innerDivStyle.ToString());

                innerDiv.AppendChild(text);
                outerDiv.AppendChild(innerDiv);
                tableCellElement.AppendChild(outerDiv);
            }
            else
            {
                tableCellElement.AppendChild(text);
            }

            return(string.IsNullOrEmpty(value) && cellStyleIndex == 0);
        }