SetBorder() public method

Set the table cell border Added by lckuiper @ 20101117
public SetBorder ( TableCellBorderType borderType, Border border ) : void
borderType TableCellBorderType Table Cell border to set
border Border Border object to set the table cell border
return void
Beispiel #1
0
 private void ExportWord()
 {
     if (Tables != null)
     {
         var columns = Columns;
         using (Docx.DocX doc = Docx.DocX.Create(FileName))
         {
             //表格的边框样式
             Docx.Border border = new Docx.Border();
             border.Tcbs = Docx.BorderStyle.Tcbs_single;
             int n = 0;
             foreach (TableEntity tableEntity in Tables)
             {
                 string tableId = tableEntity.ID.ToString();
                 //插入表名
                 Docx.Paragraph title = doc.InsertParagraph();
                 title.Append(tableEntity.TableName + "(" + tableEntity.Attr + ")");
                 title.Alignment = Docx.Alignment.center;
                 title.FontSize(15);
                 title.Bold();
                 title.SetLineSpacing(Docx.LineSpacingType.After, 1);
                 title.SetLineSpacing(Docx.LineSpacingType.Before, 1);
                 DataTable fields   = service.GetColumnDataTable(tableId);
                 int       rowCount = (fields == null ? 0 : fields.Rows.Count) + 1;
                 //计算表格多少行,多少列
                 Docx.Table table = doc.InsertTable(rowCount, columns.Count);
                 //先生成列头
                 Docx.Row colRow = table.Rows[0];
                 int      k      = 0;
                 foreach (string colKey in columns.Keys)
                 {
                     Docx.Cell colCell = colRow.Cells[k];
                     colCell.Paragraphs[0].Append(columns[colKey]).Alignment = Docx.Alignment.center;
                     colCell.SetBorder(Docx.TableCellBorderType.Top, border);
                     colCell.SetBorder(Docx.TableCellBorderType.Bottom, border);
                     colCell.SetBorder(Docx.TableCellBorderType.Left, border);
                     colCell.SetBorder(Docx.TableCellBorderType.Right, border);
                     k++;
                 }
                 for (int i = 0; i < fields.Rows.Count; i++)
                 {
                     //一个属性为一行
                     Docx.Row row = table.Rows[i + 1];
                     //循环每列
                     int j = 0;
                     foreach (string key in columns.Keys)
                     {
                         Docx.Cell cell = row.Cells[j];
                         string    text = fields.Rows[i][key].ToString();
                         if (key == requiredKey)
                         {
                             text = text.ToLower() == "true" ? "是" : "";
                         }
                         cell.Paragraphs[0].Append(text).Alignment = Docx.Alignment.center;
                         cell.Paragraphs[0].FontSize(10);
                         cell.SetBorder(Docx.TableCellBorderType.Top, border);
                         cell.SetBorder(Docx.TableCellBorderType.Bottom, border);
                         cell.SetBorder(Docx.TableCellBorderType.Left, border);
                         cell.SetBorder(Docx.TableCellBorderType.Right, border);
                         j++;
                     }
                 }
                 n++;
                 if (OnProgress != null)
                 {
                     OnProgress(this, new ProgressEventArgs()
                     {
                         Max = Tables.Count, Value = n
                     });
                 }
             }
             doc.Save();
         }
     }
 }
        /// <summary>
        /// Appends the common visual style to this cell.
        /// </summary>
        /// <param name="cell">The cell.</param>
        /// <param name="style">The style.</param>
        /// <returns>
        /// A <see cref="T:Novacode.Paragraph" /> with the common style.
        /// </returns>
        private static Paragraph AppendCommonVisualStyle(this Cell cell, StyleModel style)
        {
            cell.FillColor         = style.Content.GetColor();
            cell.VerticalAlignment = style.Content.Alignment.Vertical.ToDocxVerticalAlignment();

            foreach (var borderModel in style.Borders)
            {
                var border = new Border
                {
                    Space = 0,
                    Color = borderModel.GetColor(),
                    Size  = borderModel.Weight.ToDocxBorderSize(),
                    Tcbs  = borderModel.LineStyle.ToDocxLineStyle()
                };

                switch (borderModel.Position)
                {
                case KnownBorderPosition.Left:
                    cell.SetBorder(TableCellBorderType.Left, border);
                    break;

                case KnownBorderPosition.Top:
                    cell.SetBorder(TableCellBorderType.Top, border);
                    break;

                case KnownBorderPosition.Right:
                    cell.SetBorder(TableCellBorderType.Right, border);
                    break;

                case KnownBorderPosition.Bottom:
                    cell.SetBorder(TableCellBorderType.Bottom, border);
                    break;

                case KnownBorderPosition.DiagonalLeft:
                    cell.SetBorder(TableCellBorderType.TopRightToBottomLeft, border);
                    break;

                case KnownBorderPosition.DiagonalRight:
                    cell.SetBorder(TableCellBorderType.TopLeftToBottomRight, border);
                    break;
                }
            }

            var paragraph = cell.Paragraphs.First();

            paragraph.Alignment = style.Content.Alignment.Horizontal.ToDocxHorizontalAlignment();
            paragraph.Font(style.Font.ToFont().FontFamily);
            paragraph.FontSize(style.Font.Size); // int.Parse(style.Font.Size, CultureInfo.InvariantCulture));

            if (style.Font.Bold == YesNo.Yes)
            {
                paragraph.Bold();
            }

            if (style.Font.Italic == YesNo.Yes)
            {
                paragraph.Italic();
            }

            if (style.Font.Underline == YesNo.Yes)
            {
                paragraph.UnderlineStyle(UnderlineStyle.singleLine);
            }

            return(paragraph);
        }
 private void SetBorder(Cell cell)
 {
     cell.SetBorder(TableCellBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.Black));
     cell.SetBorder(TableCellBorderType.Bottom, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.Black));
     cell.SetBorder(TableCellBorderType.Left, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.Black));
     cell.SetBorder(TableCellBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.Black));
 }