public int SetCellAlignH(int Row, int Col, THFlxAlignment Ha)
        {
            int        cellFormat = this.GetCellFormat(Row, Col);
            TFlxFormat format     = this.GetFormat(cellFormat);

            format.HAlignment = Ha;
            cellFormat        = this.AddFormat(format);
            this.SetCellFormat(Row, Col, cellFormat);
            return(cellFormat);
        }
Beispiel #2
0
        private static int TextAlign(TextWriter OutString, THFlxAlignment HAlign, int SLen, int ColLen, int OrigColLen)
        {
            int Pad = ColLen - SLen;

            switch (HAlign)
            {
            case THFlxAlignment.center:
                int P2 = (OrigColLen - SLen) / 2;
                if (P2 < 0)
                {
                    OutString.Write(new string(' ', Pad));
                    return(0);
                }

                OutString.Write(new string(' ', ColLen - OrigColLen + P2));
                return(OrigColLen - P2 - SLen);

            case THFlxAlignment.right:
                OutString.Write(new string(' ', Pad));
                return(0);
            }
            return(Pad);
        }
Beispiel #3
0
        internal static void DrawRichText(ExcelFile Workbook, IFlxGraphics Canvas, TFontCache FontCache, real Zoom100, bool ReverseRightToLeftStrings,
                                          ref RectangleF CellRect, ref RectangleF PaintClipRect, ref RectangleF TextRect, ref RectangleF ContainingRect,
                                          real Clp, THFlxAlignment HJustify, TVFlxAlignment VJustify, real Alpha,
                                          Color DrawFontColor, TSubscriptData SubData, TRichString OutText, SizeF TextExtent,
                                          TXRichStringList TextLines,
                                          Font AFont, TFloatList MaxDescent, real[] X, real[] Y)
        {
            RectangleF FinalRect = CellRect;

            if (ContainingRect.Right > PaintClipRect.Left &&
                ContainingRect.Left < PaintClipRect.Right &&
                Intersect(TextRect, PaintClipRect, out FinalRect))
            {
                real dy             = 0;
                int  TextLinesCount = TextLines.Count;

                if (VJustify == TVFlxAlignment.justify || VJustify == TVFlxAlignment.distributed)
                {
                    if (TextLinesCount > 1)
                    {
                        dy = (CellRect.Height - TextExtent.Height - 2 * Clp) / (TextLinesCount - 1);
                    }
                }

                float eps = 0.0001F; //Tolerance for the comparison, these are floats, not doubles, and we get rounding errors soon.
                if (TextRect.Left - eps <= ContainingRect.Left && TextRect.Right + eps >= ContainingRect.Right && TextRect.Top - eps <= ContainingRect.Top && TextRect.Bottom + eps >= ContainingRect.Bottom)
                {
                    Canvas.SetClipReplace(PaintClipRect);                      //This will improve pdf export a lot, since we can now join all the BT/ET tags inside one, and then we can also join fonts inside the ET/BT tags.
                }
                else
                {
                    if (Alpha == 0 || Alpha == 90)
                    {
                        Canvas.SetClipReplace(FinalRect);
                    }
                    else
                    {
                        Canvas.SetClipReplace(RectangleF.FromLTRB(PaintClipRect.Left, FinalRect.Top, PaintClipRect.Right, FinalRect.Bottom));                         //rotated text can move to other used cells horizontally.
                    }
                }

                real AcumDy = 0;
                for (int i = 0; i < TextLinesCount; i++)
                {
                    TXRichString TextLine = TextLines[i];
                    if ((Alpha == 0) &&
                        (HJustify == THFlxAlignment.justify && TextLine.Split) ||
                        (HJustify == THFlxAlignment.distributed)
                        )
                    {
                        Canvas.SetClipReplace(FinalRect);
                        WriteJustText(Workbook, Canvas, FontCache, Zoom100, ReverseRightToLeftStrings,
                                      AFont, DrawFontColor, Y[i] + AcumDy, SubData.Offset(Canvas, AFont), CellRect, Clp, TextLine.s,
                                      TextLine.XExtent, MaxDescent[i], HJustify == THFlxAlignment.distributed, TextLine.AdaptFormat);
                    }
                    else
                    {
                        WriteText(Workbook, Canvas, FontCache, Zoom100, AFont, DrawFontColor, X[i],
                                  Y[i] + AcumDy, SubData.Offset(Canvas, AFont), GetVisualString(TextLine.s, ReverseRightToLeftStrings),
                                  Alpha, MaxDescent[i], TextLine.AdaptFormat);
                    }
                    AcumDy += dy;
                }
            }
        }
Beispiel #4
0
        internal void SetBorder(XlsFile xls, Rectangle area, bool has, THFlxAlignment hAlignment)
        {
            TFlxBorderStyle borderStyle = TFlxBorderStyle.None;

            if (has)
            {
                borderStyle = TFlxBorderStyle.Thin;
            }

            TFlxFormat format = xls.GetDefaultFormat;
            format.Borders.Bottom.Style = borderStyle;
            format.Borders.Top.Style = borderStyle;
            format.Borders.Left.Style = borderStyle;
            format.Borders.Right.Style = borderStyle;
            format.HAlignment = hAlignment;

            //format.Font.Style = TFlxFontStyles.Bold;

            // back color
            //
            //format.FillPattern.FgColorIndex = xls.NearestColorIndex(Color.Red);
            //format.FillPattern.Pattern = TFlxPatternStyle.Solid;
            int xf = xls.AddFormat(format);

            //int xf = GetThinBorderXF(xls);
            xls.SetCellFormat(area.Top, area.Left, area.Bottom - 1, area.Right - 1, xf);
        }
Beispiel #5
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);
            }
        }