Пример #1
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);
        }
Пример #2
0
        protected bool IsTextEmpty(HSSFCell cell)
        {
            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)
                    {
                        return(false);
                    }

                    value = str.ToString();
                    break;

                case CellType.NUMERIC:
                    HSSFCellStyle style = cell.CellStyle as HSSFCellStyle;
                    if (style == null)
                    {
                        return(false);
                    }

                    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:
                    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:
                return(true);
            }

            return(string.IsNullOrEmpty(value));
        }