Table

  • Demo
  • C# source
  • VB.Net source
This demo shows you how to create a table with specified data in a Word document. We also show you how to set the border and background color of the table.
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the demo you want, please contact support@e-iceblue.com for the free customized demo just for you.
using System;
using System.Data;
using System.Drawing;

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Formatting;


namespace DemoOnlineCode
{
    class Table
    {
        public void demoTable(String docFile, DataTable dataTable,
            string borderColor = null,
            string headerBackColor = null,
            string rowBackColor = null,
            string alternationRowColor = null)
        {
            Document document = new Document(docFile, FileFormat.Auto);
            addTable(document.Sections[0], dataTable,
               ConvertStrToColor(borderColor),
               ConvertStrToColor(headerBackColor),
               ConvertStrToColor(rowBackColor),
               ConvertStrToColor(alternationRowColor));
            document.SaveToFile("demo.doc", FileFormat.Doc);
        }

        private void addTable(Section section,
            DataTable dataTable,
            Color borderColor,
            Color headerBackColor,
            Color rowBackColor,
            Color alternationRowColor)
        {
            Spire.Doc.Table table = section.AddTable();
            int rowCount = dataTable.Rows.Count;
            int columnCount = dataTable.Columns.Count;
            table.DefaultRowHeight = 25;
            table.DefaultColumnWidth = 0;
            table.ResetCells(rowCount + 1, columnCount);

            table.TableFormat.Borders.Left.BorderType
                = Spire.Doc.Documents.BorderStyle.Hairline;
            table.TableFormat.Borders.Left.Color = borderColor;

            table.TableFormat.Borders.Top.BorderType
                = Spire.Doc.Documents.BorderStyle.Hairline;
            table.TableFormat.Borders.Top.Color = borderColor;

            table.TableFormat.Borders.Right.BorderType
                = Spire.Doc.Documents.BorderStyle.Hairline;
            table.TableFormat.Borders.Right.Color = borderColor;

            table.TableFormat.Borders.Bottom.BorderType
                = Spire.Doc.Documents.BorderStyle.Hairline;
            table.TableFormat.Borders.Bottom.Color = borderColor;

            table.TableFormat.Borders.Horizontal.BorderType
                = Spire.Doc.Documents.BorderStyle.Hairline;
            table.TableFormat.Borders.Horizontal.Color = borderColor;

            table.TableFormat.Borders.Vertical.BorderType
                = Spire.Doc.Documents.BorderStyle.Hairline;
            table.TableFormat.Borders.Vertical.Color = borderColor;


            TableRow headerRow = table.Rows[0];
            headerRow.IsHeader = true;

            for (int c = 0; c < columnCount; c++)
            {
                Paragraph p = headerRow.Cells[c].AddParagraph();
                p.Format.HorizontalAlignment = HorizontalAlignment.Center;

                Spire.Doc.Fields.TextRange headerText = p.AppendText(dataTable.Columns[c].ColumnName);
                headerText.CharacterFormat.Bold = true;

                CellFormat cellStyle = headerRow.Cells[c].CellFormat;
                cellStyle.VerticalAlignment = VerticalAlignment.Middle;
                headerRow.Cells[c].CellFormat.BackColor = headerBackColor;
            }

            for (int i = 0; i < rowCount; i++)
            {
                object[] rowContent = dataTable.Rows[i].ItemArray;
                DataRow row = dataTable.Rows[i];

                for (int j = 0; j < columnCount; j++)
                {
                    Paragraph p = table.Rows[i + 1].Cells[j].AddParagraph();

                    if (rowContent[j] is byte[])
                    {
                        p.Format.HorizontalAlignment = HorizontalAlignment.Center;
                        p.AppendPicture(rowContent[j] as byte[]);
                    }
                    else
                    {
                        p.AppendText(rowContent[j].ToString());
                    }

                    CellFormat cellStyle = table.Rows[i + 1].Cells[j].CellFormat;
                    cellStyle.VerticalAlignment = VerticalAlignment.Middle;

                    cellStyle.BackColor = rowBackColor;
                    if (i % 2 == 1 && alternationRowColor != Color.Empty)
                    {
                        cellStyle.BackColor = alternationRowColor;
                    }
                }
            }
        }

        private Color ConvertStrToColor(string strColor)
        {
            if (String.IsNullOrWhiteSpace(strColor))
            {
                return Color.Empty;
            }
            else
            {
                return ColorTranslator.FromHtml("#" + strColor);
            }
        }
    }
}

Imports System.Data
Imports System.Drawing

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Formatting


Namespace DemoOnlineCode
    Class Table
        Public Sub demoTable(docFile As [String],
                             dataTable As DataTable,
                             Optional borderColor As String = Nothing,
                             Optional headerBackColor As String = Nothing,
                             Optional rowBackColor As String = Nothing,
                             Optional alternationRowColor As String = Nothing)
            Dim document As New Document(docFile, FileFormat.Auto)
            addTable(document.Sections(0),
                     dataTable, ConvertStrToColor(borderColor),
                     ConvertStrToColor(headerBackColor),
                     ConvertStrToColor(rowBackColor),
                     ConvertStrToColor(alternationRowColor))
            document.SaveToFile("demo.doc", FileFormat.Doc)
        End Sub
        Private Sub addTable(section As Section,
                             dataTable As DataTable,
                             borderColor As Color,
                             headerBackColor As Color,
                             rowBackColor As Color,
                             alternationRowColor As Color)
            Dim table As Spire.Doc.Table = section.AddTable()
            Dim rowCount As Integer = dataTable.Rows.Count
            Dim columnCount As Integer = dataTable.Columns.Count
            table.DefaultRowHeight = 25
            table.DefaultColumnWidth = 0
            table.ResetCells(rowCount + 1, columnCount)

            table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
            table.TableFormat.Borders.Left.Color = borderColor

            table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
            table.TableFormat.Borders.Top.Color = borderColor

            table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
            table.TableFormat.Borders.Right.Color = borderColor

            table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
            table.TableFormat.Borders.Bottom.Color = borderColor

            table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
            table.TableFormat.Borders.Horizontal.Color = borderColor

            table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
            table.TableFormat.Borders.Vertical.Color = borderColor


            Dim headerRow As TableRow = table.Rows(0)
            headerRow.IsHeader = True

            For c As Integer = 0 To columnCount - 1
                Dim p As Paragraph = headerRow.Cells(c).AddParagraph()
                p.Format.HorizontalAlignment = HorizontalAlignment.Center

                Dim headerText As Spire.Doc.Fields.TextRange _
                    = p.AppendText(dataTable.Columns(c).ColumnName)
                headerText.CharacterFormat.Bold = True

                Dim cellStyle As CellFormat = headerRow.Cells(c).CellFormat
                cellStyle.VerticalAlignment = VerticalAlignment.Middle
                headerRow.Cells(c).CellFormat.BackColor = headerBackColor
            Next
            For i As Integer = 0 To rowCount - 1
                Dim rowContent As Object() = dataTable.Rows(i).ItemArray
                Dim row As DataRow = dataTable.Rows(i)

                For j As Integer = 0 To columnCount - 1
                    Dim p As Paragraph = table.Rows(i + 1).Cells(j).AddParagraph()

                    If TypeOf rowContent(j) Is Byte() Then
                        p.Format.HorizontalAlignment = HorizontalAlignment.Center
                        p.AppendPicture(TryCast(rowContent(j), Byte()))
                    Else
                        p.AppendText(rowContent(j).ToString())
                    End If

                    Dim cellStyle As CellFormat = table.Rows(i + 1).Cells(j).CellFormat
                    cellStyle.VerticalAlignment = VerticalAlignment.Middle

                    cellStyle.BackColor = rowBackColor
                    If i Mod 2 = 1 AndAlso alternationRowColor <> Color.Empty Then
                        cellStyle.BackColor = alternationRowColor
                    End If
                Next
            Next
        End Sub
        Private Function ConvertStrToColor(strColor As String) As Color
            If [String].IsNullOrWhiteSpace(strColor) Then
                Return Color.Empty
            Else
                Return ColorTranslator.FromHtml("#" & strColor)
            End If
        End Function
    End Class
End Namespace