ExcelTechnology

How to Convert Doc to XML via VB.NET without Installing Microsoft Word

It is possible that we need to change the documents format sometimes for displaying contents or meeting some special requirements. It is common to convert Word documents or Excel files to other formats for the two formats are frequently used by users and Word and Excel have the function to convert the documents to some other formats directly, for example, html and xml.
For common users, we can use Save As of Word or Excel to make the documents as other formats. But for programmers, they usually need to use C# or VB.NET to convert formats.
We can find several professional programmer forums where programmers discuss their problems or search for help when they need a good method. Also, we can get many good methods about how to convert formats.
Recently, I found a good method about converting doc to XML. Therefore, I want to introduce it and hope that it will be useful for programmers who haven’t installed Microsoft Word on computer but need to convert the document to XML thorough VB.NET.
The following code shows us how to convert doc to XML with VB.NET. And this example is about country and populations. What’s more, we need to install Spire.Doc on computer.
'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)

21.02.2011. u 03:25 | 0 Komentara | Print | # | ^

<< Arhiva >>

Creative Commons License
Ovaj blog je ustupljen pod Creative Commons licencom Imenovanje-Dijeli pod istim uvjetima.

< veljača, 2011 >
P U S Č P S N
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28            

Kolovoz 2011 (2)
Srpanj 2011 (2)
Lipanj 2011 (2)
Svibanj 2011 (2)
Travanj 2011 (3)
Ožujak 2011 (2)
Veljača 2011 (2)
Siječanj 2011 (5)
Prosinac 2010 (5)

Dnevnik.hr
Gol.hr
Zadovoljna.hr
Novaplus.hr
NovaTV.hr
DomaTV.hr
Mojamini.tv

Opis bloga

something about Excel Technology in VB.Net and C#

Linkovi

Dnevnik.hr
Video news portal Nove TV

Blog.hr
Blog servis

Igre.hr
Najbolje igre i igrice

Forum.hr
Monitor.hr