The sample demonstrates how to Create Table in Word for Silverlight via Spire.Doc.
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="Table_doc.App"> <Application.Resources> </Application.Resources> </Application>
using System; using System.Windows; namespace Table_doc { public partial class App : Application { public App() { this.Startup += this.Application_Startup; this.Exit += this.Application_Exit; this.UnhandledException += this.Application_UnhandledException; InitializeComponent(); } private void Application_Startup(object sender, StartupEventArgs e) { this.RootVisual = new MainPage(); } private void Application_Exit(object sender, EventArgs e) { } private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { // If the app is running outside of the debugger then report the exception using // the browser's exception mechanism. On IE this will display it a yellow alert // icon in the status bar and Firefox will display a script error. if (!System.Diagnostics.Debugger.IsAttached) { // NOTE: This will allow the application to continue running after an exception has been thrown // but not handled. // For production applications this error handling should be replaced with something that will // report the error to the website and stop the application. e.Handled = true; Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); }); } } private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e) { try { string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace; errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n"); System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");"); } catch (Exception) { } } } }
Imports System.Windows Namespace Table_doc Partial Public Class App Inherits Application Public Sub New() AddHandler Me.Startup, AddressOf Application_Startup AddHandler Me.Exit, AddressOf Application_Exit AddHandler Me.UnhandledException, AddressOf Application_UnhandledException InitializeComponent() End Sub Private Sub Application_Startup(ByVal sender As Object, ByVal e As StartupEventArgs) Me.RootVisual = New MainPage() End Sub Private Sub Application_Exit(ByVal sender As Object, ByVal e As EventArgs) End Sub Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs) ' If the app is running outside of the debugger then report the exception using ' the browser's exception mechanism. On IE this will display it a yellow alert ' icon in the status bar and Firefox will display a script error. If Not Debugger.IsAttached Then ' NOTE: This will allow the application to continue running after an exception has been thrown ' but not handled. ' For production applications this error handling should be replaced with something that will ' report the error to the website and stop the application. e.Handled = True Deployment.Current.Dispatcher.BeginInvoke(Sub() ReportErrorToDOM(e)) End If End Sub Private Sub ReportErrorToDOM(ByVal e As ApplicationUnhandledExceptionEventArgs) Try Dim errorMsg As String = e.ExceptionObject.Message + e.ExceptionObject.StackTrace errorMsg = errorMsg.Replace(""""c, "'"c).Replace(vbCrLf, vbLf) System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(""Unhandled Error in Silverlight Application " & errorMsg & """);") Catch e1 As Exception End Try End Sub End Class End Namespace
<UserControl x:Class="Table_doc.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" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <Button Content="Run" Height="23" HorizontalAlignment="Left" Margin="306,253,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> </Grid> </UserControl>
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 Table_doc { public partial class MainPage : UserControl { private SaveFileDialog saveFileDialog = null; public MainPage() { InitializeComponent(); this.saveFileDialog = new SaveFileDialog(); this.saveFileDialog.Filter = "Word Documents(*.doc)|*.doc"; } private void button1_Click(object sender, RoutedEventArgs e) { //create a blank word document Document document = new Document(); //add one section Section section = document.AddSection(); //add one paragraph Paragraph paragraph = section.AddParagraph(); TextRange txtRange = paragraph.AppendText("This is a sample demonstrates how to create table using Spire.Doc\n\n"); txtRange.CharacterFormat.Font.Bold = true; txtRange.CharacterFormat.Font.Italic = true; txtRange.CharacterFormat.TextColor = Color.Red; //add one table AddTable(section); //save the word document bool? result = this.saveFileDialog.ShowDialog(); if (result.HasValue && result.Value) { using (Stream stream = this.saveFileDialog.OpenFile()) { document.SaveToStream(stream, FileFormat.Doc); } } } private void AddTable(Section section) { //prepare the data of table String[] header = { "Name", "Capital", "Continent", "Area", "Population" }; String[][] data = { new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"}, new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"}, new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"}, new String[]{"Chile", "Santiago", "South America", "756943", "13200000"}, new String[]{"Colombia", "Bagota", "South America", "1138907", "33000000"}, new String[]{"Cuba", "Havana", "North America", "114524", "10600000"}, new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"}, new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"}, new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"}, new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"}, new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"}, new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"}, new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"}, }; //create the table and set the cells Spire.Doc.Table table = section.AddTable(); table.ResetCells(data.Length + 1, header.Length); //edit the header row TableRow row = table.Rows[0]; row.IsHeader = true; row.Height = 20; row.HeightType = TableRowHeightType.Exactly; row.RowFormat.BackColor = Color.Gray; 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; } //edit the data rows 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.Bisque; 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]); } } //add the borders table.TableFormat.Borders.BorderType = BorderStyle.Thick; } } }
Imports System.Windows Imports System.Windows.Controls Imports System.Drawing Imports System.IO Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace Table_doc Partial Public Class MainPage Inherits UserControl Private saveFileDialog As SaveFileDialog = Nothing Public Sub New() InitializeComponent() Me.saveFileDialog = New SaveFileDialog() Me.saveFileDialog.Filter = "Word Documents(*.doc)|*.doc" End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) 'create a blank word document Dim document As New Document() 'add one section Dim section As Section = document.AddSection() 'add one paragraph Dim paragraph As Paragraph = section.AddParagraph() Dim txtRange As TextRange = paragraph.AppendText("This is a sample demonstrates how to create table using Spire.Doc" & vbLf & vbLf) txtRange.CharacterFormat.Font.Bold = True txtRange.CharacterFormat.Font.Italic = True txtRange.CharacterFormat.TextColor = Color.Red 'add one table AddTable(section) 'save the word document Dim result? As Boolean = Me.saveFileDialog.ShowDialog() If result.HasValue AndAlso result.Value Then Using stream As Stream = Me.saveFileDialog.OpenFile() document.SaveToStream(stream, FileFormat.Doc) End Using End If End Sub Private Sub AddTable(ByVal section As Section) 'prepare the data of table Dim header() As String = { "Name", "Capital", "Continent", "Area", "Population" } Dim data()() As String = { New String(){"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, New String(){"Bolivia", "La Paz", "South America", "1098575", "7300000"}, New String(){"Brazil", "Brasilia", "South America", "8511196", "150400000"}, New String(){"Canada", "Ottawa", "North America", "9976147", "26500000"}, New String(){"Chile", "Santiago", "South America", "756943", "13200000"}, New String(){"Colombia", "Bagota", "South America", "1138907", "33000000"}, New String(){"Cuba", "Havana", "North America", "114524", "10600000"}, New String(){"Ecuador", "Quito", "South America", "455502", "10600000"}, New String(){"El Salvador", "San Salvador", "North America", "20865", "5300000"}, New String(){"Guyana", "Georgetown", "South America", "214969", "800000"}, New String(){"Jamaica", "Kingston", "North America", "11424", "2500000"}, New String(){"Mexico", "Mexico City", "North America", "1967180", "88600000"}, New String(){"Nicaragua", "Managua", "North America", "139000", "3900000"}, New String(){"Paraguay", "Asuncion", "South America", "406576", "4660000"} } 'create the table and set the cells Dim table As Spire.Doc.Table = section.AddTable() table.ResetCells(data.Length + 1, header.Length) 'edit the header row Dim row As TableRow = table.Rows(0) row.IsHeader = True row.Height = 20 row.HeightType = TableRowHeightType.Exactly row.RowFormat.BackColor = Color.Gray For i As Integer = 0 To header.Length - 1 row.Cells(i).CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle Dim p As Paragraph = row.Cells(i).AddParagraph() p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center Dim txtRange As TextRange = p.AppendText(header(i)) txtRange.CharacterFormat.Bold = True Next i 'edit the data rows For r As Integer = 0 To data.Length - 1 Dim dataRow As TableRow = table.Rows(r + 1) dataRow.Height = 20 dataRow.HeightType = TableRowHeightType.Exactly dataRow.RowFormat.BackColor = Color.Bisque For c As Integer = 0 To data(r).Length - 1 dataRow.Cells(c).CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle dataRow.Cells(c).AddParagraph().AppendText(data(r)(c)) Next c Next r 'add the borders table.TableFormat.Borders.BorderType = BorderStyle.Thick End Sub End Class End Namespace