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.
WordTable.xaml
<UserControlx:Class="CreateTable.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"d:DesignHeight="254"d:DesignWidth="415">
<UserControl.Background>
<ImageBrushStretch="None" />
UserControl.Background>
<Gridx:Name="LayoutRoot"Height="254"Width="412">
<Grid.RowDefinitions>
<RowDefinitionHeight="0*" />
<RowDefinitionHeight="466*" />
Grid.RowDefinitions>
<Grid.Background>
<ImageBrushImageSource="/CreateTable;component/Images/Table1.jpg" />
Grid.Background>
<sdk:LabelHeight="101"HorizontalAlignment="Center"Margin="-2,59,-2,94"Name="label1"VerticalAlignment="Center"Width="415"Content="Insert Table with Data in Word"FontSize="28"FontFamily="Times New Roman"FontWeight="Bold"Foreground="#FFD5E8D3"Grid.Row="1" />
<ButtonContent="RUN"Height="39"HorizontalAlignment="Left"Margin="180,166,0,0"Name="button1"VerticalAlignment="Top"Width="63"Background="#FFFA58DC"Grid.Row="1"Click="button1_Click" />
Grid>
UserControl>
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