Tab 1
此 Demo 展示如何在 Word 文档中创建带数据的表格,以及如何设置表格的边框和背景颜色。
Data
| Name | Capital | Continent | Area | Population | Flag | 
| Argentina | Buenos Aires | South America | 2777815 | 32300003 | |
| Bolivia | La Paz | South America | 1098575 | 7300000 | |
| Brazil | Brasilia | South America | 8511196 | 150400000 | |
| Canada | Ottawa | North America | 9976147 | 26500000 | |
| Chile | Santiago | South America | 756943 | 13200000 | |
| Colombia | Bagota | South America | 1138907 | 33000000 | |
| Cuba | Havana | North America | 114524 | 10600000 | |
| Ecuador | Quito | South America | 455502 | 10600000 | |
| El Salvador | San Salvador | North America | 20865 | 5300000 | |
| Guyana | Georgetown | South America | 214969 | 800000 | 
Option
downloads
                                    如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo。
如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。。
Tab 2
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);
            }
        }
    }
}
                    Tab 3
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
                    
    


					



