Word to xml in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to export doc document to XML file.

 

//Create word document
Document document = new Document();

Section section = document.AddSection();            

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"},
        new String[]{"Peru", "Lima", "South America", "1285215", "21600000"},
        new String[]{"United States of America", "Washington", "North America", "9363130", "249200000"},
        new String[]{"Uruguay", "Montevideo", "South America", "176140", "3002000"},
        new String[]{"Venezuela", "Caracas", "South America", "912047", "19700000"}
    };
Spire.Doc.Table table = section.AddTable();
table.ResetCells(data.Length + 1, header.Length);

// ***************** First Row *************************
TableRow row = table.Rows[0];
row.IsHeader = true;
row.Height = 20;    //unit: point, 1point = 0.3528 mm
row.HeightType = TableRowHeightType.Exactly;
row.RowFormat.BackColor = Color.Gray;
for (int i = 0; i < header.Length; i++)
{
    row.Cells[i].CellFormat.VerticalAlignment = 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;
}

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.Empty;
    for (int c = 0; c < data[r].Length; c++)
    {
        dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
        dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
    }
}

//Save xml file.
document.SaveToFile("Sample.xml",FileFormat.Xml);

'Create word document
Dim document_Renamed As New Document()

Dim section As Section = document_Renamed.AddSection()

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"}, _
        New String() {"Peru", "Lima", "South America", "1285215", "21600000"}, _
        New String() {"United States of America", "Washington", "North America", "9363130", "249200000"}, _
        New String() {"Uruguay", "Montevideo", "South America", "176140", "3002000"}, _
        New String() {"Venezuela", "Caracas", "South America", "912047", "19700000"} _
    }
Dim table As Spire.Doc.Table = section.AddTable()
table.ResetCells(data.Length + 1, header.Length)

' ***************** First Row *************************
Dim row As TableRow = table.Rows(0)
row.IsHeader = True
row.Height = 20    'unit: point, 1point = 0.3528 mm
row.HeightType = TableRowHeightType.Exactly
row.RowFormat.BackColor = Color.Gray
For i As Integer = 0 To header.Length - 1
    row.Cells(i).CellFormat.VerticalAlignment = 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

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.Empty
    For c As Integer = 0 To data(r).Length - 1
        dataRow.Cells(c).CellFormat.VerticalAlignment = VerticalAlignment.Middle
        dataRow.Cells(c).AddParagraph().AppendText(data(r)(c))
    Next
Next

'Save xml file.
document_Renamed.SaveToFile("Sample.xml", FileFormat.Xml)