Exemplo n.º 1
0
        internal int CalcCellHeight(int Row, int Col, TRichString val, int CellXF, ExcelFile Workbook, real RowMultDisplay, real ColMultDisplay, TMultipleCellAutofitList MultipleRowAutofits)
        {
            if (CellXF < 0)
            {
                return(0xFF);
            }
            if (CellXF >= XFHeight.Length)
            {
                return(0xFF);                                        //Just to make sure. We don't want a wrong file to blow here.
            }
            int Result  = XFHeight[CellXF];
            int Result0 = Result;

            if (val == null)
            {
                return(Result);
            }

            TXFRecord XFRec = Wg.CellXF[CellXF];
            bool      Vertical;
            real      Alpha = FlexCelRender.CalcAngle(XFRec.Rotation, out Vertical);


            if (!XFRec.WrapText && !Vertical && Alpha == 0)
            {
                return(Result);
            }

            Font AFont = gr.FontCache.GetFont(XFFonts[CellXF], 1);

            RectangleF    CellRect = new RectangleF();
            TXlsCellRange rg       = Workbook.CellMergedBounds(Row, Col);

            for (int c = rg.Left; c <= rg.Right; c++)
            {
                CellRect.Width += Workbook.GetColWidth(c, true) / ColMultDisplay;
            }

            SizeF            TextExtent;
            TXRichStringList TextLines  = new TXRichStringList();
            TFloatList       MaxDescent = new TFloatList();

            real Clp = 1 * FlexCelRender.DispMul / 100f;
            real Wr  = 0;

            if (Alpha == 0)
            {
                Wr = CellRect.Right - CellRect.Left - 2 * Clp;
            }
            else
            {
                Wr = 1e6f;                 //When we have an angle, it means "make the row as big as needed to fit". So SplitText needs no limits.
            }


            RenderMetrics.SplitText(gr.Canvas, gr.FontCache, 1, val, AFont, Wr, TextLines, out TextExtent, Vertical, MaxDescent, null);

            if (TextLines.Count <= 0)
            {
                return(Result);
            }

            real H = 0;
            real W = 0;

            for (int i = 0; i < TextLines.Count; i++)
            {
                H += TextLines[i].YExtent;
                if (TextLines[i].XExtent > W)
                {
                    W = TextLines[i].XExtent;
                }
            }

            if (Alpha != 0)
            {
                real SinAlpha = (real)Math.Sin(Alpha * Math.PI / 180); real CosAlpha = (real)Math.Cos(Alpha * Math.PI / 180);
                H = H * CosAlpha + W * Math.Abs(SinAlpha);
            }
            Result = (int)Math.Ceiling(RowMultDisplay * (H + 2 * Clp));

            if (rg.RowCount > 1)
            {
                if (MultipleRowAutofits != null)
                {
                    MultipleRowAutofits.Add(new TMultipleCellAutofit(rg, Result));
                }
                return(Result0);                //We will autofit this later.
            }
            if (Result < 0)
            {
                Result = 0;
            }

            return(Result);
        }
Exemplo n.º 2
0
        internal static void Write(TextWriter OutString, ExcelFile Workbook, TXlsCellRange Range,
                                   int[] ColumnWidths, int CharactersForFirstColumn, bool ExportHiddenRowsOrColumns, bool ExportTextOutsideCells)
        {
            if (Range == null)
            {
                Range = new TXlsCellRange(1, 1, Workbook.RowCount, Workbook.GetColCount(Workbook.ActiveSheet, false));
            }

            for (int r = Range.Top; r <= Range.Bottom; r++)
            {
                if (!ExportHiddenRowsOrColumns && Workbook.GetRowHidden(r))
                {
                    continue;
                }

                int    cIndex        = 0;
                double FirstColWidth = Workbook.GetColWidth(Range.Left);
                string Remaining     = string.Empty;

                int            AcumColLen   = 0;
                bool           InMergedCell = false;
                THFlxAlignment MergedAlign  = THFlxAlignment.general;
                TCellType      MergedType   = TCellType.Unknown;
                int            OrigColLen   = 0;

                for (int c = Range.Left; c <= Range.Right; c++)
                {
                    if (!ExportHiddenRowsOrColumns && Workbook.GetColHidden(c))
                    {
                        continue;
                    }
                    string     s   = Workbook.GetStringFromCell(r, c).ToString();
                    TFlxFormat fmt = null;
                    if (ExportTextOutsideCells)
                    {
                        fmt = Workbook.GetCellVisibleFormatDef(r, c);
                    }

                    if (string.IsNullOrEmpty(s))
                    {
                        s = Remaining;
                    }

                    int ColLen = 0;
                    if (ColumnWidths == null)
                    {
                        if (CharactersForFirstColumn <= 0)
                        {
                            ColLen = s.Length;
                        }
                        else if (FirstColWidth <= 0)
                        {
                            ColLen = 0;
                        }
                        else
                        {
                            ColLen = (int)Math.Round((double)CharactersForFirstColumn * Workbook.GetColWidth(c) / FirstColWidth);
                        }
                    }
                    else
                    {
                        if (cIndex >= ColumnWidths.Length)
                        {
                            break;
                        }
                        ColLen = ColumnWidths[cIndex];
                    }

                    cIndex++;
                    if (InMergedCell)
                    {
                        OrigColLen += ColLen;
                    }
                    else
                    {
                        OrigColLen = ColLen;
                    }

                    if (s.Length == 0)
                    {
                        AcumColLen += ColLen;
                        continue;
                    }

                    THFlxAlignment HAlign = THFlxAlignment.left;
                    TCellType      CellType;
                    if (InMergedCell)
                    {
                        HAlign   = MergedAlign;
                        CellType = MergedType;
                    }
                    else
                    {
                        Object   CellVal = Workbook.GetCellValue(r, c);
                        TFormula fmla    = CellVal as TFormula;
                        if (fmla != null)
                        {
                            CellVal = fmla.Result;
                        }
                        CellType = TExcelTypes.ObjectToCellType(CellVal);
                        if (ExportTextOutsideCells && fmt != null && Remaining.Length == 0)
                        {
                            HAlign = GetDataAlign(CellType, fmt);
                        }
                    }

                    if (HAlign == THFlxAlignment.left)
                    {
                        OutString.Write(new string(' ', AcumColLen));
                    }
                    else
                    {
                        TXlsCellRange mr = Workbook.CellMergedBounds(r, c);
                        InMergedCell = mr.Right > c;
                        if (mr.Right > c)
                        {
                            AcumColLen += ColLen;
                            if (c == mr.Left)
                            {
                                Remaining   = s;
                                MergedAlign = HAlign;
                                MergedType  = CellType;
                            }
                            continue;
                        }
                        if (mr.Right > mr.Left)
                        {
                            s         = Remaining;
                            Remaining = string.Empty;
                        }

                        MergedAlign  = THFlxAlignment.left;
                        MergedType   = TCellType.Unknown;
                        InMergedCell = false;
                        ColLen      += AcumColLen;
                    }
                    AcumColLen = 0;

                    if (s.Length > ColLen)
                    {
                        if (ExportTextOutsideCells && HAlign == THFlxAlignment.right)
                        {
                            if (CellType == TCellType.Number)
                            {
                                OutString.Write(new string('#', ColLen));
                            }
                            else
                            {
                                OutString.Write(s.Substring(s.Length - ColLen));
                            }
                        }
                        else
                        {
                            OutString.Write(s.Substring(0, ColLen));
                        }
                        if (ExportTextOutsideCells && HAlign != THFlxAlignment.right)
                        {
                            Remaining = s.Substring(ColLen);
                        }
                    }
                    else
                    {
                        int Pad = ColLen - s.Length;
                        if (ExportTextOutsideCells && Remaining.Length == 0)
                        {
                            Pad = TextAlign(OutString, HAlign, s.Length, ColLen, OrigColLen);
                        }
                        OutString.Write(s);
                        OutString.Write(new string(' ', Pad));
                        Remaining = string.Empty;
                    }
                }

                if (ExportTextOutsideCells && Remaining.Length > 0)
                {
                    OutString.Write(Remaining);
                }
                Remaining = string.Empty;
                OutString.Write(TCompactFramework.NewLine);
            }
        }
Exemplo n.º 3
0
        internal int CalcCellWidth(int Row, int Col, TRichString val, int CellXF, ExcelFile Workbook, real RowMultDisplay, real ColMultDisplay, TMultipleCellAutofitList MultipleColAutofits)
        {
            if (val == null || val.Value == null || val.Value.Length == 0)
            {
                return(0);
            }

            TXFRecord XFRec = Wg.CellXF[CellXF];
            bool      Vertical;
            real      Alpha = FlexCelRender.CalcAngle(XFRec.Rotation, out Vertical);

            Font AFont = gr.FontCache.GetFont(XFFonts[CellXF], 1);             //dont dispose

            TXlsCellRange rg = Workbook.CellMergedBounds(Row, Col);

            SizeF            TextExtent;
            TXRichStringList TextLines  = new TXRichStringList();
            TFloatList       MaxDescent = new TFloatList();

            real Clp = 1 * FlexCelRender.DispMul / 100f;
            real Wr  = 0;

            if (Alpha != 90 && Alpha != -90)
            {
                Wr = 1e6f;                                          //this means "make the column as big as needed to fit". So SplitText needs no limits.
            }
            else
            {
                RectangleF CellRect = new RectangleF();
                for (int r = rg.Top; r <= rg.Bottom; r++)
                {
                    CellRect.Height += Workbook.GetRowHeight(r, true) / RowMultDisplay;
                }
                Wr = CellRect.Height - 2 * Clp;
            }


            RenderMetrics.SplitText(gr.Canvas, gr.FontCache, 1, val, AFont, Wr, TextLines, out TextExtent, Vertical, MaxDescent, null);

            if (TextLines.Count <= 0)
            {
                return(0);
            }

            real Rr = 0;
            real Ww = 0;

            for (int i = 0; i < TextLines.Count; i++)
            {
                Rr += TextLines[i].YExtent;
                if (TextLines[i].XExtent > Ww)
                {
                    Ww = TextLines[i].XExtent;
                }
            }
            if (Alpha != 0)
            {
                real SinAlpha = (real)Math.Sin(Alpha * Math.PI / 180); real CosAlpha = (real)Math.Cos(Alpha * Math.PI / 180);
                Ww = Ww * CosAlpha + Rr * Math.Abs(SinAlpha);
            }
            int Result = (int)Math.Ceiling(ColMultDisplay * (Ww + 4 * Clp));

            if (rg.ColCount > 1)
            {
                if (MultipleColAutofits != null)
                {
                    MultipleColAutofits.Add(new TMultipleCellAutofit(rg, Result));
                }
                return(0);                //We will autofit this later.
            }

            if (Result < 0)
            {
                Result = 0;
            }
            return(Result);
        }