How to Create Table in Word Document with Silverlight

Microsoft Word provides users with a function to insert table. Because table, users can present data information in Word more conveniently. Also, comparing with showing data with text, table can make reader learn data more clearly. In this post, I will share a method about how to create a table with data in Word with Silverlight.

Spire.Doc for Silverlight, a component specialized in operating Word document is used in this method. So, at the beginning, I have add its dll file as reference in project.

STEPS

Step 1. Design UserControl

Rename MainPage.xaml as WordTable.xaml. Double click it and then design UserControl. Set background as an image. Then, add a label. Change label content and set format for text, including font style and color. Finally, add a button to run.

Step 2. Declare saveFileDialog

Declare a saveFiledialog for saving Word document. Then, set filter for this saveFileDialog, which is used for choosing Word format (.doc or .docx). In this example, I just set the format as .docx.

        private SaveFileDialog saveFileDialog = null;
        public MainPage()
        {
            InitializeComponent();
            this.saveFileDialog = new SaveFileDialog();
            this.saveFileDialog.Filter = “Word Documents(*.docx)|*.docx”;
        }

Step 3. Save Data

Declare a new Word document and add section in this document. Then, save header and body data in string arrays.

            Document document = new Document();
            Section section = document.AddSection();
            String[] header = { “Vendor No”, “Vendor Name”, “Address”, “City”, “State”, “Zip” };
            String[][] data =
                {
                    new String[]{“3501”,“Cacor Corporation”,“161 Southfield Rd”,“Southfield”,“OH”,“60093”},
                    new String[]{“3502”,“Underwater”“50 N 3rd Street”,“Indianapolis”,“IN”,“46208”},
                    new String[]{“3503”,“J.W.  Luscher Mfg.”,“65 Addams Street”,“Berkely”,“MA”,“02779”},
                    new String[]{“3504”,“Scuba Professionals”,“3105 East Brace”,“Rancho Dominguez”,“CA”,“90221”},
                    new String[]{“3505”,“Divers’  Supply Shop”,“5208 University Dr”,“Macon”,“GA”,“20865”},
                    new String[]{“3506”,“Techniques”,“52 Dolphin Drive”,“Redwood City”,“CA”,“94065-1086”},
                    new String[]{“3507”,“Perry Scuba”,“3443 James Ave”,“Hapeville”,“GA”,“30354”},
                    new String[]{“3508”,“Beauchat, Inc.”,“45900 SW 2nd Ave”,“Ft Lauderdale”,“FL”,“33315”},
                    new String[]{“3509”,“Amor Aqua”,“42 West 29th Street”,“New York”,“NY”,“10011”},
                    new String[]{“3510”,“Aqua Research Corp.”,“P.O. Box 998”,“Cornish”,“NH”,“03745”},
                    new String[]{“3511”,“B&K Undersea Photo”,“116 W 7th Street”,“New York”,“NY”,“10011”},
                    new String[]{“3512”,“Diving International Unlimited”,“1148 David Drive”,“San Diego”,“DA”,“92102”},
                    new String[]{“3513”,“Nautical Compressors”,“65 NW 167 Street”,“Miami”,“FL”,“33015”},
                    new String[]{“3514”,“Glen Specialties, Inc.”,“17663 Campbell Lane”,“Huntington Beach”,“CA”,“92647”},
                };

Step 3. Create Table

Add table in this section and then use table.ResetCells() method to confirm data row and column number.

            Table table = section.AddTable();
            table.ResetCells(data.Length + 1, header.Length);

Step 4. Set Header and Data Format

Set the first row as header row and then set its row height and background color. Use for loop to get header data information and set font style, color and alignment. Then, use for loop to get body data rows and set format and in this for loop, use another for loop to set data information format. Finally, set border style for whole table.

            TableRow row = table.Rows[0];
            row.IsHeader = true;
            row.Height = 20;
            row.HeightType = TableRowHeightType.Exactly;
            row.RowFormat.BackColor = Color.LimeGreen;
            for (int i = 0; i < header.Length; i++)
            {
                row.Cells[i].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
                Paragraph p = row.Cells[i].AddParagraph();
                p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                TextRange txtRange = p.AppendText(header[i]);
                txtRange.CharacterFormat.Bold = true;
                txtRange.CharacterFormat.TextColor = Color.WhiteSmoke;
            }
            for (int r = 0; r < data.Length; r++)
            {
                TableRow dataRow = table.Rows[r + 1];
                dataRow.Height = 20;
                dataRow.HeightType = TableRowHeightType.Exactly;
                dataRow.RowFormat.BackColor = Color.AliceBlue;
                for (int c = 0; c < data[r].Length; c++)
                {
                    dataRow.Cells[c].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
                    dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
                }
            }

Step 5. Save Document

Judge if the saveFileDialog which I declare at step 1 can pop up. If yes, save the encrypted document through it.

            bool? result = this.saveFileDialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFileDialog.OpenFile())
                {
                    document.SaveToStream(stream, FileFormat.Docx);
                }
            }

Full WordTable.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Drawing;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace CreateTable
{
    public partial class MainPage : UserControl
    {
        private SaveFileDialog saveFileDialog = null;
        public MainPage()
        {
            InitializeComponent();
            this.saveFileDialog = new SaveFileDialog();
            this.saveFileDialog.Filter = “Word Documents(*.docx)|*.docx”;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document document = new Document();
            Section section = document.AddSection();

            String[] header = { “Vendor No”, “Vendor Name”, “Address”, “City”, “State”, “Zip” };
            String[][] data =

                {
                    new String[]{“3501”,“Cacor Corporation”,“161 Southfield Rd”,“Southfield”,“OH”,“60093”},
                    new String[]{“3502”,“Underwater”“50 N 3rd Street”,“Indianapolis”,“IN”,“46208”},
                    new String[]{“3503”,“J.W.  Luscher Mfg.”,“65 Addams Street”,“Berkely”,“MA”,“02779”},
                    new String[]{“3504”,“Scuba Professionals”,“3105 East Brace”,“Rancho Dominguez”,“CA”,“90221”},
                    new String[]{“3505”,“Divers’  Supply Shop”,“5208 University Dr”,“Macon”,“GA”,“20865”},
                    new String[]{“3506”,“Techniques”,“52 Dolphin Drive”,“Redwood City”,“CA”,“94065-1086”},
                    new String[]{“3507”,“Perry Scuba”,“3443 James Ave”,“Hapeville”,“GA”,“30354”},
                    new String[]{“3508”,“Beauchat, Inc.”,“45900 SW 2nd Ave”,“Ft Lauderdale”,“FL”,“33315”},
                    new String[]{“3509”,“Amor Aqua”,“42 West 29th Street”,“New York”,“NY”,“10011”},
                    new String[]{“3510”,“Aqua Research Corp.”,“P.O. Box 998”,“Cornish”,“NH”,“03745”},
                    new String[]{“3511”,“B&K Undersea Photo”,“116 W 7th Street”,“New York”,“NY”,“10011”},
                    new String[]{“3512”,“Diving International Unlimited”,“1148 David Drive”,“San Diego”,“DA”,“92102”},
                    new String[]{“3513”,“Nautical Compressors”,“65 NW 167 Street”,“Miami”,“FL”,“33015”},
                    new String[]{“3514”,“Glen Specialties, Inc.”,“17663 Campbell Lane”,“Huntington Beach”,“CA”,“92647”},
                };

            Table table = section.AddTable();
            table.ResetCells(data.Length + 1, header.Length);
            TableRow row = table.Rows[0];
            row.IsHeader = true;
            row.Height = 20;
            row.HeightType = TableRowHeightType.Exactly;
            row.RowFormat.BackColor = Color.LimeGreen;
            for (int i = 0; i < header.Length; i++)
            {
                row.Cells[i].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
                Paragraph p = row.Cells[i].AddParagraph();
                p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                TextRange txtRange = p.AppendText(header[i]);
                txtRange.CharacterFormat.Bold = true;
                txtRange.CharacterFormat.TextColor = Color.WhiteSmoke;
            }

            for (int r = 0; r < data.Length; r++)
            {
                TableRow dataRow = table.Rows[r + 1];

                dataRow.Height = 20;
                dataRow.HeightType = TableRowHeightType.Exactly;
                dataRow.RowFormat.BackColor = Color.AliceBlue;
                for (int c = 0; c < data[r].Length; c++)
                {
                    dataRow.Cells[c].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
                    dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
                }
            }
            table.TableFormat.Borders.BorderType = BorderStyle.Thick;

            bool? result = this.saveFileDialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFileDialog.OpenFile())
                {
                    document.SaveToStream(stream, FileFormat.Docx);
                }
            }
        }
    }
}

RESULT

___________________________________________________________________________________________

Click Here to LEARN MORE about Spire.Doc for Silverlight

Click Here to DOWNLOAD Spire.Doc for Silverlight

Spire.Office also can be used to realize this function

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

2 thoughts on “How to Create Table in Word Document with Silverlight”

  1. Do you mind if I quote a few of your articles as
    long as I provide credit and sources back to your website?
    My website is in the very same area of interest as
    yours and my users would really benefit from some of the information you
    provide here. Please let me know if this okay with you.
    Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s