Exemplo n.º 1
0
        private void RenderPhotoRow(PhotoRowLayout layout, ITableRow row)
        {
            //	In the design we represent a photo row as a pair of table rows
            //	without a table: one row for the photos, and the other for the
            //	captions. This design is useful for PDF, and requires special
            //	handling of borders to make the vertical pairs of cells (photo
            //	cell and caption cell) appear as a single cell. But it's not
            //	necessary in Word because we can just add the photo and caption
            //	as two paragraphs in a single cell. So deconstruct the photo
            //	row into its pairs of cells and then render each pair as a
            //	single cell.
            for (int x = 0; x < layout.NumPhotos; ++x)
            {
                PhotoLayout photoCell   = (PhotoLayout)layout.PhotoRow.GetSubLayoutAtIndex(x).GetSubLayoutAtIndex(0);
                TextLayout  captionCell = (TextLayout)layout.CaptionRow.GetSubLayoutAtIndex(x).GetSubLayoutAtIndex(0);

                s.TableCellStyle cellStyle = null;
                if (layout.Style != null)
                {
                    s.PhotoStyle photoStyle = (s.PhotoStyle)layout.Style;
                    cellStyle = new s.TableCellStyle
                    {
                        Padding = photoStyle.Padding
                    };
                }
                ITableCell cell = row.AddCell(1, cellStyle, layout.TrackingInfo);
                RenderPhoto(photoCell, cell);
                RenderText(captionCell, cell);
            }
        }
Exemplo n.º 2
0
        public ITableCell NewCell()
        {
            ITableCell cell = CreateCell();

            AddCell(cell);

            return(cell);
        }
Exemplo n.º 3
0
        protected void ExportToPdfButton_Click(object sender, EventArgs e)
        {
            ReportFactory.SetStyle1ForPDF();
            PdfDocument document = new PdfDocument(PageSize.A4);

            document.PageOrientation = PageOrientation.Landscape;

            ITable table = document.NewTable(2);

            ITableRow row1 = table.NewRow();

            row1.BackgoundColor = ReportColor.Red;
            row1.TextColor      = ReportColor.While;

            ITableCell cell11 = row1.NewCell();

            cell11.Value = "Tiêu đề báo cáo";
            cell11.HorizontalAlignment = HorizontalAlignment.Center;
            cell11.Colspan             = 2;

            ITableCell cell21 = row1.NewCell();

            cell21.Value = "Tiêu đề báo cáo 2";
            cell21.HorizontalAlignment = HorizontalAlignment.Left;


            ITableRow row2 = table.NewRow();

            ITableCell cell12 = row2.NewCell();

            cell12.Value = "Tiêu đề báo cáo dòng 2";
            cell12.HorizontalAlignment = HorizontalAlignment.Right;

            IParagraph p1 = new Paragraph("Hướng dẫn sử dụng thư viện xuất ra tệp PDF.");

            IText textBold = new Text(" Bold");

            textBold.Style = FontStyle.Bold;
            p1.AddText(textBold);

            document.AddElement(p1);

            IParagraph p2 = new Paragraph("Hướng dẫn sử dụng thư viện xuất ra tệp PDF.");

            p2.PaddingBottom = 10f;

            IText textBold2 = new Text(" Bold");

            textBold2.Style = FontStyle.Bold;
            p2.AddText(textBold);

            document.AddElement(p2);

            document.InsertFromExcel(MapPath("~/Uploads/data_empty.xlsx"));
            document.InsertFromExcel(MapPath("~/Uploads/data.xlsx"));

            document.Save(MapPath("~/Uploads/b.pdf"));
        }
    public bool Draw(DrawingContext context, ref Rect rect, ITableCell cell)
    {
        if (cell is not WdeCell wde)
        {
            return(false);
        }

        if (wde.Value is string s)
        {
            return(false);
        }

        if (wde.Value is long l)
        {
            return(false);
        }

        if (wde.Value is double d)
        {
            return(false);
        }


        if (wde.Value is bool b)
        {
            var size         = 20;
            var checkboxRect = new Rect(rect.Center.X - size / 2, rect.Center.Y - size / 2, size, size);
            context.FillRectangle(CheckBackgroundBrush, checkboxRect, 4);
            if (b)
            {
                context.DrawLine(CheckPen, new Point(rect.Center.X - 5, rect.Center.Y), new Point(rect.Center.X - 1, rect.Center.Y + 4));
                context.DrawLine(CheckPen, new Point(rect.Center.X + 5, rect.Center.Y - 4), new Point(rect.Center.X - 2, rect.Center.Y + 3));
            }
            return(true);
        }

        if (wde.Value is System.Action)
        {
            rect = rect.Deflate(3);

            context.DrawRectangle(ButtonBackgroundPen.Brush, ButtonBorderPen, rect, 4, 4);

            var state = context.PushClip(rect);
            var ft    = new FormattedText
            {
                Text       = "Click",
                Constraint = new Size(rect.Width, rect.Height),
                Typeface   = Typeface.Default,
                FontSize   = 12
            };
            context.DrawText(ButtonTextPen.Brush, new Point(rect.Center.X - ft.Bounds.Width / 2, rect.Center.Y - ft.Bounds.Height / 2), ft);
            state.Dispose();

            return(true);
        }

        return(false);
    }
Exemplo n.º 5
0
            private string BuildCellMarkdownCode(int column, ITableCell cell)
            {
                var columnSpec        = _columnRenderSpecs[column];
                var maximumWidth      = columnSpec.MaximumWidth;
                var cellText          = cell.BuildCodeFormattedString(new TableCellRenderSpecification(columnSpec.Alignment, maximumWidth));
                var truncatedCellText = cellText.Length > maximumWidth?cellText.Substring(0, maximumWidth) : cellText.PadRight(maximumWidth);

                return(truncatedCellText);
            }
Exemplo n.º 6
0
        private void RenderTableCell(TableCellLayout layout, ITableRow row)
        {
            ITableCell cell = row.AddCell(layout.ColumnSpan, (s.TableCellStyle)layout.Style, layout.TrackingInfo);

            foreach (Layout sublayout in layout.SubLayouts)
            {
                Render(sublayout, cell);
            }
        }
Exemplo n.º 7
0
        public void CellIsMergedCell_ReturnsTrue_WhenCellMergedWithOtherHorizontally()
        {
            // Arrange
            SCTableRow scTableRow = ((ITable)_fixture.Pre001.Slides[1].Shapes.First(sp => sp.Id == 4)).Rows[1];
            ITableCell cell1x0    = scTableRow.Cells[0];
            ITableCell cell1x1    = scTableRow.Cells[1];

            // Act-Assert
            cell1x0.IsMergedCell.Should().BeTrue();
            cell1x1.IsMergedCell.Should().BeTrue();
            cell1x0.Should().BeSameAs(cell1x1);
        }
Exemplo n.º 8
0
        protected void ExportToPdfMergeRowButton_Click(object sender, EventArgs e)
        {
            ReportFactory.SetStyle1ForPDF();
            PdfDocument document = new PdfDocument(PageSize.A0);

            document.PageOrientation = PageOrientation.Landscape;

            document.InsertFromExcel(MapPath("~/Uploads/Report_4_TongHopCallReport.xlsx"));

            ReportColor green    = ReportColor.Create(0, 100, 45);
            ReportColor textHong = ReportColor.Create(230, 184, 183);
            ReportColor textKhds = ReportColor.Create(99, 37, 35);
            ReportColor textKhln = ReportColor.Create(118, 147, 60);
            ReportColor textThds = ReportColor.Create(96, 73, 122);
            ReportColor textThln = ReportColor.Create(49, 134, 155);

            document.Save(MapPath("~/Uploads/Report_4_TongHopCallReport.pdf"), (paragraphs) =>
            {
                foreach (IElementRoot paragraph in paragraphs)
                {
                    if (paragraph is ITable)
                    {
                        ITable table   = paragraph as ITable;
                        ITableRow row1 = table.Rows[0];

                        for (int i = 1; i <= row1.Cells.Count; i++)
                        {
                            ITableCell cell     = row1.Cells[i - 1];
                            cell.BackgoundColor = green;

                            if (i > 3)
                            {
                                cell.TextColor = textHong;
                            }
                            else
                            {
                                cell.TextColor = ReportColor.While;
                            }
                        }

                        ITableRow row3 = table.Rows[2];
                        for (int i = 1; i <= row3.Cells.Count; i++)
                        {
                            ITableCell cell     = row3.Cells[i - 1];
                            cell.TextColor      = ReportColor.While;
                            cell.BackgoundColor = green;
                        }
                    }
                }

                return(paragraphs);
            });
        }
Exemplo n.º 9
0
        internal void ItemRender(HtmlTextWriter writer, int index)
        {
            writer.AddAttribute("role", "row");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            foreach (var item in DataSource)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Td);

                ITableCell cell = Columns.ElementAt(index).GetCell(item);
                writer.Write(cell.Value);
                writer.RenderEndTag();                 // Td
            }

            writer.RenderEndTag();             // Tr
        }
Exemplo n.º 10
0
        void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            if (_currentRowIndex == -1 || _currentRowIndex != e.RowIndex)
            {
                _currentRowIndex = e.RowIndex;
                _currentRow      = _table[e.RowIndex];
            }

            ITableCell cell = _currentRow[e.ColumnIndex];

            e.Value = cell.ToString();
            if (cell.Depth > 1)
            {
                //dataGridView1[e.ColumnIndex, e.RowIndex].Style.Font = new Font(dataGridView1.DefaultCellStyle.Font, FontStyle.Bold);
                dataGridView1[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Blue;
            }
        }
    public bool Draw(DrawingContext context, ref Rect rect, ITableCell c)
    {
        if (c is not SingleRecordDatabaseCellViewModel cell)
        {
            return(false);
        }

        if (cell.ActionCommand != null)
        {
            DrawButton(context, rect, cell.ActionLabel, 3, cell.ActionCommand.CanExecute(null));
            return(true);
        }

        if (cell.IsModified && cell.Parent.Entity.ExistInDatabase)
        {
            context.DrawRectangle(null, ModifiedCellPen, rect);
            // don't return true, because we want to draw original value anyway
        }

        if (cell.HasItems && !cell.UseFlagsPicker && !cell.UseItemPicker)
        {
            if (rect.Contains(mouseCursor))
            {
                var threeDotRect = GetThreeDotRectForCell(rect);
                DrawButton(context, threeDotRect, "...", 3);
                rect = rect.Deflate(new Thickness(0, 0, threeDotRect.Width, 0));
            }
        }

        if (cell.ParameterValue?.BaseParameter is IItemParameter && cell.TableField is DatabaseField <long> longField)
        {
            var icons = ViewBind.ResolveViewModel <IItemIconsService>();
            var icn   = icons.GetIcon((uint)longField.Current.Value);
            if (icn != null)
            {
                context.DrawImage(icn, new Rect(rect.X, rect.Center.Y - 18 / 2, 18, 18));
            }
            rect = rect.Deflate(new Thickness(20, 0, 0, 0));
        }

        return(false);
    }
Exemplo n.º 12
0
        public ITableCell CreateCell()
        {
            ITableCell cell = TableCell.Create();

            if (BackgoundColor != null)
            {
                cell.BackgoundColor = BackgoundColor;
            }

            if (FontList.HasValue)
            {
                cell.FontList = FontList;
            }

            if (FontSize.HasValue)
            {
                cell.FontSize = FontSize;
            }

            if (TextColor != null)
            {
                cell.TextColor = TextColor;
            }

            if (Style.HasValue)
            {
                cell.Style = Style;
            }

            if (HorizontalAlignment.HasValue)
            {
                cell.HorizontalAlignment = HorizontalAlignment;
            }

            if (VerticalAlignment.HasValue)
            {
                cell.VerticalAlignment = VerticalAlignment;
            }

            return(cell);
        }
            private void AddHeaderCell(ITableHeader header, string text, Width width = null)
            {
                IText      headerText;
                ITableCell cell = header.AddCell();

                Color startColor = new Color(117, 117, 117);
                Color endColor   = new Color(84, 84, 84);

                cell.Borders            = bordersStyle;
                cell.Background         = new Background(new LinearGradientBrush(startColor, endColor, 90));
                cell.Alignment.Vertical = Alignment.Middle;

                if (width != null)
                {
                    cell.Width = width;
                }

                headerText = cell.AddText();

                headerText.Style.Font  = headerFont;
                headerText.Style.Brush = Brushes.White;
                headerText.Alignment   = TextAlignment.Center;
                headerText.AddContent(text);
            }
Exemplo n.º 14
0
        public void MergeCells(ITableCell inputCell1, ITableCell inputCell2) // TODO: Optimize method
        {
            SCTableCell cell1 = (SCTableCell)inputCell1;
            SCTableCell cell2 = (SCTableCell)inputCell2;

            if (CannotBeMerged(cell1, cell2))
            {
                return;
            }

            int minRowIndex = cell1.RowIndex < cell2.RowIndex ? cell1.RowIndex : cell2.RowIndex;
            int maxRowIndex = cell1.RowIndex > cell2.RowIndex ? cell1.RowIndex : cell2.RowIndex;
            int minColIndex = cell1.ColumnIndex < cell2.ColumnIndex ? cell1.ColumnIndex : cell2.ColumnIndex;
            int maxColIndex = cell1.ColumnIndex > cell2.ColumnIndex ? cell1.ColumnIndex : cell2.ColumnIndex;

            // Horizontal merging
            List <A.TableRow> aTableRowList = this.ATable.Elements <A.TableRow>().ToList();

            if (minColIndex != maxColIndex)
            {
                int horizontalMergingCount = maxColIndex - minColIndex + 1;
                for (int rowIdx = minRowIndex; rowIdx <= maxRowIndex; rowIdx++)
                {
                    A.TableCell[] rowATblCells     = aTableRowList[rowIdx].Elements <A.TableCell>().ToArray();
                    A.TableCell   firstMergingCell = rowATblCells[minColIndex];
                    firstMergingCell.GridSpan = new Int32Value(horizontalMergingCount);
                    Span <A.TableCell> nextMergingCells =
                        new Span <A.TableCell>(rowATblCells, minColIndex + 1, horizontalMergingCount - 1);
                    foreach (A.TableCell aTblCell in nextMergingCells)
                    {
                        aTblCell.HorizontalMerge = new BooleanValue(true);

                        MergeParagraphs(minRowIndex, minColIndex, aTblCell);
                    }
                }
            }

            // Vertical merging
            if (minRowIndex != maxRowIndex)
            {
                // Set row span value for the first cell in the merged cells
                int verticalMergingCount = maxRowIndex - minRowIndex + 1;
                IEnumerable <A.TableCell> rowSpanCells = aTableRowList[minRowIndex].Elements <A.TableCell>()
                                                         .Skip(minColIndex)
                                                         .Take(maxColIndex + 1);
                foreach (A.TableCell aTblCell in rowSpanCells)
                {
                    aTblCell.RowSpan = new Int32Value(verticalMergingCount);
                }

                // Set vertical merging flag
                foreach (A.TableRow aTblRow in aTableRowList.Skip(minRowIndex + 1).Take(maxRowIndex))
                {
                    foreach (A.TableCell aTblCell in aTblRow.Elements <A.TableCell>().Take(maxColIndex + 1))
                    {
                        aTblCell.VerticalMerge = new BooleanValue(true);

                        MergeParagraphs(minRowIndex, minColIndex, aTblCell);
                    }
                }
            }

            // Delete a:gridCol and a:tc elements if all columns are merged
            for (int colIdx = 0; colIdx < Columns.Count;)
            {
                int?gridSpan = ((SCTableCell)Rows[0].Cells[colIdx]).ATableCell.GridSpan?.Value;
                if (gridSpan > 1 && Rows.All(row =>
                                             ((SCTableCell)row.Cells[colIdx]).ATableCell.GridSpan?.Value == gridSpan))
                {
                    int deleteColumnCount = gridSpan.Value - 1;

                    // Delete a:gridCol elements
                    foreach (Column column in Columns.Skip(colIdx + 1).Take(deleteColumnCount))
                    {
                        column.AGridColumn.Remove();
                        Columns[colIdx].Width += column.Width; // append width of deleting column to merged column
                    }

                    // Delete a:tc elements
                    foreach (A.TableRow aTblRow in aTableRowList)
                    {
                        IEnumerable <A.TableCell> removeCells =
                            aTblRow.Elements <A.TableCell>().Skip(colIdx).Take(deleteColumnCount);
                        foreach (A.TableCell aTblCell in removeCells)
                        {
                            aTblCell.Remove();
                        }
                    }

                    colIdx += gridSpan.Value;
                    continue;
                }

                colIdx++;
            }

            // Delete a:tr
            for (int rowIdx = 0; rowIdx < Rows.Count;)
            {
                int?rowSpan = ((SCTableCell)Rows[rowIdx].Cells[0]).ATableCell.RowSpan?.Value;
                if (rowSpan > 1 && Rows[rowIdx].Cells.All(c => ((SCTableCell)c).ATableCell.RowSpan?.Value == rowSpan))
                {
                    int deleteRowsCount = rowSpan.Value - 1;

                    // Delete a:gridCol elements
                    foreach (SCTableRow row in Rows.Skip(rowIdx + 1).Take(deleteRowsCount))
                    {
                        row.ATableRow.Remove();
                        Rows[rowIdx].Height += row.Height;
                    }

                    rowIdx += rowSpan.Value;
                    continue;
                }

                rowIdx++;
            }

            rowCollection.Reset();
        }
Exemplo n.º 15
0
 public void AddCell(ITableCell cell)
 {
     Cells.Add(cell);
 }
Exemplo n.º 16
0
 private string BuildCellMarkdownCode(int column, ITableCell cell)
 {
     var columnSpec = _columnRenderSpecs[column];
     var maximumWidth = columnSpec.MaximumWidth;
     var cellText = cell.BuildCodeFormattedString(new TableCellRenderSpecification(columnSpec.Alignment, maximumWidth));
     var truncatedCellText = cellText.Length > maximumWidth ? cellText.Substring(0, maximumWidth) : cellText.PadRight(maximumWidth);
     
     return truncatedCellText;
 }
Exemplo n.º 17
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="writer"></param>
        public override void WriteHtml(HtmlTextWriter writer)
        {
            RouteValueDictionary routeValues = new RouteValueDictionary(htmlAttributes);

            foreach (var attribute in routeValues)
            {
                writer.AddAttribute(attribute.Key, attribute.Value.ToString());
            }

            writer.AddAttribute("role", TableName);
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            #region Head

            writer.AddAttribute("role", "rowgroup");
            writer.RenderBeginTag(HtmlTextWriterTag.Thead);

            writer.AddAttribute("role", "row");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            foreach (var column in Columns)
            {
                writer.AddAttribute("role", "columnheader");
                //writer.AddAttribute("data-role", "sorter");
                writer.RenderBeginTag(HtmlTextWriterTag.Th);
                writer.Write("<a href='#'>{0}</a>", column.Title);
                writer.RenderEndTag();                 // Th
            }

            writer.RenderEndTag();             // Tr

            writer.RenderEndTag();             // Thead

            #endregion

            #region Body

            writer.RenderBeginTag(HtmlTextWriterTag.Tbody);

            if (Orientation == OrientationTypes.Horizontal)
            {
                foreach (var item in DataSource)
                {
                    if (!string.IsNullOrEmpty(RowId))
                    {
                        writer.AddAttribute("id", item.GetType().GetProperty(RowId).GetValue(item, null).ToString());
                    }

                    if (!string.IsNullOrEmpty(RowClass))
                    {
                        var isRead = item.GetType().GetProperty(RowClass).GetValue(item, null).ToString();
                        writer.AddAttribute("class", (isRead == "True") ? "read" : "unread");
                    }

                    writer.AddAttribute("role", "row");
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                    foreach (var column in Columns)
                    {
                        ITableCell cell = column.GetCell(item);

                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write(cell.Value);
                        writer.RenderEndTag();                         // Td
                    }

                    writer.RenderEndTag();                     // Tr
                }
            }
            else
            {
                ItemRenderGroup(writer);
            }

            writer.RenderEndTag();             // Tbody

            #endregion

            #region Foot

            if (IsPageable)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tfoot);

                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                writer.AddAttribute("colspan", Enumerable.Count(Columns).ToString());
                writer.RenderBeginTag(HtmlTextWriterTag.Td);

                writer.AddAttribute("class", "table-pager-wrapper");
                writer.AddAttribute("data-role", "pager");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);


                writer.WriteLine("<span class='table-pager-prev'><i class='glyphicon glyphicon-chevron-left'></i> Prev</span>");
                writer.WriteLine("<span class='table-pager-info'>Showing {0} of {1} items</span>", Enumerable.Count(DataSource), Enumerable.Count(DataSource));
                writer.WriteLine("<span class='table-pager-next'>Next <i class='glyphicon glyphicon-chevron-right'></i></span>");


                writer.RenderEndTag();                 // Div

                writer.RenderEndTag();                 // Td

                writer.RenderEndTag();                 // Tr

                writer.RenderEndTag();                 // Tfoot
            }

            #endregion

            writer.RenderEndTag();             // Table
        }
Exemplo n.º 18
0
 public TableColumn()
 {
     _headerCell = new EmptyTableCell();
     Alignment = TableColumnAlignment.Unspecified;
 }
Exemplo n.º 19
0
        protected void ExportToPdfMergeRowButton_Click(object sender, EventArgs e)
        {
            ReportFactory.SetStyle1ForPDF();
            PdfDocument document = new PdfDocument(PageSize.A0);

            document.PageOrientation = PageOrientation.Landscape;

            //ITable table = document.NewTable(3);
            //ITableRow row1 = table.NewRow();
            //row1.BackgoundColor = ReportColor.Red;
            //row1.TextColor = ReportColor.While;

            //ITableCell r1c123 = row1.NewCell();
            //r1c123.Value = "R1C1 - R2C1 - R3C1";
            //r1c123.HorizontalAlignment = HorizontalAlignment.Center;
            //r1c123.VerticalAlignment = VerticalAlignment.Middle;
            //r1c123.Rowspan = 3;

            //ITableCell r1c2 = row1.NewCell();
            //r1c2.Value = "R1C2";

            //ITableCell r1c3 = row1.NewCell();
            //r1c3.Value = "R1C3";


            //ITableRow row2 = table.NewRow();

            //ITableCell r2c23 = row2.NewCell();
            //r2c23.Colspan = 2;
            //r2c23.Value = "R2C2 - R2C3";

            ////ITableCell r2c3 = row2.NewCell();
            ////r2c3.Value = "R2C3";


            //ITableRow row3 = table.NewRow();

            //ITableCell r3c2 = row3.NewCell();
            //r3c2.Value = "R3C2";

            //ITableCell r3c3 = row3.NewCell();
            //r3c3.Value = "R3C3";

            document.InsertFromExcel(MapPath("~/Uploads/data_merge_row.xlsx"));

            ReportColor green    = ReportColor.Create(0, 100, 45);
            ReportColor textHong = ReportColor.Create(230, 184, 183);
            ReportColor textKhds = ReportColor.Create(99, 37, 35);
            ReportColor textKhln = ReportColor.Create(118, 147, 60);
            ReportColor textThds = ReportColor.Create(96, 73, 122);
            ReportColor textThln = ReportColor.Create(49, 134, 155);

            document.Save(MapPath("~/Uploads/data_merge_row.pdf"), (paragraphs) =>
            {
                foreach (IElementRoot paragraph in paragraphs)
                {
                    if (paragraph is ITable)
                    {
                        ITable table   = paragraph as ITable;
                        ITableRow row1 = table.Rows[0];

                        for (int i = 1; i <= row1.Cells.Count; i++)
                        {
                            ITableCell cell     = row1.Cells[i - 1];
                            cell.BackgoundColor = green;

                            if (i >= 3)
                            {
                                cell.TextColor = textHong;
                            }
                            else
                            {
                                cell.TextColor = ReportColor.While;
                            }
                        }

                        ITableRow row4 = table.Rows[3];
                        for (int i = 1; i <= row4.Cells.Count; i++)
                        {
                            ITableCell cell = row4.Cells[i - 1];
                            cell.TextColor  = ReportColor.While;
                            if (i <= 10)
                            {
                                cell.BackgoundColor = green;
                            }
                            else if (i == 11)
                            {
                                cell.BackgoundColor = textKhds;
                            }
                            else if (i == 12)
                            {
                                cell.BackgoundColor = textKhln;
                            }
                            else if (i == 13)
                            {
                                cell.BackgoundColor = textThds;
                            }
                            else if (i == 14)
                            {
                                cell.BackgoundColor = textThln;
                            }
                        }

                        ITableRow row5 = table.Rows[4];
                        for (int i = 1; i <= row5.Cells.Count; i++)
                        {
                            ITableCell cell = row5.Cells[i - 1];
                            cell.TextColor  = ReportColor.While;

                            if (i <= 11)
                            {
                                cell.BackgoundColor = textKhds;
                            }
                            else if (i <= 18)
                            {
                                cell.BackgoundColor = textKhln;
                            }
                            else if (i <= 29)
                            {
                                cell.BackgoundColor = textThds;
                            }
                            else
                            {
                                cell.BackgoundColor = textThln;
                            }
                        }
                    }
                }

                return(paragraphs);
            });
        }
Exemplo n.º 20
0
 public void CellIsMergedCell_ReturnsTrue_WhenCellMergedWithOtherVertically(ITableCell cell1, ITableCell cell2)
 {
     cell1.IsMergedCell.Should().BeTrue();
     cell2.IsMergedCell.Should().BeTrue();
     cell1.Should().Be(cell2);
 }
Exemplo n.º 21
0
        /// <summary>
        /// Hỗ trợ định dạng mới của excel (*.xlsx).
        /// </summary>
        /// <param name="fileName"></param>
        public void InsertFromExcel(string fileName)
        {
            FileInfo excelFile = new FileInfo(fileName);

            using (var excel = new ExcelPackage(excelFile, false))
            {
                ExcelWorkbook   workbook   = excel.Workbook;
                ExcelWorksheets worksheets = workbook.Worksheets;
                foreach (ExcelWorksheet sheet in worksheets)
                {
                    ExcelAddressBase activeRange = sheet.Dimension;

                    if (activeRange != null)
                    {
                        int columnLength = activeRange.Columns;
                        int rowLength    = activeRange.Rows;

                        ExcelRange cells = sheet.Cells[activeRange.Start.Row, activeRange.Start.Column, activeRange.End.Row, activeRange.End.Column];
                        // ExcelStyle fullStyle = cells.Style;

                        ITable table = Table.Create(columnLength);
                        // Duyệt từng dòng.
                        for (int i = 1; i <= rowLength; i++)
                        {
                            ITableRow tableRow = table.NewRow();
                            // Duyệt từng cột.
                            //int columnIndex = 1;
                            for (int j = 1; j <= columnLength; j++)
                            {
                                ITableCell tableCell = tableRow.NewCell();

                                ExcelRange cell = sheet.Cells[i, j];

                                // tableCell.BackgoundColor = cell.Style.Fill.BackgroundColor.ToReportColorByExcelColor();
                                var style = cell.Style;
                                var fill  = style.Fill;
                                var font  = style.Font;

                                // border
                                if (style.Border.Top.Style == ExcelBorderStyle.None)
                                {
                                    tableCell.Border.Top.Style = BorderStyle.None;
                                }
                                else
                                {
                                    tableCell.Border.Top.Style = BorderStyle.Solid;
                                }

                                if (style.Border.Right.Style == ExcelBorderStyle.None)
                                {
                                    tableCell.Border.Right.Style = BorderStyle.None;
                                }
                                else
                                {
                                    tableCell.Border.Right.Style = BorderStyle.Solid;
                                }

                                if (style.Border.Bottom.Style == ExcelBorderStyle.None)
                                {
                                    tableCell.Border.Bottom.Style = BorderStyle.None;
                                }
                                else
                                {
                                    tableCell.Border.Bottom.Style = BorderStyle.Solid;
                                }

                                if (style.Border.Left.Style == ExcelBorderStyle.None)
                                {
                                    tableCell.Border.Left.Style = BorderStyle.None;
                                }
                                else
                                {
                                    tableCell.Border.Left.Style = BorderStyle.Solid;
                                }

                                if (string.IsNullOrEmpty(cell.Text))
                                {
                                    // Gán cell đầu tiên của hàng là 1 space để nếu hàng có tất cả các cell empty thì in ra PDF vẫn có hàng đó (mặc định không hiển thị).
                                    if (j == 1)
                                    {
                                        tableCell.Value = " ";
                                    }
                                }
                                else
                                {
                                    if (cell.IsRichText)
                                    {
                                        tableCell.Value = cell.Text;
                                    }
                                    else
                                    {
                                        tableCell.Value = cell.Text.Replace("\n", " ");
                                    }

                                    if (font.Bold && font.Italic)
                                    {
                                        tableCell.Style = FontStyle.BoldItalic;
                                    }
                                    else if (font.Bold)
                                    {
                                        tableCell.Style = FontStyle.Bold;
                                    }
                                    else if (font.Italic)
                                    {
                                        tableCell.Style = FontStyle.Italic;
                                    }

                                    tableCell.FontSize = font.Size;

                                    // align
                                    tableCell.HorizontalAlignment = style.HorizontalAlignment.ToHorizontalAlignment();
                                    tableCell.VerticalAlignment   = style.VerticalAlignment.ToVerticalAlignment();
                                }
                            }
                        }

                        // caculate merge cells.
                        MergeCellsCollection mergeCells = sheet.MergedCells;
                        foreach (string mergeCell in mergeCells)
                        {
                            var mergeRange  = sheet.Cells[mergeCell];
                            int startRow    = mergeRange.Start.Row;
                            int startColumn = mergeRange.Start.Column;
                            int endRow      = mergeRange.End.Row;
                            int endColumn   = mergeRange.End.Column;

                            int colspan = endColumn - startColumn + 1;
                            int rowspan = endRow - startRow + 1;
                            for (int i = startRow; i <= endRow; i++)
                            {
                                for (int j = startColumn; j <= endColumn; j++)
                                {
                                    ITableCell cellMerge = table.Rows[i - 1].Cells[j - 1];
                                    if (i == startRow && j == startColumn)
                                    {
                                        if (colspan > 1)
                                        {
                                            cellMerge.Colspan = colspan;
                                        }

                                        if (rowspan > 1)
                                        {
                                            cellMerge.Rowspan = rowspan;
                                        }
                                    }
                                    else
                                    {
                                        ((TableCell)cellMerge).IsMerge = true;
                                    }
                                }
                            }
                        }

                        // Xoá tất cả cell merge có Colspan <= 1 && Rowspan <= 1
                        // IEnumerable<ITableCell> mergeCellsTmp = table.Rows.SelectMany(row => row.Cells).Where(cell => cell.IsMerge && (!cell.Colspan.HasValue || cell.Colspan <= 1) && (!cell.Rowspan.HasValue || cell.Rowspan <= 1));
                        foreach (ITableRow row in table.Rows)
                        {
                            row.Cells.RemoveAll(cell => cell.IsMerge && (!cell.Colspan.HasValue || cell.Colspan <= 1) && (!cell.Rowspan.HasValue || cell.Rowspan <= 1));
                        }

                        AddElement(table);
                    }
                }
            }
        }
Exemplo n.º 22
0
 public TableColumn()
 {
     _headerCell = new EmptyTableCell();
     Alignment   = TableColumnAlignment.Unspecified;
 }